Search code examples
node.jsgulpdel

gulp clean is not working in correct manner


I have directory structure like this.

dist
|--client
   |--assets
      |--images
   |--bower_components
|--server
|--.bower.json

I am trying to clean dist folder, except assets/images folder.

But when i execute this command in dryRun mode, it doesn't remove assets/images file. But after disabling it, it remove all the files and folder.

gulp.task('clean:dist', () => {
   del.sync([
    `${paths.dist}/!(.git*|.openshift|Procfile)**`,
     `${paths.dist}/client/**`,
    `!${paths.dist}/client/assets`,
    `!${paths.dist}/client/assets/**`], {
    //dryRun: true
  });
  //console.log('dELETE FIELSE ARE: ' + JSON.stringify(value));
});

value of used constant is:

${paths.dist} ='dist';

Solution

  • The offical del documentation states the following:

    The glob pattern ** matches all children and the parent.

    So this won't work:

    del.sync(['public/assets/**', '!public/assets/goat.png']);
    

    You have to explicitly ignore the parent directories too:

    del.sync(['public/assets/**', '!public/assets', '!public/assets/goat.png']);
    

    In your case you delete dist/client/**, which includes the dist/client directory itself. If you just ignore dist/client/assets/** the dist/client directory is still deleted.

    You need to explicitly ignore the dist/client directory:

    gulp.task('clean:dist', () => {
       del.sync([
        `${paths.dist}/!(.git*|.openshift|Procfile)**`,
        `${paths.dist}/client/**`,
        `!${paths.dist}/client`,
        `!${paths.dist}/client/assets/**`]);
    });