添加并执行一组脚本到 package.json

Add and execute set of scripts to 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”执行“示例”命令。

我的问题如何运行“Test1”?有可能吗?

您可以通过 && 运算符 运行 多个脚本。例如:

{
  "scripts": {
    "lint": "eslint",
    "test": "jest",
    "ci-test": "npm run lint && npm run test",
  }
}

npm run ci-test 将执行 linttest 脚本。

不支持您示例中的嵌套脚本。

但您可以执行以下操作:

{
  "scripts": {
    "sample": "run --spec '*spec.js'",
    "CB.Test1": "Test one com",
    "CB.Test2": "Test two com"
  }
}

因此您可以调用 npm run CB.Test1

docs

Objects/Arrays 不支持脚本。您必须使用字符串。这意味着您必须逐行添加。

我不认为你能做到这一点,但你应该在 scripts 标签下打破两个类似的命令。 运行 这些命令可以有两种方式:concurrentlysequentially 根据需要。

如果您想 运行 他们同时对 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

您可以在此处查看文档:Concurrently and npm-run-all