如何解决添加并执行一组脚本到 package.json
我是 Node.js 新手,需要在 package.json 中添加一组脚本并一个一个执行,解释如下。我需要知道 Node.js 可以吗?
"scripts": {
"sample": "run --spec '*spec.js'","CB":{
"Test1": "Test one com","Test2": "Test two com"
}
},
我可以使用“npm run sample
”执行“sample”命令。
我的问题是如何运行“Test1”?有可能吗?
解决方法
您可以通过 &&
运算符运行多个脚本。例如:
{
"scripts": {
"lint": "eslint","test": "jest","ci-test": "npm run lint && npm run test",}
}
npm run ci-test
将执行 lint
和 test
脚本。
不支持像您的示例中的嵌套脚本。
但您可以执行以下操作:
{
"scripts": {
"sample": "run --spec '*spec.js'","CB.Test1": "Test one com","CB.Test2": "Test two com"
}
}
因此您可以调用例如 npm run CB.Test1
。
见docs。
,脚本不支持对象/数组。你必须使用字符串。这意味着您必须逐行添加。
,我认为您不能这样做,但是您应该在脚本标签下中断两个类似的命令。可以通过两种方式运行这些命令:concurrently
或 sequentially
,具体取决于需求。
如果你想同时运行它们,首先对 package.json 进行这些更改:
npm i --save concurrently
"scripts": {
"sample": "run --spec '*spec.js'","CB":{ "concurrently \"command1 arg\" \"command2 arg\""}
},
npm run CB
如果第二个脚本基于第一个脚本并且想要使用 run-s 按顺序运行,您可以执行以下操作:
"scripts": {
"sample": "run --spec '*spec.js'","Test1": "Test one com","Test2": "Test two com"
},
run-s Test1 Test2
您可以在此处查看文档:{{3}} 和 Concurrently。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。