4

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.

sthames42
  • 752
  • 7
  • 15

3 Answers3

7

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

Louis
  • 128,628
  • 25
  • 249
  • 295
  • This is a good answer so I upvoted. I marked the first as the accepted answer though because this solution requires a 2nd source stream. I was trying to create a single stream for all my source files. – sthames42 Sep 29 '15 at 21:32
  • I don't get what you mean by "requires a 2nd source stream". If you want all files in `foo` but not directories you do `gulp.src("foo/**", { nodir: true })`. This is *it*. One source. – Louis Sep 29 '15 at 23:13
  • I'm sorry, @Louis, what I meant was I was copying many other files in other folders and subfolders using file globbing. It was only in this one folder I didn't want the subfolders. Using the nodir option would have excluded all folders AFAIK so a second stream would be required. – sthames42 Sep 30 '15 at 01:38
  • You're right of course and I did have to filter out a .svn folder at the root but I had to do that anyway for the other folders. I confess I didn't know about es until after i posted the question but I use it for much now. The other solution is still more appropriate for me in this case but I upvoted again for the elegance of your updated solution. It's certain to help someone else. – sthames42 Sep 30 '15 at 12:23
6

If you want to include .dotFiles as well then try

gulp.src(['folder/*.*', 'folder/.*'])
Dick Grayson
  • 126
  • 4
  • 1
    As Louis points out and I can confirm, this will include hidden folders like .svn which must be specifically filtered. If you don't mind multiple streams and you don't know what hidden folders you may have, his solution is better. – sthames42 Sep 30 '15 at 12:28
  • Agreed. And this will also not catch files without an extension, e.g., the "LICENSE" file generated by GitHub (in case you for some reason wanted to copy that). – sfarbota Jan 02 '16 at 09:47
1

You can try

gulp.src('folder/*.*')

This won't match dot files however.

kombucha
  • 1,394
  • 1
  • 10
  • 19