sending custom arguments to npm (yarn) scripts with lerna

时间:2018-01-11 08:41:17

标签: npm yarnpkg npm-scripts lerna

I have an issue when trying to pass arguments to an npm script with lerna.

I have a node script that I want to run inside each package in the workspace. lerna docs suggests the following:

{
    "scripts": {
      "my-script": "lerna exec -- node \\$LERNA_ROOT_PATH/scripts/my-script.js"
    }
}

so now, if I run in the root yarn run my-script it will run the script inside each package in the workspace.

Sometimes, I need to scope the execution to a specific package. So running this from command line obviously works: lerna exec --scope somepackage -- node \$LERNA_ROOT_PATH/scripts/create-common-scripts.js.

My question: how can I connect the npm script with the lerna scope argument. this is not working: yarn run my-script --scope somepackage, as it sets the argument to the end of the command: lerna exec -- node \\$LERNA_ROOT_PATH/scripts/my-script.js --scope somepackage.

Thanks!

1 个答案:

答案 0 :(得分:1)

使用Lerna时,通常会嵌套命令。意味着一个命令将调用另一个命令,等等。例如以下命令:

npm run release (in monorepo root) [1] > lerna run release [2] > npm run release (in package) [3] > release-it [4]

在shell中,可以在运行时使用双破折号(--)将参数传递给嵌套命令。它标记了参数(选项)列表的结尾。 --之后的任何参数都将被提升到下一个命令。这将适用于嵌套了多个级别的命令,您所要做的就是添加--的数量以匹配要传递给它们的命令的级别。

请牢记上一个示例,以下命令:

$ npm run release -- --stream -- -- --dry-run --no-git.requireCleanWorkingDir

将参数提升为:

1. npm run release
2. lerna run release --stream
3. npm run release
4. release-it --dry-run --no-git.requireCleanWorkingDir