Javascript grunt (minimatch/glob) 文件夹排除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12632029/
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-08-23 10:54:13  来源:igfitidea点击:

grunt (minimatch/glob) folder exclusion

javascriptregexnode.jsglobgruntjs

提问by Jesse

I have a situation where I'm trying to use grunt to lint a codebase, excluding specific folders.

我有一种情况,我试图使用 grunt 来整理代码库,不包括特定文件夹。

grunt uses minimatch (similar to bsdglob) under the hood to match files, but I can't seem to figure out how to do a .gitignore style exclude of a folder.

grunt 在引擎盖下使用 minimatch(类似于 bsdglob)来匹配文件,但我似乎无法弄清楚如何排除文件夹的 .gitignore 样式。

I'd like to ingest this:

我想摄取这个:

ignoreme

ignoreme

and match these:

并匹配这些:

/folder/path/here/to/something/ok.js
/another/folder/path.js
/test.js

but not match these:

但不匹配这些:

/folder/ignoreme/something.js
/folder/path/here/to/ignoreme/metoo/file.js

This will match everything, including ignoreme:

这将匹配所有内容,包括ignoreme:

/**/*.js

So I figured I could do something like:

所以我想我可以做这样的事情:

/**/!(ignoreme)/**/*.js

but that matches files in the ignoreme folder.

但这与 ignoreme 文件夹中的文件匹配。

I'm used to regexes, but can't seem to figure out how to repeat a pattern or something here - I also tried stuff like:

我习惯了正则表达式,但似乎无法弄清楚如何在这里重复模式或其他东西 - 我也尝试过类似的东西:

/(!(ignoreme)|*)*/*.js

hoping the container would repeat, but that doesn't work, it just fails to match everything.

希望容器会重复,但这不起作用,它只是无法匹配所有内容。

Any way to either pass a regex to grunt file paths, or make this work for me?

有什么方法可以将正则表达式传递给 grunt 文件路径,或者让这对我有用?

Update:

更新:

Here's how I'm currently dealing with this issue:

以下是我目前处理此问题的方式:

var pattern = /\/ignoreme\//
var files = grunt.file.expandFiles(arrayOfFilesPassedToMinimatch).filter(function(f){
  return !pattern.test(f)
})

I'd still be interested if folder excludes are possible in minimatch.

如果在 minimatch 中可以排除文件夹,我仍然会感兴趣。

回答by Cowboy Ben Alman

In the currently-in-development version 0.4.0a, the grunt.file.expandmethod now supports exclusions, and does so in an arguably less complex way than the underlying minimatch matching library. This is possible because grunt.file.expandaccepts multiplepatterns (whereas minimatch only accepts one).

在当前正在开发的版本 0.4.0a 中,该grunt.file.expand方法现在支持排除,并且可以说比底层的 minimatch 匹配库更简单。这是可能的,因为grunt.file.expand接受多个模式(而 minimatch 只接受一个)。

From the grunt.file.expand documentation:

grunt.file.expand 文档

This method accepts either comma separated wildcard patterns or an array of wildcard patterns. Paths matching patterns that begin with ! will be excluded from the returned array. Patterns are processed in order, so inclusion and exclusion order is significant.

此方法接受逗号分隔的通配符模式或通配符模式数组。匹配以 ! 开头的模式的路径 将从返回的数组中排除。模式按顺序处理,因此包含和排除顺序很重要。

That means you could specify ['/**/*.js', '!**/ignoreme/**']and while the first pattern would add all .jsfiles to the result set, the second pattern would then remove all /ignoreme/files from the result set.

这意味着您可以指定['/**/*.js', '!**/ignoreme/**'],当第一个模式将所有.js文件添加到结果集中时,第二个模式将从/ignoreme/结果集中删除所有文件。

Take a look at the grunt.file.match unit testsif you're really curious.

如果您真的很好奇,请查看grunt.file.match 单元测试

Note that the version of grunt offering this functionality hasn't officially been released, but if you're interested in using it in a project, see the When will I be able to use in-development feature 'X'?FAQ entry.

请注意,提供此功能的 grunt 版本尚未正式发布,但如果您有兴趣在项目中使用它,请参阅何时才能使用开发中的功能“X”?常见问题入口。