88// and the value is a string or array. Otherwise return false.
99const envKey = ( key , val ) => {
1010 return ! / ^ [ / @ _ ] / . test ( key ) &&
11- ( typeof val === 'string' || Array . isArray ( val ) ) &&
12- `npm_config_${ key . replace ( / - / g, '_' ) . toLowerCase ( ) } `
11+ ( typeof envVal ( val ) === 'string' ) &&
12+ `npm_config_${ key . replace ( / - / g, '_' ) . toLowerCase ( ) } `
1313}
1414
15- const envVal = val => Array . isArray ( val ) ? val . join ( '\n\n' ) : val
15+ const envVal = val => Array . isArray ( val ) ? val . map ( v => envVal ( v ) ) . join ( '\n\n' )
16+ : val === null || val === undefined || val === false ? ''
17+ : typeof val === 'object' ? null
18+ : String ( val )
1619
1720const sameConfigValue = ( def , val ) =>
1821 ! Array . isArray ( val ) || ! Array . isArray ( def ) ? def === val
@@ -30,20 +33,48 @@ const sameArrayValue = (def, val) => {
3033 return true
3134}
3235
36+ const setEnv = ( rawKey , rawVal ) => {
37+ const val = envVal ( rawVal )
38+ const key = envKey ( rawKey , val )
39+ if ( key )
40+ process . env [ key ] = val
41+ }
42+
3343const setEnvs = npm => {
34- // The objects in the config.list array are arranged in
35- // a prototype chain, so we can just for/in over the top
36- // of the stack and grab any that don't match the default
37- const { config : { list : [ configs ] } } = npm
44+ // This ensures that all npm config values that are not the defaults are
45+ // shared appropriately with child processes, without false positives.
46+ //
47+ // if the key is the default value,
48+ // if the environ is NOT the default value,
49+ // set the environ
50+ // else skip it, it's fine
51+ // if the key is NOT the default value,
52+ // if the env is setting it, then leave it (already set)
53+ // otherwise, set the env
54+ console . error ( npm . config . list )
55+ const { config : { list : [ cli , env ] } } = npm
56+ const cliSet = new Set ( Object . keys ( cli ) )
57+ const envSet = new Set ( Object . keys ( env ) )
3858 const { defaults } = require ( './defaults.js' )
39- for ( const key in configs ) {
40- const val = configs [ key ]
41- const environ = envKey ( key , val )
42- if ( ! sameConfigValue ( defaults [ key ] , val ) && environ ) {
43- process . env [ environ ] = envVal ( val )
59+ // the configs form a prototype chain, so we can for/in over cli to
60+ // see all the current values, and hasOwnProperty to see if it's
61+ // set therre.
62+ for ( const key in cli ) {
63+ if ( sameConfigValue ( defaults [ key ] , cli [ key ] ) ) {
64+ if ( ! sameConfigValue ( defaults [ key ] , env [ key ] ) ) {
65+ // getting set back to the default in the cli config
66+ setEnv ( key , cli [ key ] )
67+ }
68+ } else {
69+ // config is not the default
70+ if ( ! ( envSet . has ( key ) && ! cliSet . has ( key ) ) ) {
71+ // was not set in the env, so we have to put it there
72+ setEnv ( key , cli [ key ] )
73+ }
4474 }
4575 }
4676
77+ // also set some other common ones.
4778 process . env . npm_execpath = require . main . filename
4879 process . env . npm_node_execpath = process . execPath
4980 process . env . npm_command = npm . command
0 commit comments