Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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()); 
like image 899
Rolando Avatar asked Feb 16 '15 17:02

Rolando


People also ask

What does gulp clean do?

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.

How do I delete all files in a directory in FS?

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.

What was the used by the gulp files?

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.


2 Answers

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);

like image 140
Heikki Avatar answered Oct 02 '22 07:10

Heikki


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()); 
like image 38
Jonas Berlin Avatar answered Oct 02 '22 07:10

Jonas Berlin