npm glob模式不匹配子目录 [英] npm glob pattern not matching subdirectories

查看:100
本文介绍了npm glob模式不匹配子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的package.json中,我有一个脚本块,该脚本块使用**/*Test.js来匹配文件.通过npm运行时,它们与多个子目录的匹配程度不超过一个级别.直接在命令行上执行时,它们将按预期工作.

In my package.json, I have a scripts block that uses **/*Test.js to match files. When run via npm, they do not match sub-directories more than one level. When executed on the command line directly, they work as expected.

任何人都可以解释正在发生的事情,并提供解决方法或解决方案吗?

Can anyone explain what is happening, and provide a workaround or solution?

{
  "name": "immutable-ts",
  "scripts": {
    "test": "echo mocha dist/**/*Test.js",
  }
}

执行

% npm run test

> immutable-ts@0.0.0 test:unit .../immutable-ts
> echo mocha dist/**/*Test.js

mocha dist/queue/QueueTest.js dist/stack/StackTest.js

% echo mocha dist/**/*Test.js

mocha dist/queue/QueueTest.js dist/stack/StackTest.js dist/tree/binary/BinaryTreeTest.js

% ls dist/**/*                                                                                                                                                                                          

dist/collections.js  dist/queue/QueueTest.js  dist/tree/binary/BinaryTree.js      dist/immutable.js.map        dist/stack/Stack.js.map             dist/tree/binary/BinaryTreeTest.js.map
dist/immutable.js    dist/stack/Stack.js      dist/tree/binary/BinaryTreeTest.js  dist/queue/Queue.js.map      dist/stack/StackTest.js.map
dist/queue/Queue.js  dist/stack/StackTest.js  dist/collections.js.map             dist/queue/QueueTest.js.map  dist/tree/binary/BinaryTree.js.map

推荐答案

解决方案

更改脚本,以使传递给Mocha的内容不受外壳程序的扩展保护:

Solution

Change your scripts so that what you pass to Mocha is protected from expansion by the shell:

"scripts": {
    "test": "mocha 'dist/**/*Test.js'",
 }

请注意为mocha赋予的参数周围的单引号.

Note the single quotes around the parameter given to mocha.

此问题无需借助外部工具即可解决.问题的根本原因是npm使用sh作为将运行脚本命令的外壳.

This issue is fixable without resorting to external tools. The root cause of your problem is that by npm uses sh as the shell that will run your script commands.

在绝大多数情况下,当* nix进程启动shell时,它将启动sh,除非有其他提示.您为登录名设置的shell首选项不构成以其他方式告诉它"的方法.因此,如果您以zsh作为登录外壳,则无需npm使用zsh.

It is overwhelmingly the case that when a *nix process starts a shell it will start sh unless there is something telling it to do otherwise. The shell preference you set for logins does not constitute a way to "tell it otherwise". So if you have, say, zsh as your login shell, it does not entail that npm will use zsh.

sh的那些实现,除了sh应该提供的扩展之外,不包括任何扩展名,因此无法以您希望的方式理解** glob.据我所知,它被解释为*.但是, Mocha使用其glob的JavaScript实现解释传递给它的路径.因此,您可以通过保护全局变量免受sh解释来解决此问题.考虑以下package.json:

Those implementations of sh that do not include any extensions beyond what sh should provide do not understand the ** glob in the way you want it to. As far as I can tell, it is interpreted as *. However, Mocha interprets the paths passed to it using its a JavaScript implementation of globs. So you can work around the issue by protecting your globs from being interpreted by sh. Consider the following package.json:

{
  "name": "immutable-ts",
  "scripts": {
    "bad": "mocha test/**/*a.js",
    "good": "mocha 'test/**/*a.js'",
    "shell": "echo $0"
  }
}

shell脚本就是这样,我们可以检查正在运行该脚本的shell.如果运行它,应该会看到sh.

The shell script is just so that we can check what shell is running the script. If you run it, you should see sh.

现在,给出以下树:

test/
├── a.js
├── b.js
├── x
│   ├── a
│   │   ├── a.js
│   │   └── b.js
│   ├── a.js
│   └── b
│       └── a.js
└── y
    ├── a.js
    └── q

所有包含it(__filename);a.jsb.js文件.您得到以下结果:

With all a.js and b.js files containing it(__filename);. You get the following results:

$ npm run bad

> immutable-ts@ bad /tmp/t2
> mocha test/**/*a.js

  - /tmp/t2/test/x/a.js
  - /tmp/t2/test/y/a.js

  0 passing (6ms)
  2 pending

$ npm run good

> immutable-ts@ good /tmp/t2
> mocha 'test/**/*a.js'

  - /tmp/t2/test/a.js
  - /tmp/t2/test/x/a.js
  - /tmp/t2/test/x/a/a.js
  - /tmp/t2/test/x/b/a.js
  - /tmp/t2/test/y/a.js

  0 passing (5ms)
  5 pending

这篇关于npm glob模式不匹配子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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