| 15 | export function flatFileListToDirectoryStructure(files) { |
| 16 | const inodes = new Map() |
| 17 | const mkdir = function (name) { |
| 18 | if (!inodes.has(name)) { |
| 19 | const dir = { |
| 20 | type: 'tree', |
| 21 | fullpath: name, |
| 22 | basename: basename(name), |
| 23 | metadata: {}, |
| 24 | children: [], |
| 25 | } |
| 26 | inodes.set(name, dir) |
| 27 | // This recursively generates any missing parent folders. |
| 28 | // We do it after we've added the inode to the set so that |
| 29 | // we don't recurse infinitely trying to create the root '.' dirname. |
| 30 | dir.parent = mkdir(dirname(name)) |
| 31 | if (dir.parent && dir.parent !== dir) dir.parent.children.push(dir) |
| 32 | } |
| 33 | return inodes.get(name) |
| 34 | } |
| 35 | |
| 36 | const mkfile = function (name, metadata) { |
| 37 | if (!inodes.has(name)) { |