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());
The clean task clears any image catches and removes any old files present in build. It is possible to clean only specific file or folder and leave some of them untouched as illustrated in the following code. gulp.
To remove all files from a directory, first you need to list all files in the directory using fs. readdir , then you can use fs. unlink to remove each file.
Gulp is a task runner that uses Node. js as a platform. Gulp purely uses the JavaScript code and helps to run front-end tasks and large-scale web applications. It builds system automated tasks like CSS and HTML minification, concatenating library files, and compiling the SASS files.
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());
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