Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete original files after concat in Gulp

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);
});
like image 980
Roland Soós Avatar asked Jan 07 '23 08:01

Roland Soós


2 Answers

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();
      });
    });
});
like image 67
Sven Schoenung Avatar answered Jan 08 '23 22:01

Sven Schoenung


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);
});
like image 23
Roland Soós Avatar answered Jan 08 '23 22:01

Roland Soós