yarn run pass parameters to multiple commands?

Created on 10 Jan 2018  路  15Comments  路  Source: yarnpkg/yarn

yarn 1.3.2

Is there some way I can pass a parameter to multiple commands?

  "scripts": {
    "start": "node dist/index.js",
    "funstart": "node dist/setup.js && node dist/index.js"
  }

If I run yarn run start --param=foo ; the index.js will receive the --param=foo
If I dun yarn run funstart --param=foo ; the index.js will not receive the --param=foo

Is there some way to pass arguments to multiple commands (without writing a "setupAndRunIndex.js")

thanks!

triaged

Most helpful comment

New to yarn, but surprised to see this basic thing missing !!
More surprisingly this issue is open since Jan 11, 2018 with a few comments only!!

Fyi and assuming good intent, know that this is kinda rude. What you deem a basic thing is maybe less important to us than other things you take for granted, like installing packages right.

And for what it's worth, albeit undocumented at the moment, this feature will be available in the next major (it'll work just like bash, so you'd write foo "$@" && bar "$@").

All 15 comments

Unfortunately no, the extra parameters are simply added at the end of the command.

Bummer... so there is no way to string commands together with parameters? I can't believe I've never run into this before.

I love yarn's with more succinct error messages with yarn run compared to npm run and have recently moved to that for my DevOps but just realized that my use of npm get xxx within my scripts is not mimicked in yarn (ie., to pull off variables of the command line). Although looking here maybe the answer is that with Yarn I'm meant to pick up these variables from stdin on the script receiving the run command, is that correct? The docs don't seem to indicate this at all.

Ha. I guess yarn's implementation is much more what I originally wanted npm's be. Nice. Sorry for jumping in here.

@chadbr you could at least make a JS script to accomplish that if nothing else.

If I dun yarn run funstart --param=foo ; the index.js will not receive the --param=foo

Don't you mean setup.js won't receive the param? It should be equivalent to node dist/setup.js && node dist/index.js --param=foo which passes the param to index.js.

New to yarn, but surprised to see this basic thing missing !!
More surprisingly this issue is open since Jan 11, 2018 with a few comments only!!

Not blaming the community. I will also look into this, but till now I have heard that yarn was created for better functionalities missing in npm. But here I am seeing no difference on this basic requirement.

New to yarn, but surprised to see this basic thing missing !!
More surprisingly this issue is open since Jan 11, 2018 with a few comments only!!

Fyi and assuming good intent, know that this is kinda rude. What you deem a basic thing is maybe less important to us than other things you take for granted, like installing packages right.

And for what it's worth, albeit undocumented at the moment, this feature will be available in the next major (it'll work just like bash, so you'd write foo "$@" && bar "$@").

@arcanis, apologies if I sounded rude on this. & yes the comment was intended for good only. My only concern was no update on the same. Most of the places such things are either closed or properly tagged like backlog, enhancement!

Anyways good to hear that it will be there 鉂わ笍
Keep up the good work馃憤

And for what it's worth, albeit undocumented at the moment, this feature will be available in the next major (it'll work just like bash, so you'd write foo "$@" && bar "$@").

@arcanis This is awesome to hear! Do you have a link to the commit or PR that added this?

Not a commit (this was done at the beginning of the v2, about a year ago), but we've implemented it here:

https://github.com/yarnpkg/berry/tree/master/packages/berry-shell

And for what it's worth, albeit undocumented at the moment, this feature will be available in the next major (it'll work just like bash, so you'd write foo "$@" && bar "$@").

Very cool! When's 1.18 expected to land?

Soon, but the feature mentioned in my post is in the next major, which will only come in a few months (you're welcome to try it now, however 馃槈).

Oops, didn't read that carefully enough. How do I try it out?

The install instructions are here: https://next.yarnpkg.com/getting-started/install

As a workaround, leverage the fact that yarn 1.x runs the command inside a shell and use that shell's function defining syntax. For example:

  "scripts": {
    "old": "node dist/setup.js && node dist/index.js",
    "new": "f() { node dist/setup.js \"$@\" && node dist/index.js \"$@\"}; f"
  }

For this example I know yarn will use /bin/sh, a POSIX shell. So I can use POSIX function definition to wrap the commands, then call that function immediately. Here's what happens:

  1. Yarn appends the arguments
  2. The shell passes those arguments to the function, f
  3. f then passes the entire arguments ("$@") to both commands on each side of the && connector.

Once inside a function, you can slice and dice arguments. As an example:

"meta": "f() { ./node_modules/.bin/meta \"${1:---help}\" --exclude=\"$(yarn -s getroot)\" \"${@:2}\"; }; f" 

That extracts the first argument and puts after the meta command, defaulting to --help if an argument isn't passed. Then at the end, after inserting a static argument, all passed arguments from 2nd position on (${@:2}) are given to the meta command.

Was this page helpful?
0 / 5 - 0 ratings