-
Notifications
You must be signed in to change notification settings - Fork 3k
Passing args into run-scripts #5518
Conversation
Why the "--"? Args should defined directly, specially since for default scripts they don't accept parameters: npm test --foo=bar |
I assume to keep compatibility with current syntax, which is |
But for the cases where by default there are no arguments like npm test, it is really ugly and probably leads to error by newbies that would that's an error in the documentation... npm test -- --foo=bar |
since the Benjamins-MacBook-Air:api benjamincoe$ npm test --help
Top hits for "test"
————————————————————————————————————————————————————————————————————————————————
npm help scripts test:8 Where flags such as
But, we gain the ability to pass args to a script with:
|
I smell issues with shell escaping; and what happens if you need to insert arguments in the middle of the shell command? Instead of doing it, I suggest another approach. Bash can substitute scripts: {
test: "mocha $* test/*.js"
} $ npm test -- --grep foo spawn("sh",
["-c", "mocha $* test/*.js", "unused arg for $0", "--grep", "foo"],
conf)
// shell calls "mocha --grep foo test/*.js" This would also allow parametrized scripts like: scripts: {
link: "/bin/ln -s $2 $1"
}
// usage: "npm run link <source> <destination>" And it's much more generic. |
We are not going to support passing args into the middle of the script, sorry. If you really need this, write your test command using literally any of the command line parsers that anyone uses. (Minimist, dashdash, nopt, and commander all support this just fine.) As a matter of fact, this works fine with mocha, since mocha uses commander:
|
The reason for the Where this is going to break is in |
@isaacs, I don't like the design how |
Just want to throw in my I definitely understand the restrictions and reasoning; but we're already going to have to flag this as a breaking change. My two-cents: if we're going to break backwards with some of the flag-parsing, let's Do It Right and break backwards entirely, in such a way that the (really common.) So, on that note: let's figure out which breaking changes would need to be made to support The most obvious breaking change would be for people relying heavily on the Should you accept my arguments, I'd suggest a depreciation period, during which the new argument-passing functionality be only available under (P.S. Obviously, this is controversial. Consider me to be playing 'advocatus diaboli' here.) |
+1 to @ELLIOTTCABLE comment, the ambiguity of |
@isaacs: will we have to include |
I like the approach. As far as I understand correctly for projects using Grunt all that'd be needed would be to specify: "scripts": {
"grunt": "grunt"
} and then run npm run grunt -- task:target Since it's more ugly than simply: grunt task:target one can define an alias: alias gr='npm run grunt --' and write: gr tast:target Then, |
It was not talked about to remove the "run" word and allow to use just |
This will not work in general since |
I know they would collide, but the idea was to make them first class commands, like |
@piranna What exactly do you propose? To special-case EDIT: besides, you'd still need to type |
What I propose (and I believe was in discussion) is to allow all the entries on the I know this is not related with the |
I'm afraid overriding After reading the rationale, I don't get the need for If we'd able to write |
+1, I believe this is the best interface. "Si quieres viajar alrededor del mundo y ser invitado a hablar en un |
@@ -69,13 +69,13 @@ runScript.completion = function (opts, cb) { | |||
|
|||
function runScript (args, cb) { | |||
if (!args.length) return list(cb) | |||
var pkgdir = args.length === 1 ? process.cwd() | |||
: path.resolve(npm.dir, args[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is us removing the npm test foobar
feature where it runs the tests for node_modules/foobar
instead of top level.
This is a reasonably feature to remove and break back compat on.
Just reviewed this, this looks fine. |
@bcoe What needs to happen to get this landed on the 1.5 / 2.0 branch? Anything I can do to help? |
I rebased this on https://github.com/Raynos/npm/tree/run-args tests pass locally ( except that integration test that needs a local couch). Would love to get this in & shipped for 1.5.0. If you need more tests or documentation let me know and I'll jump in and help with that. |
@@ -124,6 +124,11 @@ function run (pkg, wd, cmd, cb) { | |||
} | |||
log.verbose("run-script", cmds) | |||
chain(cmds.map(function (c) { | |||
// pass cli arguments afer -- to script. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "after"
I still think requiring the |
👍. The fact that a tool used requires a specific way of passing arguments is not a proper argument for applying this solution, a tool can be changed etc. Arguments should be about usage scenarios. But if the decision to require |
I just wanted to +1 (:+1: ) the evils of the extra In general well-designed software strives to make its users refer to the manual as infrequently as possible. But with the current extra
Removing the |
Maybe I'm a bit late.
I was looking to create parameterized npm scripts that interpolate args in arbitrary places, not only at the end. I came to a quick&dirty solution that I haven't seen anywhere else so I'm posting it here for future readers. You just need to wrap your script in an immediately invoked function: {
"print": "f(){ echo Hello $1! && echo Bye $2! ;};f",
"test": "npm run print -- world stranger"
} You get: $ npm test -s # -s is to suppress npm messages for this example
Hello world!
Bye stranger! You can also use This of course won't work on Windows unless you're using the Windows Subsystem for Linux (WSL, aka Bash for Windows), but since it is portable I would use this for dead-simple scripts only. Otherwise, you're better set with a separated |
@stefanmaric, great hack! Exactly what I looked for, thank you |
Since v2, NPM scripts allow you to pass arguments through to the command run when you run it: npm/npm#5518
Since v2, NPM scripts allow you to pass arguments through to the command run when you run it: npm/npm#5518
this is the start of an approach for passing arguments into run-scripts that @isaacs and I have been talking about. It allows you to run:
npm run-script start -- --foo=3
The problem is that it breaks the ability to run a script in the context of a package, e.g.,
npm start <package> <package> <package>
,npm run-script start <package>
.We will still have the ability to run commands in the context of another package, with with the syntax:
npm explore <pkg> -- run test
, which seems like a reasonable compromisethoughts
See #3494