简体   繁体   中英

Can you gulp clean all files in a directory except for a single file?

Is there a way to use gulp-clean such that instead of passing in the files or directories I want to delete, to delete everything that does not match a specific file name in the directory?

For example, If I have 3 files in directory "dir":

dir/a.js
dir/b.js
dir/c.js

Sample Pseudocode of what I want to do, (delete everything in /dir/ thats not a.js:

gulp.src('!./dir/a.js').pipe(clean());

This should work:

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

gulp.task('clean', function(cb) {
    del(['dir/**/*', '!dir/a.js'], cb);
});

If the excluded file is in a sub directory you need to exclude that dir from deletion. For example:

del(['dir/**/*', '!dir/subdir', '!dir/subdir/a.js'], cb);

or:

del(['dir/**/*', '!dir/subdir{,/a.js}'], cb);

gulp-filter can be used to filter files from a gulp stream:

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

gulp.src('**/*.js')
    .pipe(filter(['*', '!dir/a.js']))
    .pipe(clean());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM