Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting files in a gulp task

I have a gulp task in which I want to take some source files and copy them to build/premium and build/free and then remove some extra files from build/free.

My attempt at that was doing this:

gulp.task("build", ["clean"], function () {
  gulp.src(["src/*", "!src/composer.*", "LICENSE"])
    .pipe(gulp.dest("build/premium"))
    .pipe(del(["build/free/plugins/*", "!build/free/plugins/index.php"]))
    .pipe(gulp.dest("build/free"));
});

Which results in an error:

TypeError: dest.on is not a function
    at DestroyableTransform.Stream.pipe (stream.js:45:8)
    at Gulp.<anonymous> (/Users/gezim/projects/myproj/gulpfile.js:9:6)

How do I accomplish this the deleting port? Is there a better way altogether to do this?

like image 225
Gezim Avatar asked Jan 27 '16 16:01

Gezim


2 Answers

This is a simple clean task implementation with gulp-del:

var del = require('gulp-del');

gulp.task('clean', function(){
  return del(['folderA/js', 'folderA/css', 'folderB/js']);
});

In your case you can just call it after build (read "use build as a dependency"):

gulp.task("build", function () {
  return gulp.src(['src/*', '!src/composer.*', 'LICENSE'])
    .pipe(gulp.dest("build/premium"))
    .pipe(gulp.dest("build/free"));
});

gulp.task("complete-build", ["build"] function(){
  return del(['build/free/plugins/*', '!build/free/plugins/index.php']);
});

Then call the "complete-build" task to perform it. To be honest this is more a "Grunt"-like approach to the problem, but done with Gulp. Perhaps the recommendation to filter things before writing them in the build/free folder is more in the Gulp spirit.

Update 2/2018

The delete module has been renamed to del now as reported by @gerl:

var del = require('del');

gulp.task('clean', function(){
  return del(['folderA/js', 'folderA/css', 'folderB/js']);
});
like image 106
MarcoL Avatar answered Oct 19 '22 09:10

MarcoL


I would use gulp-filter to drop only what should not be copied from the 2nd destination.

I interpreted the intent of the task as wanting everything present in src to be present in build/premium. However, build/free should exclude everything which was originally in src/plugins but should still include src/plugins/index.php.

Here is a working gulpfile:

var gulp = require("gulp");
var filter = require("gulp-filter");
var del = require("del");

gulp.task("clean", function () {
  return del("build");
});

gulp.task("build", ["clean"], function () {
  return gulp.src(["src/**", "!src/composer.*", "LICENSE"])
    .pipe(gulp.dest("build/premium"))
    .pipe(filter(["**", "!plugins/**", "plugins/index.php"]))
    .pipe(gulp.dest("build/free"));
});

The patterns passed to filter are relative paths. Since the gulp.src pattern has src/** it means they are relative to src.

Note also that del cannot be passed straight to .pipe() as it returns a promise. It can be returned from a task, like the clean task does.

like image 42
Louis Avatar answered Oct 19 '22 08:10

Louis