5

我有一种情况,我需要一个 glob 模式(使用minimatch)来匹配不在某个目录中的所有 JavaScript 文件。不幸的是,我正在使用另一个不公开任何选项的工具(如ignoreglob),因此它必须是一个单一的 glob 才能完成这项工作。

这是我到目前为止所拥有的

globtester 截图

示例输入(它不应顶部匹配,但与底部匹配):

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

到目前为止,这是我对 glob 模式的看法:

**/!(docs)/*.js

有了它,我匹配docs/foo/thing.jsandclient/docs/foo/thing.js而不是匹配docs-src/thing.jsor client/docs-src/thing.js。如果我将 glob 切换到**/!(docs)/**/*.jsthen 我可以匹配client/docs-src/thing.js,但我也匹配client/docs/thing.js

我不确定这是可能的,所以我可能需要为我的问题找到另一种解决方案:-/

4

2 回答 2

5

我认为您可能遇到了 minimatch(或 fnmatch(3) 的任何实现)和 globstar 的限制。可能值得注意的是,我所知道的 fnmatch 的 C 实现实际上并没有实现 globstar,但由于 fnmatch impls(包括 minimatch)服务于他们的 globers 的利益,这可能会有所不同。

您认为应该工作的 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' ]

编辑:哦,我明白了,你只想匹配任何文件夹深度的东西,这些东西在路径的任何地方都没有任何路径 docs部分。不,这不可能以支持任意深度的方式实现,例如 glob 或 minimatch 模式。您必须使用排除项,或构造一个如下所示的 glob:{!(docs),!(docs)/!(docs),!(docs)/!(docs)/!(docs),!(docs)/!(docs)/!(docs)/!(docs)}/*.js

否则,类似的路径x/docs/y/z.js**/!(docs)/**/*.js通过说第一个**不匹配任何内容、!(docs)匹配x、下一个**匹配docs/y然后*.js匹配来匹配z.js

于 2018-01-19T21:49:40.100 回答
1

我靠得更近了,如下:

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

全球测试仪

仍然试图让它在任意深度下工作。

于 2018-01-19T22:12:17.853 回答