ken*_*dds 5 javascript glob node.js minimatch
我有一种情况,我需要一个 glob 模式(使用minimatch)来匹配不在某个目录中的所有 JavaScript 文件。不幸的是,我正在使用另一个不公开任何选项的工具(如ignoreglob),因此它必须是单个 glob 才能完成这项工作。
例如输入(它应该不匹配的顶部,但它应当匹配的底部):
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
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我对 glob 模式的了解:
**/!(docs)/*.js
Run Code Online (Sandbox Code Playgroud)
与我匹配docs/foo/thing.js和client/docs/foo/thing.js不匹配docs-src/thing.js或client/docs-src/thing.js。如果我将 glob 切换到**/!(docs)/**/*.js然后我可以匹配client/docs-src/thing.js,但我也匹配client/docs/thing.js.
我不确定这是否可行,所以我可能需要为我的问题找到另一个解决方案:-/
我认为您可能会遇到 minimatch(或 fnmatch(3) 的任何实现)和 globstar 的限制。也许值得注意的是,我所知道的 fnmatch 的 C 实现实际上没有实现 globstar,但由于 fnmatch impls(包括 minimatch)服务于他们的 globbers 的利益,这可能会有所不同。
当用作 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' ]
Run Code Online (Sandbox Code Playgroud)
编辑:哦,我明白了,你只想匹配任何文件夹深度的东西,在路径的任何地方都没有任何 docs路径部分。不,这不可能以支持任意深度的方式实现,例如 glob 或 minimatch 模式。您必须使用排除项,或构建一个如下所示的 glob:{!(docs),!(docs)/!(docs),!(docs)/!(docs)/!(docs),!(docs)/!(docs)/!(docs)/!(docs)}/*.js
否则,路径 likex/docs/y/z.js将匹配**/!(docs)/**/*.js,表示第一个不**匹配,!(docs)匹配对x,下一个**匹配对docs/y,然后*.js匹配对z.js。
| 归档时间: |
|
| 查看次数: |
1831 次 |
| 最近记录: |