buildTree = lines => {
const vfs = {}
let current
let currentCommand
for (const line of lines) {
const {groups} = regex.exec(line)
console.log({current, currentCommand, groups})
if (groups.command) {
currentCommand = groups.command
switch (groups.command) {
case 'ls': continue
case 'cd': {
switch (groups.argument.trim()) {
case '..': {
current = current.parent
break
}
case '/': {
vfs['/'] ??= {}
current = vfs['/']
break
}
default: {
current[groups.argument] ??= {parent: current}
current = current[groups.argument]
break
}
}
break
}
}
continue
}
if (currentCommand === 'ls') {
if (groups.directoryName) {
current[groups.directoryName] ??= {parent: current}
}
if (groups.fileName) {
current[groups.fileName] ??= parseInt(groups.size)
}
}
}
return vfs;
}