Pass command line -- argument to child script in Yarn

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.


As an alternative you could use a *.env file and cat the variables out of it in your script.

"run":"docker build -t --build-arg VAR=`cat vars.env` -f Dockerfile .

for example


A straightforward way to achieve this is to write an inline Bash function using parameter expansion with $*:

"scripts": {
    "dev": "wrap () { node index.js \"$*\" | cat; }; wrap"
}

Calling the above with yarn dev foo bar will run node index.js foo bar and then pipe the result into cat as a demo.
You can tack on commands both to the start and the end, simply keep in mind that semis are required here.

For anything more involved you'll probably want a standalone script.


Yarn's run only supports appending your args to the end of the command chain, and at least as of date 2018-06-14, there isn't a way to override that.

When I've needed this in the past, I've cooked up my own dev.js script that was called by my package.json, and pulled args out environment variables.