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

Add npm_lifecycle_script to env in makeEnv #4330

Merged

Conversation

adityavohra7
Copy link
Contributor

Summary

Fixes #4003.

The motivation behind this PR is to get more parity with npm wrt the process.env variables available when running scripts.

Test plan

Made these exact changes in my globally installed yarn's node_modules. Using the following package.json

{
  "name": "foo",
  "version": "1.0.0",
  "scripts": {
    "test": "env | grep npm_"
  }
}

I ran yarn test to yield:

MacBook-Pro:temp adityavohra7$ yarn test
yarn test v0.27.5
warning package.json: No license field
$ env | grep npm_
npm_config_version_git_tag=true
npm_config_init_license=MIT
npm_config_registry=https://registry.yarnpkg.com
npm_execpath=/usr/local/lib/node_modules/yarn/bin/yarn.js
npm_config_argv={"remain":[],"cooked":["test"],"original":["test"]}
npm_lifecycle_event=test
npm_package_name=foo
npm_package_version=1.0.0
npm_package_scripts_test=env | grep npm_
npm_config_strict_ssl=true
npm_config_save_prefix=^
npm_config_version_git_message=v%s
npm_lifecycle_script=env | grep npm_
npm_config_user_agent=yarn/0.27.5 npm/? node/v8.4.0 darwin x64
npm_config_ignore_scripts=
npm_config_version_git_sign=
npm_config_ignore_optional=
npm_config_init_version=1.0.0
npm_config_version_tag_prefix=v
npm_node_execpath=/usr/local/bin/node
Done in 0.15s.

npm_lifecycle_script now appears in process.env.

@yarnpkg yarnpkg deleted a comment from buildsize bot Sep 7, 2017
@yarnpkg yarnpkg deleted a comment from buildsize bot Sep 7, 2017
const manifest = await config.maybeReadManifest(cwd);
if (manifest) {
if (manifest.scripts && Object.prototype.hasOwnProperty.call(manifest.scripts, stage)) {
env.npm_lifecycle_script = manifest.scripts[stage];
Copy link
Member

Choose a reason for hiding this comment

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

This is already exposed as npm_lifecycle_event on line 50 above. Isn't that enough?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, sorry. I read the original issue now and I understand what this is doing.

Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

LGTM.

@arcanis - any concerns?

@arcanis
Copy link
Member

arcanis commented Sep 7, 2017

Nope, was waiting for the tests to end. Good to merge!

@arcanis arcanis merged commit aeb89c0 into yarnpkg:master Sep 7, 2017
@adityavohra7
Copy link
Contributor Author

Wow thanks for the quick review and edits and merge everyone! 😄

@adityavohra7 adityavohra7 deleted the add_npm_lifecycle_script_to_env branch September 7, 2017 13:47
@palmerj3
Copy link

palmerj3 commented Sep 7, 2017

So I was using process.env.npm_lifecycle_script to determine if it was yarn or npm running the preinstall script. I would use this to subtly educate team members to use Yarn over NPM.

Is there another way I could accomplish this?
@BYK

@adityavohra7
Copy link
Contributor Author

adityavohra7 commented Sep 7, 2017

@palmerj3, maybe npm_config_user_agent or npm_execpath? This SO post seems to suggest the latter.

@palmerj3
Copy link

palmerj3 commented Sep 7, 2017

@adityavohra7 cool thanks! Hope my comment didn't seem like I was putting down this PR - you did the right thing :) The link you sent seems like a more proper way to do what i'm looking to do.

@BYK BYK mentioned this pull request Sep 8, 2017
joaolucasl pushed a commit to joaolucasl/yarn that referenced this pull request Oct 27, 2017
* Add npm_lifecycle_script to env in makeEnv

* Update execute-lifecycle-script.js
caass pushed a commit to cloudflare/workers-sdk that referenced this pull request Jul 7, 2022
The environment variable `npm_config_user_agent` can be used to guess the package manager
that was used to execute wrangler. It's imperfect (just like regular user agent sniffing!)
but the package managers we support all set this property:

- [npm](https://github.com/npm/cli/blob/1415b4bdeeaabb6e0ba12b6b1b0cc56502bd64ab/lib/utils/config/definitions.js#L1945-L1979)
- [pnpm](https://github.com/pnpm/pnpm/blob/cd4f9341e966eb8b411462b48ff0c0612e0a51a7/packages/plugin-commands-script-runners/src/makeEnv.ts#L14)
- [yarn](https://yarnpkg.com/advanced/lifecycle-scripts#environment-variables)
- [yarn classic](yarnpkg/yarn#4330)
caass pushed a commit to cloudflare/workers-sdk that referenced this pull request Jul 8, 2022
The environment variable `npm_config_user_agent` can be used to guess the package manager
that was used to execute wrangler. It's imperfect (just like regular user agent sniffing!)
but the package managers we support all set this property:

- [npm](https://github.com/npm/cli/blob/1415b4bdeeaabb6e0ba12b6b1b0cc56502bd64ab/lib/utils/config/definitions.js#L1945-L1979)
- [pnpm](https://github.com/pnpm/pnpm/blob/cd4f9341e966eb8b411462b48ff0c0612e0a51a7/packages/plugin-commands-script-runners/src/makeEnv.ts#L14)
- [yarn](https://yarnpkg.com/advanced/lifecycle-scripts#environment-variables)
- [yarn classic](yarnpkg/yarn#4330)
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.

Missing process.env.npm_lifecycle_script
4 participants