简体   繁体   English

如何编写一个匹配所有不在目录中的js文件的minimatch glob

[英]How to write a single minimatch glob that matches all js files not in a directory

I have a situation where I need a single glob pattern (that uses minimatch ) to match all JavaScript files which are not in a certain directory. 我有一种情况,我需要一个glob模式(使用minimatch )来匹配不在某个目录中的所有JavaScript文件。 Unfortunately, I'm using another tool that doesn't expose any options (like an ignore glob), so it has to be a single glob to do the job. 不幸的是,我正在使用另一个不暴露任何选项的工具(比如ignore glob),所以它必须是一个单独的glob来完成这项工作。

Here's what I have so far 这是我到目前为止所拥有的

globtester的屏幕截图

Example input (it should not match the top, but it should match the bottom): 例如输入(它应该匹配的顶部,但它应该匹配的底部):

docs/foo/thing.js
docs/thing.js
client/docs/foo/thing.js
client/docs/thing.js

src/foo/thing.js
src/thing.js
docs-src/foo/thing.js
docs-src/thing.js
client/docs-src/foo/thing.js
client/docs-src/thing.js

And here's what I have for the glob pattern so far: 到目前为止,这是我对glob模式的看法:

**/!(docs)/*.js

With that I'm matching docs/foo/thing.js and client/docs/foo/thing.js and not matching docs-src/thing.js or client/docs-src/thing.js . 有了这个我匹配docs/foo/thing.jsclient/docs/foo/thing.js而不匹配docs-src/thing.jsclient/docs-src/thing.js If I switch my glob to **/!(docs)/**/*.js then I can match client/docs-src/thing.js , but I also match client/docs/thing.js . 如果我将我的glob切换到**/!(docs)/**/*.js client/docs-src/thing.js然后我可以匹配client/docs-src/thing.js ,但我也匹配client/docs/thing.js

I'm not certain this is possible so I may need to find another solution for my problem :-/ 我不确定这是可能的,所以我可能需要为我的问题找到另一个解决方案: - /

I think you may be running into a limitation of minimatch (or any implementation of fnmatch(3)) and globstar. 我想你可能会遇到minimatch(或fnmatch(3)的任何实现)和globstar的限制。 It's perhaps worth noting that no C implementation of fnmatch that I'm aware of actually does implement globstar, but since fnmatch impls (including minimatch) serve the interests of their globbers, this may vary. 值得注意的是,我所知道的fnmatch的C实现实际上并没有实现globstar,但是由于fnmatch impls(包括minimatch)服务于他们的globbers的利益,这可能会有所不同。

The glob that you think should work actually does work, when used as a glob. 当你用作glob时,你认为应该工作的glob实际上是可行的。

$ find . -type f
./docs/foo/thing.js
./docs/thing.js
./docs/nope.txt
./docs-src/foo/thing.js
./docs-src/thing.js
./x.sh
./client/docs/foo/thing.js
./client/docs/thing.js
./client/docs/nope.txt
./client/docs-src/foo/thing.js
./client/docs-src/thing.js
./client/docs-src/nope.txt
./client/nope.txt
./src/foo/thing.js
./src/thing.js

$ for i in ./!(docs)/**/*.js; do echo $i; done
./client/docs-src/foo/thing.js
./client/docs-src/thing.js
./client/docs/foo/thing.js
./client/docs/thing.js
./docs-src/foo/thing.js
./docs-src/thing.js
./src/foo/thing.js
./src/thing.js

$ node -p 'require("glob").sync("./!(docs)/**/*.js")'
[ './client/docs-src/foo/thing.js',
  './client/docs-src/thing.js',
  './client/docs/foo/thing.js',
  './client/docs/thing.js',
  './docs-src/foo/thing.js',
  './docs-src/thing.js',
  './src/foo/thing.js',
  './src/thing.js' ]

EDIT: Oh, I see, you want to only match things at any folder depth that do not have any docs path parts anywhere in the path. 编辑:哦,我明白了,你想只匹配任何文件夹深度的东西,在路径中的任何地方都没有任何 docs路径部分。 No, that is not possible to do in a way that supports arbitrary depth, as a glob or a minimatch pattern. 不,这不可能以支持任意深度的方式进行,如glob或minimatch模式。 You'll have to use excludes, or construct a glob that looks like: {!(docs),!(docs)/!(docs),!(docs)/!(docs)/!(docs),!(docs)/!(docs)/!(docs)/!(docs)}/*.js 你将不得不使用排除,或构建一个类似于: {!(docs),!(docs)/!(docs),!(docs)/!(docs)/!(docs),!(docs)/!(docs)/!(docs)/!(docs)}/*.js的glob {!(docs),!(docs)/!(docs),!(docs)/!(docs)/!(docs),!(docs)/!(docs)/!(docs)/!(docs)}/*.js

Otherwise, a path like x/docs/y/z.js will match against **/!(docs)/**/*.js by saying that the first ** matches nothing, the !(docs) matches against x , the next ** matches against docs/y and then *.js matches against z.js . 否则,像x/docs/y/z.js这样的路径将匹配**/!(docs)/**/*.js x/docs/y/z.js ,说第一个**没有匹配, !(docs)匹配x ,下一个**docs/y匹配,然后*.jsz.js匹配。

I got closer, with the following: 我走得更近了,有以下几点:

**/?(docs*[a-zA-Z0-9]|!(docs))/*.js

globtester globtester

Still trying to get it to work for arbitrary depth. 仍然试图让它为任意深度工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM