Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass command line -- argument to child script in Yarn

Tags:

I have a package.json that looks similar to this:

"scripts": {     "dev": "cross-env BABEL_ENV=client webpack --config webpack/client.development.js && yarn dev:stub-server | cross-env BABEL_ENV=server babel-node src/server/server.js",     "dev:stub-server": "./node_modules/.bin/robohydra ./stubs/robohydra-config.json -p 3100" } 

I added some logic in the code to change the way the dev:stub-server is configured depending on a command line argument. So, whenever I run the following I get what I expect:

yarn dev:stub-server --results=4 $ ./node_modules/.bin/robohydra ./stubs/robohydra-config.json -p 3100 -- --results=4 

As you can see, the options are forwarded to the underlying script and everything works as expected.

My problem is that I cannot have the --results propagated from the yarn dev command to dev:stub-server in the correct position. The parent script runs dev:stub-server but the argument is forwarded to the underlying script at the end as follows:

yarn dev --results=2 $ cross-env BABEL_ENV=client webpack --config webpack/client.development.js && yarn dev:stub-server | cross-env BABEL_ENV=server babel-node src/server/server.js --results=2 

Is there a way to make the above work as follows instead?

yarn dev --results=2 $ cross-env BABEL_ENV=client webpack --config webpack/client.development.js && yarn dev:stub-server --results=2 | cross-env BABEL_ENV=server babel-node src/server/server.js 

Thanks in advance!

like image 522
Luciano M. L. Avatar asked Jun 13 '18 10:06

Luciano M. L.


People also ask

How do you pass an argument to a yarn script?

yarn run [script] [<args>] json . You can pass additional arguments to your script by passing them after the script name. Running this command will execute jest -o --watch . [script] can also be any locally installed executable that is inside node_modules/.

How do I set an environment variable in yarn?

To set a environment variable you need to use the set command. The result should look like this: set PORT=1234 && yarn start:dev . Save this answer.

Which scripts are used to invoke yarn commands?

YARN commands are invoked by the bin/yarn script. Running the yarn script without any arguments prints the description for all commands. YARN has an option parsing framework that employs parsing generic options as well as running classes.


1 Answers

On mac I am using:

"scripts": {   "benchmark": "sh -c 'ng run ${0}:benchmark'", } 

Which I then call yarn benchmark editor where editor is my parameter.

like image 189
Philippe Avatar answered Sep 21 '22 14:09

Philippe