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

Isaacs/lifecycle envs #999

Merged
merged 10 commits into from
Mar 20, 2020
Merged

Isaacs/lifecycle envs #999

merged 10 commits into from
Mar 20, 2020

Conversation

isaacs
Copy link
Contributor

@isaacs isaacs commented Mar 10, 2020

Use @npmcli/run-script for the run-script command, and add a few missing configs to flatOptions.

@isaacs isaacs requested a review from a team as a code owner March 10, 2020 20:36
@isaacs isaacs changed the base branch from latest to release/v7.0.0-beta March 10, 2020 20:37
@mikemimik mikemimik added this to the OSS - Sprint 6 milestone Mar 10, 2020
@mikemimik mikemimik added Enhancement new feature or improvement Release 7.x work is associated with a specific npm 7 release semver:major backwards-incompatible breaking changes labels Mar 10, 2020
Copy link
Contributor

@mikemimik mikemimik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple things I found:

  • small nits about using regexp
  • few questions around config we're setting in lib/config/flat-options.js
  • out of file scope cleanup in lib/npm.js
  • logical questions in lib/run-script.js

}
}
cmds = [cmd]
const allScripts = scripts ? Object.keys(scripts) : {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If scripts is truthy then you get an array of strings, if scripts is falsy you get an empty object. Seems like this should be the same value.

On line 146 we are using a for...of statement which is meant for array's and not objects (it will throw if it gets an object)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, this is an oversight.

const prefix = ' '
const cmds = []
const runScripts = []
for (const script of allScripts) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be a problem (from comment above)

const cmds = []
const runScripts = []
for (const script of allScripts) {
const list = cmdList.includes(script) ? cmds : runScripts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both cmds and runScripts start as an empty array. I'm not sure why we need to check if cmdList has the script in it, if in both cases we return an empty array.

for (const script of runScripts) {
output(prefix + script + indent + scripts[script])
}
return allScripts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conditional if logic above is a little confusing. I now realise the point of the cmdList.includes() is to find out if what you typed as a user was, for example, npm run test. That output should be different than if you ran, for example, npm run myscript.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for listing out scripts that are runnable as a command differently from those that have to be run with npm run.

Eg:

$ node . run
Lifecycle scripts included in npm:
  preversion
    bash scripts/update-authors.sh && git add AUTHORS && git commit -m "update AUTHORS" || true
  test
    npm run test-tap --
  posttest
    npm run lint

available via `npm run-script`:
  dumpconf
    env | grep npm | sort | uniq
  prepare
    node bin/npm-cli.js rebuild && node bin/npm-cli.js --no-audit --no-timing prune --prefix=. --no-global && rimraf test/*/*/node_modules && make -j4 mandocs
  ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand. I guess the distinction wasn't clear when reading through the code. I'm not sure if that's indicative of me needing more context or the code not being clear enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you run npm run without any args, it calls the list function, which dumps a list of which scripts are available.

The conditional on 147 is deciding which list to push the command name into, and then we loop over both lists to print out which ones are command lifecycles, and which have to be run explicitly.

@@ -159,7 +163,7 @@ const flatOptions = npm => npm.flatOptions || Object.freeze({
packageLockOnly: npm.config.get('package-lock-only'),
globalStyle: npm.config.get('global-style'),
legacyBundling: npm.config.get('legacy-bundling'),
scriptShell: npm.config.get('script-shell'),
scriptShell: npm.config.get('script-shell') || undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: What's the default returned value of npm.config.get('...') which doesn't find the key ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be literally anything.

$ npm config get script-shell --script-shell=420lol
420lol

But @npmcli/run-script only allows Boolean or undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, only allows strings. (Sorry, was thinkign of another flag.)

So this is the case that would be a problem:

npm run foo --no-script-shell

And the effect of setting that false value to undefined is to fall back to the system default (/bin/sh or cmd.exe).

@@ -57,7 +60,8 @@ const flatOptions = npm => npm.flatOptions || Object.freeze({
localPrefix: npm.localPrefix,
global: npm.config.get('global'),

metricsRegistry: npm.config.get('metrics-registry'),
metricsRegistry: npm.config.get('metrics-registry') ||
npm.config.get('registry'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Are these the same thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless specified explicitly, metrics-registry defaults to the public registry.

lib/config/set-envs.js Show resolved Hide resolved
lib/config/set-envs.js Show resolved Hide resolved
lib/config/set-envs.js Show resolved Hide resolved
@@ -113,19 +113,20 @@
'See the README.md or bin/npm-cli.js for example usage.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 106: var registryRefer is defined by never used
Line 95: var littleGuys is a reference to incorrect spelling, but lib/config/cmd-list.js already has an affordances section; shouldn't we be using that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, registryRefer should be removed, since we're not sending the referer header any more.

Istr there's a difference between "affordances" and "littleGuys", but I don't recall it offhand. Will dig in and sort that out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, no difference. Just apparently something that got overlooked 4 years ago when it stopped being relevant.

isaacs added 10 commits March 17, 2020 10:00
This sets environment variables for npm_command, npm_execpath,
npm_node_execpath, NODE_OPTIONS, and any non-default configs that do not
start with @, /, or _ (as those are potentially sensitive, or at least
not useful).

This is in preparation for the move from npm-lifecycle to
@npmcli/run-script for running lifecycle scripts.
Setting up for the switch from npm-lifecycle to @npmcli/run-script.
The 'littleGuys' (ie, isntall, verison, udpate) are included in the
cmdList.affordances section, so are no longer printed in the default
help banner anyway.  We can remove this variable.
@isaacs isaacs force-pushed the isaacs/lifecycle-envs branch from 251b751 to 9cf75ed Compare March 17, 2020 17:00
@isaacs isaacs merged commit 9cf75ed into release/v7.0.0-beta Mar 20, 2020
@isaacs
Copy link
Contributor Author

isaacs commented Mar 20, 2020

Landed as part of #1021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement new feature or improvement Release 7.x work is associated with a specific npm 7 release semver:major backwards-incompatible breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants