@@ -22,12 +22,13 @@ export interface ILogger {
2222// create a global _combinedNpmrc for cache purpose
2323const _combinedNpmrcMap : Map < string , string > = new Map ( ) ;
2424
25- function _trimNpmrcFile ( options : {
26- sourceNpmrcPath : string ;
27- linesToPrepend ?: string [ ] ;
28- linesToAppend ?: string [ ] ;
29- } ) : string {
30- const { sourceNpmrcPath, linesToPrepend, linesToAppend } = options ;
25+ function _trimNpmrcFile (
26+ options : Pick <
27+ INpmrcTrimOptions ,
28+ 'sourceNpmrcPath' | 'linesToAppend' | 'linesToPrepend' | 'supportEnvVarFallbackSyntax'
29+ >
30+ ) : string {
31+ const { sourceNpmrcPath, linesToPrepend, linesToAppend, supportEnvVarFallbackSyntax } = options ;
3132 const combinedNpmrcFromCache : string | undefined = _combinedNpmrcMap . get ( sourceNpmrcPath ) ;
3233 if ( combinedNpmrcFromCache !== undefined ) {
3334 return combinedNpmrcFromCache ;
@@ -48,7 +49,7 @@ function _trimNpmrcFile(options: {
4849
4950 npmrcFileLines = npmrcFileLines . map ( ( line ) => ( line || '' ) . trim ( ) ) ;
5051
51- const resultLines : string [ ] = trimNpmrcFileLines ( npmrcFileLines , process . env ) ;
52+ const resultLines : string [ ] = trimNpmrcFileLines ( npmrcFileLines , process . env , supportEnvVarFallbackSyntax ) ;
5253
5354 const combinedNpmrc : string = resultLines . join ( '\n' ) ;
5455
@@ -58,7 +59,18 @@ function _trimNpmrcFile(options: {
5859 return combinedNpmrc ;
5960}
6061
61- export function trimNpmrcFileLines ( npmrcFileLines : string [ ] , env : NodeJS . ProcessEnv ) : string [ ] {
62+ /**
63+ *
64+ * @param npmrcFileLines The npmrc file's lines
65+ * @param env The environment variables object
66+ * @param supportEnvVarFallbackSyntax Whether to support fallback values in the form of `${VAR_NAME:-fallback}`
67+ * @returns
68+ */
69+ export function trimNpmrcFileLines (
70+ npmrcFileLines : string [ ] ,
71+ env : NodeJS . ProcessEnv ,
72+ supportEnvVarFallbackSyntax : boolean
73+ ) : string [ ] {
6274 const resultLines : string [ ] = [ ] ;
6375
6476 // This finds environment variable tokens that look like "${VAR_NAME}"
@@ -91,18 +103,24 @@ export function trimNpmrcFileLines(npmrcFileLines: string[], env: NodeJS.Process
91103 */
92104 const nameWithFallback : string = token . substring ( 2 , token . length - 1 ) ;
93105
94- /**
95- * Get the environment variable name and fallback value.
96- *
97- * name fallback
98- * nameString -> nameString undefined
99- * nameString-fallbackString -> nameString fallbackString
100- * nameString:-fallbackString -> nameString fallbackString
101- */
102- const matched : string [ ] | null = nameWithFallback . match ( / ^ ( [ ^ : - ] + ) (?: \: ? - ( .+ ) ) ? $ / ) ;
103- // matched: [originStr, variableName, fallback]
104- const environmentVariableName : string = matched ?. [ 1 ] ?? nameWithFallback ;
105- const fallback : string | undefined = matched ?. [ 2 ] ;
106+ let environmentVariableName : string ;
107+ let fallback : string | undefined ;
108+ if ( supportEnvVarFallbackSyntax ) {
109+ /**
110+ * Get the environment variable name and fallback value.
111+ *
112+ * name fallback
113+ * nameString -> nameString undefined
114+ * nameString-fallbackString -> nameString fallbackString
115+ * nameString:-fallbackString -> nameString fallbackString
116+ */
117+ const matched : string [ ] | null = nameWithFallback . match ( / ^ ( [ ^ : - ] + ) (?: \: ? - ( .+ ) ) ? $ / ) ;
118+ // matched: [originStr, variableName, fallback]
119+ environmentVariableName = matched ?. [ 1 ] ?? nameWithFallback ;
120+ fallback = matched ?. [ 2 ] ;
121+ } else {
122+ environmentVariableName = nameWithFallback ;
123+ }
106124
107125 // Is the environment variable and fallback value defined.
108126 if ( ! env [ environmentVariableName ] && ! fallback ) {
@@ -146,18 +164,15 @@ interface INpmrcTrimOptions {
146164 logger : ILogger ;
147165 linesToPrepend ?: string [ ] ;
148166 linesToAppend ?: string [ ] ;
167+ supportEnvVarFallbackSyntax : boolean ;
149168}
150169
151170function _copyAndTrimNpmrcFile ( options : INpmrcTrimOptions ) : string {
152- const { logger, sourceNpmrcPath, targetNpmrcPath, linesToPrepend , linesToAppend } = options ;
171+ const { logger, sourceNpmrcPath, targetNpmrcPath } = options ;
153172 logger . info ( `Transforming ${ sourceNpmrcPath } ` ) ; // Verbose
154173 logger . info ( ` --> "${ targetNpmrcPath } "` ) ;
155174
156- const combinedNpmrc : string = _trimNpmrcFile ( {
157- sourceNpmrcPath,
158- linesToPrepend,
159- linesToAppend
160- } ) ;
175+ const combinedNpmrc : string = _trimNpmrcFile ( options ) ;
161176
162177 fs . writeFileSync ( targetNpmrcPath , combinedNpmrc ) ;
163178
@@ -176,12 +191,14 @@ function _copyAndTrimNpmrcFile(options: INpmrcTrimOptions): string {
176191export interface ISyncNpmrcOptions {
177192 sourceNpmrcFolder : string ;
178193 targetNpmrcFolder : string ;
194+ supportEnvVarFallbackSyntax : boolean ;
179195 useNpmrcPublish ?: boolean ;
180196 logger ?: ILogger ;
181197 linesToPrepend ?: string [ ] ;
182198 linesToAppend ?: string [ ] ;
183199 createIfMissing ?: boolean ;
184200}
201+
185202export function syncNpmrc ( options : ISyncNpmrcOptions ) : string | undefined {
186203 const {
187204 sourceNpmrcFolder,
@@ -193,9 +210,7 @@ export function syncNpmrc(options: ISyncNpmrcOptions): string | undefined {
193210 // eslint-disable-next-line no-console
194211 error : console . error
195212 } ,
196- createIfMissing = false ,
197- linesToAppend,
198- linesToPrepend
213+ createIfMissing = false
199214 } = options ;
200215 const sourceNpmrcPath : string = path . join (
201216 sourceNpmrcFolder ,
@@ -213,8 +228,7 @@ export function syncNpmrc(options: ISyncNpmrcOptions): string | undefined {
213228 sourceNpmrcPath,
214229 targetNpmrcPath,
215230 logger,
216- linesToAppend,
217- linesToPrepend
231+ ...options
218232 } ) ;
219233 } else if ( fs . existsSync ( targetNpmrcPath ) ) {
220234 // If the source .npmrc doesn't exist and there is one in the target, delete the one in the target
@@ -226,15 +240,19 @@ export function syncNpmrc(options: ISyncNpmrcOptions): string | undefined {
226240 }
227241}
228242
229- export function isVariableSetInNpmrcFile ( sourceNpmrcFolder : string , variableKey : string ) : boolean {
243+ export function isVariableSetInNpmrcFile (
244+ sourceNpmrcFolder : string ,
245+ variableKey : string ,
246+ supportEnvVarFallbackSyntax : boolean
247+ ) : boolean {
230248 const sourceNpmrcPath : string = `${ sourceNpmrcFolder } /.npmrc` ;
231249
232250 //if .npmrc file does not exist, return false directly
233251 if ( ! fs . existsSync ( sourceNpmrcPath ) ) {
234252 return false ;
235253 }
236254
237- const trimmedNpmrcFile : string = _trimNpmrcFile ( { sourceNpmrcPath } ) ;
255+ const trimmedNpmrcFile : string = _trimNpmrcFile ( { sourceNpmrcPath, supportEnvVarFallbackSyntax } ) ;
238256
239257 const variableKeyRegExp : RegExp = new RegExp ( `^${ variableKey } =` , 'm' ) ;
240258 return trimmedNpmrcFile . match ( variableKeyRegExp ) !== null ;
0 commit comments