function listToTree(cmdList) {
const tree = {};
let curr = tree;
cmdList.forEach((cmd) => {
if (cmd.type === "cd" && cmd.name !== "..") {
curr.name = curr.name ?? cmd.name;
}
if (cmd.type === "ls") {
curr.children = cmd.children.map((child) => {
return child.type === "file"
? { name: child.name, size: child.size }
: { name: child.name, children: [] };
});
}
});
return tree;
}