【问题标题】:Calling one script from another with Yarn使用 Yarn 从另一个脚本调用一个脚本
【发布时间】:2020-03-22 19:56:20
【问题描述】:

下面是一个示例,如何使用npmpackage.json 中运行另一个脚本。

yarn 的等价物是什么?

{
  "name": "npm-scripts-example",
  "version": "1.0.0",
  "description": "npm scripts example",
  "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "npm run clean",

  }
}

【问题讨论】:

  • "prebuild": "yarn run clean", 然后yarn run prebuild 应该工作不?你可以做yarn run cleantho
  • 因为我用"scripts": {"test": "yarn -v", "test2": "yarn run test" } 进行了测试,然后yarn run test2 工作得很好。同样根据yarnpkg.com/lang/fr/docs/cli/run yarn run 应该只适用于 package.json 脚本部分中的任何脚本
  • 对不起,你是 100% 正确的。不知道为什么它最初对我不起作用。
  • 没问题,很高兴能帮到你:)
  • 如果您希望脚本使用 npm 或 yarn 运行,请考虑使用 yarpm。例如:"prebuild": "yarpm run clean" 您还可以在 *nix 上使用 $npm_execpath 环境变量 - 如 here 所述。例如"prebuild": "$npm_execpath run clean",。但是,Windows cmd.exe 使用 %...% 语法引用变量 - 例如 "prebuild": "%npm_execpath% run clean"

标签: node.js npm yarnpkg package.json


【解决方案1】:

你可以这样做

{
  "name": "npm-scripts-example",
  "version": "1.0.0",
  "description": "npm scripts example",
  "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "yarn run clean",

  }
}

然后命令yarn run prebuild应该可以工作。 你也可以这样做yarn run clean

run cli 的文档:https://yarnpkg.com/lang/en/docs/cli/run/

【讨论】:

    【解决方案2】:

    改进了@l-faros 的答案,Yarn 还支持更短的语法

    {
     "scripts": {
        "clean": "rimraf ./dist && mkdir dist",
        "prebuild": "yarn clean",
    
      }
    }
    
    

    yarn prebuild 运行脚本命令。

    语法允许省略run 参数。 文档:https://classic.yarnpkg.com/en/docs/cli/#toc-user-defined-scripts

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 2022-08-02
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 2013-11-14
      • 2012-12-17
      相关资源
      最近更新 更多