For example, I have some array in the mydata.json
[
{
"name": "item1",
"description": "bla bla bla"
},
{
"name": "item2",
"description": "lorem lorem lorem"
},
{
"name": "item3",
"description": "ipsum ipsum ipsum"
},
]
I need to split the mydata.json
to three files: item1.json
, item2.json
and item3.json
.
What I have to add to my Gulp task?
gulp.task('mytask', function () {
return gulp.src("src/mydata.json")
.pipe( ??? )
.pipe(gulp.dest('./dist'));
});
This is not going to be the most performant solution since everything is done synchronously and it kind of goes outside the typical gulp workflow. It could be modified to use all the async methods if this is a concern. The upside is you can use this without having to write a new plugin.
var fs = require('fs');
gulp.task('mytask', function(done) {
var data = require('src/mydata.json');
console.log('about to write files...')
data.forEach(function(item) {
var fileName = item.name + '.json';
var fileContents = JSON.stringify(item);
fs.writeFileSync(fileName, fileContents);
console.log('saved file: ', fileName);
});
// let gulp know the task is complete
done();
});