yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Feature: expose CLI args to process.env.npm_config_* at lifecycle to compatible with npm usage

Open mc-zone opened this issue 6 years ago • 10 comments

Summary

This PR is a small addition to make better compatible about the migrating from NPM to yarn.

While run scripts, NPM expose all configuration parameters into process.env.npm_config_* for convenient use.

For example, I used to install sqlite3 which depends on node-pre-gyp that accepts a binary mirror that can be passed through CLI:

npm install sqlite3 --node_sqlite3_binary_host_mirror=http://npm.taobao.org/mirrors/

It can't work using yarn. Because node-pre-gyp get this option by process.env.npm_config_node_sqlite3_binary_host_mirror:

// support host mirror with npm config `--{module_name}_binary_host_mirror`
// e.g.: https://github.com/node-inspector/v8-profiler/blob/master/package.json#L25
// > npm install v8-profiler --profiler_binary_host_mirror=https://npm.taobao.org/mirrors/node-inspector/
var host = process.env['npm_config_' + opts.module_name + '_binary_host_mirror'] || package_json.binary.host;

So the PR try to support this feature.

Also resolve: #4581

Test plan

Tests added in fixtures/lifecycle-script/ .

mc-zone avatar Nov 07 '18 06:11 mc-zone

File name Previous Size New Size Change
yarn-[version].noarch.rpm 1.1 MB 1.1 MB -1.21 KB (0%)
yarn-[version].js 4.46 MB 4.46 MB 1.29 KB (0%)
yarn-legacy-[version].js 4.65 MB 4.63 MB -15.92 KB (0%)
yarn-v[version].tar.gz 1.11 MB 1.11 MB -900 bytes (0%)
yarn_[version]all.deb 813.46 KB 812.67 KB -812 bytes (0%)

buildsize[bot] avatar Nov 07 '18 06:11 buildsize[bot]

Thanks for this PR!

However, I'm not too fond of this. It can be easily workaround as such:

npm_config_node_sqlite3_binary_host_mirror=http://npm.taobao.org/mirrors/ yarn install

This PR means that we would have to always accept all options from the command line (because who knows, maybe the scripts need them), preventing us from doing a proper validation. That doesn't seem a good compromise.

arcanis avatar Nov 07 '18 12:11 arcanis

However, I'm not too fond of this. It can be easily workaround as such:

npm_config_node_sqlite3_binary_host_mirror=http://npm.taobao.org/mirrors/ yarn install

Yes you can, but it's not a corss platform way. You know there are some difference between Windows / Mac / Linux. In win32 cmd we should use set xxx=xxx. This bings some costs to make other fellows and enviroments work.

mc-zone avatar Nov 07 '18 12:11 mc-zone

Hm, maybe, still I don't think comfortable enabling this for all arguments. Best case, I'd go for something like this:

yarn -c node_sqlite3_binary_host_mirror=http://npm.taobao.org/mirrors/

arcanis avatar Nov 07 '18 22:11 arcanis

@arcanis Thanks for your advice! It seems a good idea. I'd like to change the implementation right away. Just a detail confirm, how to accept multiple arguments in this syntax? if we don't want to handle all of them, should I take them one by one like this?

yarn -c arg1=foo -c arg2=bar --other-flag

mc-zone avatar Nov 08 '18 03:11 mc-zone

Yep, multiple repetitions of the -c flag sound good to me 👍

arcanis avatar Nov 08 '18 08:11 arcanis

Changes:

  • Add CLI option (-c,--cli-config) to receive key=value pairs
  • Parse and save them in Config
  • Extend makeEnv in lifecycle-scripts for expose to npm_config_*

@arcanis There has already to review again, please.

mc-zone avatar Nov 12 '18 03:11 mc-zone

@arcanis Sorry for the bother. Could you please take a review for this? thanks!

mc-zone avatar Nov 23 '18 04:11 mc-zone

Any news on this PR?

Sogl avatar Apr 30 '19 17:04 Sogl

This would be an amazing feature for yarn, especially useful when orchestrating docker commands through package.json scripts. Is there anything I can do to help get this moving again?

mikehenrty avatar May 07 '22 03:05 mikehenrty