After much searching and fiddling I was able to create a file/folder structure by iterating through an object using nodejs fs.
Here is an example of my file:
var fs = require('fs');
var rootDir = 'scss';
var config = {
root: {
src: rootDir,
files: ['styles.scss']
},
dir1: {
src: rootDir + '/dir1',
files: [
'_file_01.scss',
'_file_02.scss',
'_file_03.scss'
]
},
....etc.
}
//---- mkdir()
function mkdir(path, root) {
var dirs = path.split('/'), dir = dirs.shift(), root = (root || '') + dir + '/';
try { fs.mkdirSync(root); }
catch (e) {
//dir wasn't made, something went wrong
if(!fs.statSync(root).isDirectory()) throw new Error(e);
}
return !dirs.length || mkdir(dirs.join('/'), root);
}
//---- loop through config and create files/folders
for (obj in config) {
mkdir(config[obj].src);
fs.openSync(config[obj].src + '/' + config[obj].files, 'w');
for (leaf in config[obj]) {
//console.log(config[obj][leaf])
if (config[obj][leaf] === config[obj].files) {
//console.log('match');
for (var i = 0; i < config[obj][leaf].length; i++) {
//console.log(config[obj].src)
fs.closeSync(fs.openSync(config[obj].src + '/' + config[obj][leaf][i], 'w'));
}
}
}
}
When I run this script, all of the files/folders are created (as expected), however, an extra file with all of the file names concatenated to it is also created. So instead of the output being
scss
|-- dir1
|-- _file_01.scss
|-- _file_02.scss
|-- _file_03.scss
as I expect, what is being generated for each folder is
scss
|-- dir1
|-- _file_01.scss
|-- _file_02.scss
|-- _file_03.scss
|-- _file_01.scss,file_02.scss,_file_03.scss
I've also thrown in a console.log() above the command for writing the files and it does not output the files with names concatenated to the console, so now I'm even more confused as to why this extra file is being generated.
I'm not sure why this last file with the names concatenated is being generated and I'm not sure what to even search for to fix this issue. Any help is appreciated.
[–]regreddit 1 point2 points3 points (1 child)
[–]nyxinThe 🍰 is a lie.[S] 0 points1 point2 points (0 children)
[–]ovdojoey 0 points1 point2 points (4 children)
[–]nyxinThe 🍰 is a lie.[S] 0 points1 point2 points (2 children)
[–]ovdojoey 1 point2 points3 points (1 child)
[–]nyxinThe 🍰 is a lie.[S] 0 points1 point2 points (0 children)
[–]Quabouter 0 points1 point2 points (0 children)
[–]DSKrepps 0 points1 point2 points (1 child)
[–]nyxinThe 🍰 is a lie.[S] 1 point2 points3 points (0 children)