将命令行参数或标志传递给NPM package.json脚本

时间:2015-07-01 21:06:25

标签: node.js bash shell npm

我正在寻找做这样的事情:

的package.json

...
"scripts":{

      "debug_mocha_test":"node-debug ./node_modules/mocha/bin/_mocha --grep ${names}"
  }
...

然后在命令行,我可以运行类似的东西:

npm run debug_mocha_test --names 'test1'

或其他什么

有谁知道如何做到这一点,还是有比这更好的方法?

对于某些上下文,mocha测试库具有如下的--grep函数:

http://mochajs.org/

1 个答案:

答案 0 :(得分:2)

从npm 2.0.0开始,可以将参数传递给package.json文件(https://github.com/npm/npm/pull/5518)中定义的脚本。格式并不像人们期望的那样干净,但它确实有效。

传递" foo" " start"的参数脚本:

npm run-script start -- foo=1

所以在你的情况下,package.json中的脚本应该是:

"debug_mocha_test":"node-debug ./node_modules/mocha/bin/_mocha --grep"

您可以使用以下方式运行它:

npm run debug_mocha_test -- test1
相关问题