I would like to concat my files and write it out into a single file, then I want to delete the original files. How should I solve this in my default task?
var es = require('event-stream');
var concat = require('gulp-concat');
var unique = require('array-unique');
function concatGroup(groupName, group){
return gulp.src(unique(group.files))
.pipe(concat(groupName + '.js'))
.pipe(gulp.dest(group.target));
}
gulp.task('default', function () {
var groups = {
test: {
files: [array of files],
target: "target dir"
},
test2: {
files: [array of files],
target: "target dir"
},
test3: {
files: [array of files],
target: "target dir"
}
};
var streams = [];
for (var groupName in groups) {
streams.push(concatGroup(groupName, groups[groupName]));
}
return es.concat.apply(es, streams);
});
Just listen for the end
event in your stream and then use del
:
var concat = require('gulp-concat');
var unique = require('array-unique');
var del = require('del');
gulp.task('default', function (done) {
var groupName = "test",
group = {
files: [array of files],
target: "target dir"
};
var files = unique(group.files)
gulp.src(files)
.pipe(concat(groupName + '.js'))
.pipe(gulp.dest(group.target))
.on('end', function() {
del(files).then(function() {
done();
});
});
});
Thank you guys for your help, here is my final code which works fine. I just had to create a new empty stream and return that for event-stream merge and mark this completed when the files deleted.
var es = require('event-stream');
var concat = require('gulp-concat');
var unique = require('array-unique');
var del = require('del');
function concatGroup(groupName, group) {
var stream = through.obj();
gulp.src(unique(group.files))
.pipe(concat(groupName + '.js'))
.pipe(gulp.dest(group.target))
.on('end', function () {
del(group.files).then(function () {
stream.end();
});
});
return stream;
}
gulp.task('default', function () {
var groups = {
test: {
files: [array of files],
target: "target dir"
},
test2: {
files: [array of files],
target: "target dir"
},
test3: {
files: [array of files],
target: "target dir"
}
};
var streams = [];
for (var groupName in groups) {
streams.push(concatGroup(groupName, groups[groupName]));
}
return es.concat.apply(es, streams);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With