grunt(minimatch / glob)文件夹排除 [英] grunt (minimatch/glob) folder exclusion

查看:192
本文介绍了grunt(minimatch / glob)文件夹排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



grunt在引擎盖下使用minimatch(与bsdglob相似)

grunt使用minimatch(类似于bsdglob)以匹配文件,但我似乎无法弄清楚如何做一个.gitignore风格排除文件夹。



我想摄取这个:

p>

ignoreme



并匹配这些:

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

但不匹配:

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



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

  / ** / *。js 

所以我想我可以这样做:

  / ** /!(ignoreme)/ ** / *。js 

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



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

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



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



将正则表达式传递给grunt文件路径的任何方法,或者为我工作?



更新:



以下是我目前正在处理的问题:

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

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

开发版本0.4.0a, grunt.file.expand 方法现在支持排除,并且可以证明这一点比底层minimatch匹配库更复杂的方式。这是可能的,因为 grunt.file.expand 接受多个模式(而minimatch只接受一个)。

grunt.file.expand文档


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

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



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



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


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

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.

I'd like to ingest this:

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:

/**/*.js

So I figured I could do something like:

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

but that matches files in the ignoreme folder.

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?

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.

解决方案

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

From the grunt.file.expand documentation:

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 .js files to the result set, the second pattern would then remove all /ignoreme/ files from the result set.

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

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(minimatch / glob)文件夹排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆