Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yarn run pass parameters to multiple commands? #5207

Open
chadbr opened this issue Jan 10, 2018 · 18 comments
Open

yarn run pass parameters to multiple commands? #5207

chadbr opened this issue Jan 10, 2018 · 18 comments
Assignees
Labels

Comments

@chadbr
Copy link

chadbr commented Jan 10, 2018

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!

@ghost ghost assigned arcanis Jan 10, 2018
@ghost ghost added the triaged label Jan 10, 2018
@arcanis
Copy link
Member

arcanis commented Jan 11, 2018

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

@chadbr
Copy link
Author

chadbr commented Jan 11, 2018

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

@yankeeinlondon
Copy link

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.

@yankeeinlondon
Copy link

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

@jedwards1211
Copy link

@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.

@rahulbhooteshwar
Copy link

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.

@arcanis
Copy link
Member

arcanis commented Aug 13, 2019

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 "$@").

@rahulbhooteshwar
Copy link

@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👍

@swrobel
Copy link

swrobel commented Aug 25, 2019

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?

@arcanis
Copy link
Member

arcanis commented Aug 25, 2019

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

@janosh
Copy link

janosh commented Sep 4, 2019

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?

@arcanis
Copy link
Member

arcanis commented Sep 4, 2019

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 😉).

@janosh
Copy link

janosh commented Sep 4, 2019

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

@arcanis
Copy link
Member

arcanis commented Sep 4, 2019

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

@bishopb
Copy link

bishopb commented Sep 10, 2020

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.

@shellscape
Copy link

The reply by @bishopb should just be added to the docs. Outstanding answer, and thank you for sharing.

@cincodenada
Copy link

cincodenada commented Jan 25, 2022

I think this issue can be closed, correct? Yarn 2.x is long out now and seems like it supports the $@ syntax, which won't be added to 1.x, and there's a decent workaround here for 1.x in @bishopb's suggestion.

@ABSanthosh
Copy link

For those who have no idea what @bishopb's answer means or how to use it, here's a simpler version:

Add this to scripts in package.json:

"new": "f() { touch ./src/data/articles/\"$1\"/\"$2\".md; }; f"

and run this:

 yarn new project figma

which is the equivalent of

touch './src/data/articles/project/figma.md'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests