node.js Glob / minimatch:如何 gulp.src() 所有内容,然后排除文件夹但保留一个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26485612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:02:14  来源:igfitidea点击:

Glob / minimatch: how to gulp.src() everything, then exclude folder but keep one file in it

node.jsgulpglobminimatch

提问by Borek Bernard

I have a project like this:

我有一个这样的项目:

root
  |-incl1
  |-incl2
  |- ...
  |-excl1
  |-excl2
     |- .gitignore  <-- keep this one
     |- (other files)  <-- exclude them

I need to write gulp.src()that will include all folders except excl1and excl2butkeep the .gitignorefile.

我需要写gulp.src()将包括所有文件夹excl1excl2保留.gitignore文件。

This is my code that doesn't work:

这是我的代码不起作用:

gulp.src([
  baseDir + '/**',
  '!' + baseDir + '/{excl1, excl1/**}'
  '!' + baseDir + '/excl2/{**, !.gitignore}'  // <-- doesn't work
], {dot: true})

回答by Heikki

This seems to work:

这似乎有效:

gulp.src([
    baseDir + '/**',                              // Include all
    '!' + baseDir + '/excl1{,/**}',               // Exclude excl1 dir
    '!' + baseDir + '/excl2/**/!(.gitignore)',    // Exclude excl2 dir, except .gitignore
], { dot: true });

Excluding single file from glob match was tricky because there's no similar examples in minimatch docs.

从 glob 匹配中排除单个文件很棘手,因为 minimatch 文档中没有类似的例子。

https://github.com/isaacs/minimatch

https://github.com/isaacs/minimatch

"If the pattern starts with a !character, then it is negated".

“如果模式以!字符开头,则它被否定”。