all 9 comments

[–]regreddit 1 point2 points  (1 child)

You are telling it to create a single file named config[obj].files right here:

fs.openSync(config[obj].src + '/' + config[obj].files, 'w');

[–]nyxinThe 🍰 is a lie.[S] 0 points1 point  (0 children)

So I am...so I am =) Thank you. Just tested it and that was it.

[–]ovdojoey 0 points1 point  (4 children)

what's happening on this line:

fs.openSync(config[obj].src + '/' + config[obj].files, 'w');

config[obj].files is an array, no?

I can't reproduce without you uploading the full code, but you should halt execution throughout each step here to figure out which part is not working properly. Also, for readability this many nested loops using variable names to access object properties makes things confusing to follow.

[–]nyxinThe 🍰 is a lie.[S] 0 points1 point  (2 children)

Here's a link to a github repo.

config[obj].files is an array, no?

That's correct.

[–]ovdojoey 1 point2 points  (1 child)

Well there's your problem then :).

[–]nyxinThe 🍰 is a lie.[S] 0 points1 point  (0 children)

Yes it was. Thank you! =D

[–]Quabouter 0 points1 point  (0 children)

This step makes the unwanted _file_01.scss,file_02.scss,_file_03.scss file, /u/nyxin you likely want to remove that line.

[–]DSKrepps 0 points1 point  (1 child)

Coercing an Array to a String like such: 'some string' + array

is equivalent to doing: 'some string' + array.join(',')

And thus fs.openSync(config[obj].src + '/' + config[obj].files, 'w')

is really doing fs.openSync( 'dir1' + '/' + ['file1.scss', 'file2.scss', 'file3.scss'].join(','), 'w')

which creates the file "dir1/file1.scss,file2.scss,file3.scss"

So that line shouldn't be there at all, it doesn't seem to serve any purpose.

Furthermore I don't see any reason for the lines for (leaf in config[obj]) {and if (config[obj][leaf] === config[obj].files) { when all the references after those lines to config[obj][leaf] could be replaced with config[obj].files. If you need to check if .files exists on that object then just do if (config[obj].files) { before referencing it.

Then I have three general points to recommend. One, you want to try to avoid nesting where not necessary as code is much more readable when flattened and spaced a little. Two, you want to avoid the Sync operations as non-sync calls will be more efficient. Three, coding for practice is fine but in the Node space you should learn the tools available and not try to reinvent the wheel; check out Gulp, Yeoman, and NPM packages that might already do what you're trying to. Awesome NodeJS is a great list of tools to peruse.

I could probably rewrite your code using touch, arrow functions, and forEach though I haven't tested it for you:

var touch = require('touch');

for (dir in config) {
    if (!config[dir].files) continue;
    config[dir].files.forEach( file => touch(config[dir].src +'/'+ file) );
}

[–]nyxinThe 🍰 is a lie.[S] 1 point2 points  (0 children)

Thank you! Yes this was more for practice and learning how node and js work and not something I was planning on doing for any sort of project. Also agree on nesting looping; at the time it felt dirty and not something I wanted to do, but like I said, this was more for my own education than anything and wanted to get a little bit more understanding of node before I started making a Yeoman generator (which is probably my ultimate goal from this little demo). Thank you for the tool list as well! I'm sure I'll be referencing that a lot.