@@ -57,18 +57,20 @@ export interface PackageMetadata {
57
57
'dist-tags' ?: unknown ;
58
58
}
59
59
60
- let npmrc : { [ key : string ] : string } ;
60
+ type PackageManagerOptions = Record < string , unknown > ;
61
+
62
+ let npmrc : PackageManagerOptions ;
61
63
62
64
function ensureNpmrc ( logger : logging . LoggerApi , usingYarn : boolean , verbose : boolean ) : void {
63
65
if ( ! npmrc ) {
64
66
try {
65
67
npmrc = readOptions ( logger , false , verbose ) ;
66
- } catch { }
68
+ } catch { }
67
69
68
70
if ( usingYarn ) {
69
71
try {
70
72
npmrc = { ...npmrc , ...readOptions ( logger , true , verbose ) } ;
71
- } catch { }
73
+ } catch { }
72
74
}
73
75
}
74
76
}
@@ -77,7 +79,7 @@ function readOptions(
77
79
logger : logging . LoggerApi ,
78
80
yarn = false ,
79
81
showPotentials = false ,
80
- ) : Record < string , string > {
82
+ ) : PackageManagerOptions {
81
83
const cwd = process . cwd ( ) ;
82
84
const baseFilename = yarn ? 'yarnrc' : 'npmrc' ;
83
85
const dotFilename = '.' + baseFilename ;
@@ -107,25 +109,48 @@ function readOptions(
107
109
logger . info ( `Locating potential ${ baseFilename } files:` ) ;
108
110
}
109
111
110
- let options : { [ key : string ] : string } = { } ;
112
+ const options : PackageManagerOptions = { } ;
111
113
for ( const location of [ ...defaultConfigLocations , ...projectConfigLocations ] ) {
112
114
if ( existsSync ( location ) ) {
113
115
if ( showPotentials ) {
114
116
logger . info ( `Trying '${ location } '...found.` ) ;
115
117
}
116
118
117
119
const data = readFileSync ( location , 'utf8' ) ;
118
- options = {
119
- ...options ,
120
- ...( yarn ? lockfile . parse ( data ) : ini . parse ( data ) ) ,
121
- } ;
122
-
123
- if ( options . cafile ) {
124
- const cafile = path . resolve ( path . dirname ( location ) , options . cafile ) ;
125
- delete options . cafile ;
126
- try {
127
- options . ca = readFileSync ( cafile , 'utf8' ) . replace ( / \r ? \n / , '\\n' ) ;
128
- } catch { }
120
+ // Normalize RC options that are needed by 'npm-registry-fetch'.
121
+ // See: https://github.com/npm/npm-registry-fetch/blob/ebddbe78a5f67118c1f7af2e02c8a22bcaf9e850/index.js#L99-L126
122
+ const rcConfig : PackageManagerOptions = yarn ? lockfile . parse ( data ) : ini . parse ( data ) ;
123
+ for ( const [ key , value ] of Object . entries ( rcConfig ) ) {
124
+ switch ( key ) {
125
+ case 'noproxy' :
126
+ case 'no-proxy' :
127
+ options [ 'noProxy' ] = value ;
128
+ break ;
129
+ case 'maxsockets' :
130
+ options [ 'maxSockets' ] = value ;
131
+ break ;
132
+ case 'https-proxy' :
133
+ case 'proxy' :
134
+ options [ 'proxy' ] = value ;
135
+ break ;
136
+ case 'strict-ssl' :
137
+ options [ 'strictSSL' ] = value ;
138
+ break ;
139
+ case 'local-address' :
140
+ options [ 'localAddress' ] = value ;
141
+ break ;
142
+ case 'cafile' :
143
+ if ( typeof value === 'string' ) {
144
+ const cafile = path . resolve ( path . dirname ( location ) , value ) ;
145
+ try {
146
+ options [ 'ca' ] = readFileSync ( cafile , 'utf8' ) . replace ( / \r ? \n / , '\\n' ) ;
147
+ } catch { }
148
+ }
149
+ break ;
150
+ default :
151
+ options [ key ] = value ;
152
+ break ;
153
+ }
129
154
}
130
155
} else if ( showPotentials ) {
131
156
logger . info ( `Trying '${ location } '...not found.` ) ;
@@ -134,8 +159,9 @@ function readOptions(
134
159
135
160
// Substitute any environment variable references
136
161
for ( const key in options ) {
137
- if ( typeof options [ key ] === 'string' ) {
138
- options [ key ] = options [ key ] . replace ( / \$ \{ ( [ ^ \} ] + ) \} / , ( _ , name ) => process . env [ name ] || '' ) ;
162
+ const value = options [ key ] ;
163
+ if ( typeof value === 'string' ) {
164
+ options [ key ] = value . replace ( / \$ \{ ( [ ^ \} ] + ) \} / , ( _ , name ) => process . env [ name ] || '' ) ;
139
165
}
140
166
}
141
167
0 commit comments