Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Last active April 11, 2022 13:27
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecilemuller/40880002c340edaa7e4a to your computer and use it in GitHub Desktop.
Save cecilemuller/40880002c340edaa7e4a to your computer and use it in GitHub Desktop.
Gulp: Output multiple files from a single input file
var path = require('path');
var gulp = require('gulp');
var through2 = require('through2');
var File = require('vinyl');
function generate_two_text_files_from_one_json(){
'use strict';
return through2.obj(function(file, enc, next){
var mydata = JSON.parse(file.contents.toString('utf8'));
var base = path.join(file.path, '..');
var first = new File({
base: base,
path: path.join(base, 'first.txt'),
contents: new Buffer('First file: ' + mydata.something)
});
this.push(first);
var second = new File({
base: base,
path: path.join(base, 'second.txt'),
contents: new Buffer('Second file: ' + mydata.another_thing)
});
this.push(second);
next();
});
}
gulp.task('default', function(){
'use strict';
return gulp.src(['src/*/mydata.json'])
.pipe(generate_two_text_files_from_one_json())
.pipe(gulp.dest('output'));
});
@wfpaisa
Copy link

wfpaisa commented May 4, 2017

thank you 👍

@marcoschneider
Copy link

This is great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment