Question

I am creating a Yeoman generator app. I want to create a set of parent directories and each parent directory has the same set of child templates.

Right now I am using the below commands repeatedly to achieve this. Is there a better way to loop over an array and achieve the same?

this.mkdir('app/scss/modules/tables');
this.mkdir('app/scss/modules/navigation');
this.mkdir('app/scss/modules/pagination');

this.copy('_extends.scss', 'app/scss/modules/navigation/_extends.scss');
this.copy('_mixins.scss', 'app/scss/modules/navigation/_mixins.scss');
this.copy('_variables.scss', 'app/scss/modules/navigation/_variables.scss');

this.copy('_extends.scss', 'app/scss/modules/pagination/_extends.scss');
this.copy('_mixins.scss', 'app/scss/modules/pagination/_mixins.scss');
this.copy('_variables.scss', 'app/scss/modules/pagination/_variables.scss');

this.copy('_extends.scss', 'app/scss/modules/tables/_extends.scss');
this.copy('_mixins.scss', 'app/scss/modules/tables/_mixins.scss');
this.copy('_variables.scss', 'app/scss/modules/tables/_variables.scss');
Était-ce utile?

La solution

I reckon you'd need two arrays, and at least two loops.

In pseudocode:

dirs  = [ ... directories ... ];
files = [ ... files ... ];

for (directory in dirs) {
    mkdir (d);

    for (file in files) {
        copy(file, directory + file);
    }

}

If you ever need another directory with all files, or another file to go in all directories you'd just add it to the corresponding array.

Hope you find this useful!

Autres conseils

You could also do something like this:

dirs  = [ "folder1", "folder2", "etc" ];
files = [ "file1", "file2", "etc" ];

dirs.forEach(function(directory){
    this.mkdir(directory);

    files.forEach(function(file){
        this.copy(file, directory + file);
    }.bind(this));

}.bind(this));

...if you want to avoid using a 'for-in' loop, since they're somewhat error-prone.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top