问题
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:
When using Lerna commands are often nested. Meaning one command will call anther command, etc. For example the following command:
npm run release (in monorepo root) [1] > lerna run release [2] > npm run release (in package) [3] > release-it [4]
In a shell arguments can be passed to nested commands at runtime using a double dash (--
). It marks the end of the parameter (option) list. Any parameter after the --
will be hoisted to the next command. This will work with commands nested multiple levels deep, all you have to do is add the number of --
to match the level of the command you want to pass them to.
With the previous example in mind, the following command:
$ npm run release -- --stream -- -- --dry-run --no-git.requireCleanWorkingDir
Will hoists the parameters to:
1. npm run release
2. lerna run release --stream
3. npm run release
4. release-it --dry-run --no-git.requireCleanWorkingDir
来源:https://stackoverflow.com/questions/48202815/sending-custom-arguments-to-npm-yarn-scripts-with-lerna