在节点 package.json 中,使用额外参数从另一个脚本调用脚本,在本例中添加 mocha watcher

In node package.json, invoke script from another script with extra parameter, in this case add mocha watcher

在节点的 package.json 中我想重用我在 'script'.

中已有的命令

这是实际例子

而不是(注意 watch 脚本中的额外 -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",
           }

我想要类似的东西

"scripts": {
             "test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
             "watch": "npm run script test" + "-w",
           }

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

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

所以也许还有其他选择?

这可以在 npm@2.1.17 中完成。您没有指定您的 OS 和您正在使用的 npm 的版本,但除非您已对其进行更新,否则您可能是 运行 npm@1.4.28 支持以下语法。

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

您应该可以通过向脚本传递一个附加参数来做到这一点:

"scripts": {
  "test": "mocha --compilers coffee:coffee-script/register --recursive -R list",
  "watch": "npm run test -- -w"
}

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

{
  "scripts": { "a": "ls", "b": "npm run a -- -l" }
}

输出:

$ 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
$