简体   繁体   English

在节点package.json中,使用其他参数从另一个脚本调用脚本,在这种情况下,请添加mocha watcher

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

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 -w on the watch script): 而不是(请注意监视脚本上的额外-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中做字符串concat),但是您应该得到我想要的

I know that npm scripts support: - & (parallel execution) - && (sequencial execution) 我知道npm脚本支持:-&(并行执行)-&&(顺序执行)

so maybe there is another option? 所以也许还有另一种选择?

This can be done in npm@2.1.17 . 可以在npm@2.1.17完成。 You don't specify your OS and the version of npm that you are using, but unless you have done something to update it, you are probably running npm@1.4.28 which does not support the syntax below. 您没有指定您的操作系统和使用的npm版本,但是除非您进行了一些更新,否则您可能正在运行npm@1.4.28 ,它支持以下语法。

On Linux or OSX you can update npm with sudo npm install -g npm@latest . 在Linux或OSX上,您可以使用sudo npm install -g npm@latest更新npm。 See https://github.com/npm/npm/wiki/Troubleshooting#try-the-latest-stable-version-of-npm for a guide to updating npm on all platforms. 有关在所有平台上更新npm的指南,请参见https://github.com/npm/npm/wiki/Troubleshooting#try-the-latest-stable-version-of-npm

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
$ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM