Reputation: 922
path.json
{
"plguin_a_name": {
"css": {
"src/plugin_a/1.css": "dist/plugin_a/1.css",
"src/plugin_a/2.css": "dist/plugin_a/2.css"
},
"js": {
"src/plugin_a/1.js": "dist/plugin_a/1.js",
"src/plugin_a/2.js": "dist/plugin_a/2.js"
}
},
"plugin_b_name": {
"css": {
"src/plugin_b/1.css": "dist/plugin_b/1.css",
"src/plugin_b/2.css": "dist/plugin_b/2.css"
},
"js": {
"src/plugin_b/1.js": "dist/plugin_b/1.js",
"src/plugin_b/2.js": "dist/plugin_b/2.js"
}
}
}
How can loop from this json file in gulp to copy each plugin from src to dist with a return?
I would like to hae two task to loop only the css/js or loop all object inside "plugin_x_name".
Is this function need some plugin?
Upvotes: 3
Views: 700
Reputation: 45402
You can build some function that handle an array of plugin/type to filter those which are needed :
[{
"plugin": "plugin_a_name",
"types": ["js"]
}, {
"plugin": "plugin_b_name"
}]
In the above sub-config, it will only copy JS file for plugin_a
and all the types (JS/CSS) for plugin_b
.
You can then iterate over all the filtered plugin/types and grab them from your config path.json
file if existing :
var gulp = require('gulp'),
config = require('./path.json');
function copyTask(target) {
//iterate over config files for the specified type
for (src in target) {
console.log("copy from " + src + " to " + target[src]);
gulp.src(src).pipe(gulp.dest(target[src]));
}
}
function copyFilter(pluginFilter) {
//loop on filter
for (var obj in pluginFilter) {
var pluginVal = pluginFilter[obj];
//check config has plugin
if (config[pluginVal.plugin]) {
if (pluginVal.types) {
//loop on type type filter
for (var type in pluginVal.types) {
var typeVal = pluginVal.types[type];
// check config has type
if (config[pluginVal.plugin][typeVal]) {
copyTask(config[pluginVal.plugin][typeVal]);
} else {
console.log("type " + typeVal + " not found for plugin " + pluginVal.plugin);
}
}
} else {
//iterate over all the types here
for (type in config[pluginVal.plugin]) {
copyTask(config[pluginVal.plugin][type]);
}
}
} else {
console.log("plugin " + pluginVal.plugin + " not found");
}
}
}
gulp.task('copyFromJson', function() {
copyFilter([{
"plugin": "plugin_a_name",
"types": ["js"]
}, {
"plugin": "plugin_b_name"
}]);
});
gulp.task('default', ['copyFromJson']);
Upvotes: 1