【问题标题】:Add and execute set of scripts to package.json向 package.json 添加并执行一组脚本
【发布时间】:2021-09-14 07:43:46
【问题描述】:

我是 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”?有可能吗?

【问题讨论】:

    标签: javascript node.js json npm npm-scripts


    【解决方案1】:

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

    {
      "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

    【讨论】:

    • 我的问题如何运行“Test1”?这可能吗?例如“npm run CB.Test1”
    • 不,不支持嵌套脚本/对象。但是请参阅我的更新答案。
    【解决方案2】:

    我认为你不能这样做,但你应该在脚本标签下破坏两个类似的命令。可以有两种方式来运行这些命令: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
    

    您可以在此处查看文档:Concurrentlynpm-run-all

    【讨论】:

      【解决方案3】:

      脚本不支持对象/数组。您必须使用字符串。这意味着您必须逐行添加。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-02
        • 2018-03-03
        • 2020-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        相关资源
        最近更新 更多