在节点package.json中,使用额外参数从另一个脚本调用脚本,在这种情况下添加mocha观察器

Din*_*ruz 39 json mocha.js node.js npm

在node的package.json中我想重用一个'脚本'中已有的命令.

这是一个实际的例子

而不是(注意监视脚本上的额外-w):

"scripts": {
             "test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
             "watch": "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list -w",
           }
Run Code Online (Sandbox Code Playgroud)

我想有类似的东西

"scripts": {
             "test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
             "watch": "npm run script test" + "-w",
           }
Run Code Online (Sandbox Code Playgroud)

这不起作用(不能在json中做字符串连接),但你应该得到我想要的

我知道npm脚本支持: - &(并行执行) - &&(顺序执行)

所以也许有另一种选择?

Sam*_*kes 57

这可以在npm@2.1.17.您没有指定您的操作系统和npm您正在使用的版本,但除非您已经做了更新操作,否则您可能正在运行npm@1.4.28,它支持下面的语法.

在Linux或OSX上,您可以使用更新npm sudo npm install -g npm@latest.有关在所有平台上进行更新的指南,请参阅https://github.com/npm/npm/wiki/Troubleshooting#try-the-latest-stable-version-of-npmnpm.

您应该可以通过向脚本传递一个额外的参数来执行此操作:

"scripts": {
  "test": "mocha --compilers coffee:coffee-script/register --recursive -R list",
  "watch": "npm run test -- -w"
}
Run Code Online (Sandbox Code Playgroud)

我使用以下简化的package.json验证了这一点:

{
  "scripts": { "a": "ls", "b": "npm run a -- -l" }
}
Run Code Online (Sandbox Code Playgroud)

输出:

$ npm run a

> @ a /Users/smikes/src/github/foo
> ls

package.json
$ npm run b

> @ b /Users/smikes/src/github/foo
> npm run a -- -l


> @ a /Users/smikes/src/github/foo
> ls -l

total 8
-rw-r--r--  1 smikes  staff  55  4 Jan 05:34 package.json
$ 
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,在 shell(bash、zsh 等)中,大多数(不是全部)命令使用“--”双破折号来指示“选项结束”“man bash”给出以下信息:“A -- 信号”选项结束并禁用进一步的选项处理。-- 之后的任何参数都被视为文件名和参数。- 的参数相当于 --。` (3认同)
  • TIL `--` 告诉 `npm run` 不要在 `--` 之后查找选项(在本例中为 `-w`)。 (2认同)