Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp.src() include files but ignore all folders

There is certainly such a simple answer to this that I can't find anyone that's asked it before:

What globbing pattern to include all the files in a folder but ignore ALL subfolders?

gulp.src('*') includes all files and folders. I just want the files and would rather not have to exclude the folders individually.

like image 670
sthames42 Avatar asked Dec 08 '22 01:12

sthames42


1 Answers

Just use the nodir option when you call gulp.src. This will actually test the files being read off the filesystem instead of relying on conventions that may hold today but not tomorrow.

In the following example, the debug() in the dir target will output files and directories. The debug() in the nodir target will output only files because it uses nodir. Populate the directory foo with files and subdirectories and you'll see the difference.

var gulp = require("gulp");
var debug = require("gulp-debug");

gulp.task("dir", function () {
    return gulp.src("foo/**")
        .pipe(debug());
});

gulp.task("nodir", function () {
    return gulp.src("foo/**", { nodir: true })
        .pipe(debug());
});

If you want to have one part of the tree include directories and another part exclude them then you can merge streams:

var es = require("event-stream");
gulp.task("nodir-multi", function () {
    return es.merge(gulp.src("foo/**", { nodir: true }),
                    gulp.src("bar/**"))
        .pipe(debug());
});

I don't think the convenience of having just one gulp.src call justifies relying on *.* and .* to exclude directories. Version control systems often create directories what would match .* (e.g. .git, .svn). Development tools also do the same (e.g. .deps, .tmp.foo).

like image 87
Louis Avatar answered Mar 17 '23 06:03

Louis