node.js 在 node package.json 中,从另一个带有额外参数的脚本中调用脚本,在这种情况下添加 mocha watcher

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27736579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:15:55  来源:igfitidea点击:

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

jsonnode.jsnpmmocha

提问by Dinis Cruz

in node's package.json I would like to reuse a command that I already have in a 'script'.

在节点的 package.json 中,我想重用“脚本”中已有的命令。

Here is the practical example

这是实际例子

instead of (note the extra -won the watchscript):

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

I would like to have something like

我想要类似的东西

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

which doesn't work (can't do string concats in json), but you should get what I would like

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

I know that npm scripts support: - & (parallel execution) - && (sequencial execution)

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

so maybe there is another option?

所以也许还有另一种选择?

回答by Sam Mikes

This can be done in [email protected]. You don't specify your OS and the version of npmthat you are using, but unless you have done something to update it, you are probably running [email protected]which does notsupport the syntax below.

这可以在[email protected]. 您没有指定您的操作系统和版本npm所使用,但除非你已经做了更新它,你可能运行[email protected]它并不能支持下面的语法。

On Linux or OSX you can update npm with sudo npm install -g npm@latest. See https://github.com/npm/npm/wiki/Troubleshooting#try-the-latest-stable-version-of-npmfor a guide to updating npmon all platforms.

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

You should be able to do this by passing an additional argument to your script:

您应该能够通过向脚本传递一个额外的参数来做到这一点:

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

I verified this using the following, simplified package.json:

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

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

Output:

输出:

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