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

rfc: Command to add a script to package.json #41

Closed
wants to merge 2 commits into from

Conversation

TheJaredWilcurt
Copy link
Contributor

@TheJaredWilcurt TheJaredWilcurt commented Aug 2, 2019

Thing I wish existed. No opinion on the implementation or any of the bikes.

@isaacs
Copy link
Contributor

isaacs commented Aug 4, 2019

In the bikeshedding: I think this should be npm add-script <name> <cmd>, and overwrite with a warning if one already exists. Otherwise, +1.

@ljharb
Copy link
Contributor

ljharb commented Aug 4, 2019

Can it somehow be prevented from being invoked from postinstall scripts? otherwise it seems like a new attack vector.

@isaacs
Copy link
Contributor

isaacs commented Aug 4, 2019

@ljharb That would just add a new script to the package.json of the package being installed? Ie, you could have something like this:

{
  "scripts": {
    "install": "npm add-script postinstall \"something evil\""
  }
}

But that strikes me as no more dangerous than:

  "scripts": {
    "postinstall": "something evil"
  }
}

That is, it's not safe per se, but I'm not sure if I see how it's a new attack vector.

@ljharb
Copy link
Contributor

ljharb commented Aug 4, 2019

I guess it feels like anything you can do with npm is more privileged/trusted than anything you can do without - and adding a new script to the package doing the installing is what I’m concerned about.

@TheJaredWilcurt
Copy link
Contributor Author

TheJaredWilcurt commented Aug 5, 2019

@isaacs @ljharb

Update PR based on feedback. All bikes addressed.

@isaacs
Copy link
Contributor

isaacs commented Aug 19, 2019

More potential bikeshed ideas, which I am very much not pushing for, but want to make sure get logged here.

What if we extend npm run to be able to run arbitrary commands under an arbitrary script name, and then have it respect --save.

npm run start  # runs the scripts.start
npm run start "node server.js"  # runs node server.js, with env.npm_lifecycle_script="start"
npm run start "node server.js" --save  # same as above, but save to script.start

The hazard here is that npm run start <arg> currently runs the thing configured in package.json with the arg attached to the argument list. So it could be a bit of a footgun, and adding a new command might be better:

npm exec node server.js
npm exec --save-script=start node server.js

This would also bring some of the functionality of npx into the fold as a top-level npm command.

The safest and easiest approach is to add an add-script command, as this RFC proposes. The only thing that's a bit strange is that it uses "add" to "edit" an existing script, but I don't think that's too horrible.

@isaacs isaacs closed this in c31c54a Aug 19, 2019
@TheJaredWilcurt
Copy link
Contributor Author

@isaacs I thought the name --set-script may be better than --add-script as it works for creating and replacing. I'm fine with either though

@isaacs
Copy link
Contributor

isaacs commented Aug 20, 2019

Just one npm CLI pedantic note: in npm --xyz is a config, not a command. For example, in npm install foo --save, the --save is a modifier. You could also do npm install foo --no-save or npm config set save=false.

In this case, "set the script" is the action, so it should be a positional argument, not a config flag.

But yes, npm set-script start "node server.js" is better. I'll update the RFC and make a link to this comment in the commit. Good call.

@TheJaredWilcurt
Copy link
Contributor Author

The wording for the warning message could be documented (or bike shed). My proposal was:

Warning: Existing "start" script has been overwritten. Replaced "express" with "nw .".

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

Successfully merging this pull request may close these issues.

3 participants