Reputation: 111
I have a program that takes two files one output.
Say I have gulp find the files input1.json
, input1.xml
, input2.json
, and input2.xml
. How can I then pass these files in pairs to the output to produce input1.output
and input2.output
?
The base filenames will always be the same, so it is just a case of pairing the files by the basenames, but I cannot use a fixed array of files to pick from.
The files cannot be concatenated or included in each other in any way.
Upvotes: 0
Views: 178
Reputation: 867
Try the code below. This should work as is. Just plug it in and see if it helps you out. I think it does what you need it to do. Please let me know how it goes.
gulp.task('myTask', function() {
var dir = "<MY BASE DIR>";
var streams = [];
var basename = "input";
var counter = 1;
var jsonFile = path.join(dir, basename + (counter++) + ".json");
while (fs.existsSync(jsonFile)) {
// Iterate through as many files as you have
var stream = gulp.src(jsonFile)
.pipe(through.obj(function(jsonFile, enc, callback) {
var xmlFilepath = path.join(dir, path.basename(jsonFile.path, ".json") + ".xml");
var xmlContents = fs.readFileSync(xmlFilepath);
var xmlFile = new plugins.util.File({path: xmlFilepath, contents: xmlContents});
// You now have access to both the json and the xml file
this.push(jsonFile);
this.push(xmlFile);
callback();
}))
.pipe(plugins.rename(function(path){
// This will only affect one single file per pair
path.extname = '.output';
}))
.pipe(gulp.dest(dir));
streams.push(stream);
// Keep looking for more files
jsonFile = path.join(dir, basename + (counter++) + ".json");
}
return merge(streams);
}
Upvotes: 1