Last active
August 4, 2022 06:32
-
-
Save sethdavis512/49af6ad04888721760459a04347406e9 to your computer and use it in GitHub Desktop.
Use object to build directory structure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
// Paths functions from | |
// https://lowrey.me/getting-all-paths-of-an-javascript-object/ | |
function getPaths(root) { | |
let paths = []; | |
let nodes = [{ | |
obj: root, | |
path: [] | |
}]; | |
while (nodes.length > 0) { | |
let n = nodes.pop(); | |
Object.keys(n.obj).forEach(k => { | |
if (typeof n.obj[k] === 'object') { | |
let path = n.path.concat(k); | |
paths.push(path); | |
nodes.unshift({ | |
obj: n.obj[k], | |
path: path | |
}); | |
} | |
}); | |
} | |
return paths; | |
} | |
const rootFolder = process.argv[2] || 'your-project-name'; | |
const directoryStructure = { | |
[rootFolder]: { | |
first: { | |
second: {}, | |
third: { | |
fourth: {} | |
} | |
} | |
} | |
}; | |
const allPaths = getPaths(directoryStructure); | |
allPaths.forEach(path => { | |
const pathName = path.join('/'); | |
fs.mkdirSync(pathName); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So i may have had a little too much fun...
😈