-
Notifications
You must be signed in to change notification settings - Fork 3k
pass cli args anywhere into an npm run scripts command with possible re-use #9627
Comments
ok, i found a solution. I can wrap whatever i need to pass into an npm script command into a cli tool, that In my particular case i used the
If there is a better way, I'm curious to learn :-) |
I would check out package.json config field: config A "config" object can be used to set configuration parameters used in package scripts that persist across upgrades. For instance, if a package had the following:
and then had a "start" command that then referenced the npm_package_config_port environment variable, then the user could override that by doing npm config set foo:port 8001. See npm-config(7) and npm-scripts(7) for more on package configs. |
I tried to use For now, creating a custom module which console.logs the information i need in order to use it inline in an npm scripts task seems to be the best, but i guess this might not work cross-platform, especially, I have my doubts this would work on windows :/ |
+1 to the request. I use npm scripts to run specific unit test spec. I want to be able to pass the path to test-file as argument. |
how about |
@serapath the Assuming a package.json similar to this:
They can be overridden via:
|
@jasonkarns did you mean |
No. https://docs.npmjs.com/files/package.json#config (And I conflated the two in my examples. the ENV var is for npm_config, not npm_package_config. I have removed the ENV var example.) The rest of my examples are exactly correct (and tested) as written. The "config": {
"foo": {
"bar": "baz"
}
} is available as And the package.json And the npmrc files are overridden from the command line with |
Ah, right, I was actually mentioning the part of custom argument position via cli for running npm scripts, so script |
Gotcha. The npm_config variant is much nicer when overriding via the command line (and via npmrc files). The downside is that one can't set defaults in package.json that way. Personally, I find npm_package_config_* settings preferable for use by npm-scripts (since defaults can be set in package.json itself, and overridden via CLI or npmrc files). And I prefer the npm_config_* variant preferable for variables that will be referenced from within JS itself (via |
According to #3494 we can't use $1 (Unix) or %1 (Windows) I want to be able to pass arguments without having to use Bash or Batch scripts {
"scripts": {
"zip-base": "7za (some zipping commands here) %1",
"zip-osx": "npm run zip-base -- darwin",
"zip-linux": "npm run zip-base -- linux",
"zip-win": "npm run zip-base -- win32",
"zip-all": "npm run zip-osx && npm run zip-linux && npm run zip-win"
}
} Maybe |
try
which logs access with
execute on cli with
|
Thanks, @jasonkarns, that helped with my problem. Another solution is to manually create a script which programmatically spawns the process with the arguments. Eg:
|
@AlexFrazer another option would be to use scripty |
We're closing this support issue as it has gone three days without activity. The npm CLI team itself does not provide support via this issue tracker, but we are happy when users help each other here. In our experience once a support issue goes dormant it's unlikely to get further activity. If you're still having problems, you may be better served by joining package.community and asking your question there. For more information about our new issue aging policies and why we've instituted them please see our blog post. |
This should be better supported by npm, but here is a workaround for arbitrarily positioned arguments in an npm script: package.json file:
which is called like this:
and returns
note that if you may not always need the |
Thank you @refactorized , I was able to get a script that I liked with your advice. Here is what I came up with: {
"scripts": {
"hbd": "bash -c 'echo \"happy birthday ${1-Brian}! and many returns\"' -- ",
}
} The difference is that my script has My npm script needed to provide a default value if no arguments were passed. Using my hbd script without the two dashes at the end resulted in inconsistent results when running the script with and without the dashes. If I didn't include the dashes, the variable would always be "bash", rather than nothing, which means I couldn't supply my own default value. However, by putting the dashes at the end of the npm script itself, my default value logic works. See here for information on the default value logic: https://www.cyberciti.biz/faq/bash-ksh-if-variable-is-not-defined-set-default-variable/ Now, I can run both of these commands: npm run hbd // outputs 'happy birthday Brian! and many returns'
npm run hbd Courtney // outputs 'happy birthday Courtney! and many returns' I wanted to provide my working example in case someone else was in a situation where the command had to be executed in that way. However, I ended up using a different strategy - instead of getting {
"scripts": {
"hbd": "echo \"happy birthday ${NPM_BIRTHDAY_NAME-Brian}! and many returns\"",
}
} Here are what my commands look like now: npm run hbd // outputs 'happy birthday Brian! and many returns'
NPM_BIRTHDAY_NAME=Courtney npm run hbd // outputs 'happy birthday Courtney! and many returns' It makes the command itself heavier, but it's easy enough to understand, and keeps the underlying logic simpler. |
Another way to get positional (numbered) shell variables is to use a function.
first we define a function This technique is also useful for making git "subcommands" via aliases that can accept leverage positional args: |
Unfortunately most mentioned workarounds are bash specific and not cross-platform. |
I found an already closed issue: #3494
I'm using
browserify
and a lot of different transforms and plugins to build my project and start all kinds of dev servers to help me with my work.see: https://github.com/substack/browserify-handbook#plugins
I need to pass in arguments and sometime, they cannot be hard coded, like my current local area network ip adress, e.g.
192.168.2.112
using
-- -u 192.168.2.112
won't help, because it needs to be inside of... -p [ browserify-hmr -u 192.168.2.112 ] ....
What can I do?
The text was updated successfully, but these errors were encountered: