Gulp glob to ignore file types and not copy empty

2019-04-29 22:09发布

问题:

I have created a glob for gulp which ignores javascript and coffeescript files within a set of directories. I'd like it to copy all other files into a directory which works fine. The only problem is that when there are only javascript or coffeescript files it copies an empty folder. Any ideas how this glob could be amended to not copy empty folders?

gulp.task('copyfiles', function(){
    gulp.src('apps/*/static_src/**/!(*.js|*.coffee)')
        .pipe(gulp.dest('dest'));
});

Example source files:

apps/appname/static_src/images/image.jpg
apps/appname/static_src/js/script.js

Expected output:

dest/static_src/images/image.jpg

Current output:

dest/static_src/images/image.jpg
dest/static_src/js/

回答1:

Since gulp.src accepts almost the same options as node-glob, you can add nodir: trueas an option:

gulp.src('apps/*/static_src/**/!(*.js|*.coffee)', { nodir: true })

This will preserve the dir structure from src, but omit empty ones.



回答2:

gulp.task('copyfiles', function(){
    gulp.src(['apps/*/static_src/**/*','!apps/*/static_src/{js/, js/**}'])
        .pipe(gulp.dest('dest'));
});

I think you need a pattern '!apps/*/static_src/{js/, js/**}' that matches the directory as well as the files inside to prevent ommiting an empty directory. I am not sure if there is a pattern to match a directory only by specifying its content.