@@ -502,9 +502,8 @@ export class Utilities {
502502 }
503503
504504 /**
505- * As a workaround, syncNpmrc() copies the .npmrc file to the target folder, and also trims
506- * unusable lines from the .npmrc file. If the source .npmrc file not exist, then syncNpmrc()
507- * will delete an .npmrc that is found in the target folder.
505+ * As a workaround, copyAndTrimNpmrcFile() copies the .npmrc file to the target folder, and also trims
506+ * unusable lines from the .npmrc file.
508507 *
509508 * Why are we trimming the .npmrc lines? NPM allows environment variables to be specified in
510509 * the .npmrc file to provide different authentication tokens for different registry.
@@ -513,47 +512,57 @@ export class Utilities {
513512 * we'd prefer to skip that line and continue looking in other places such as the user's
514513 * home directory.
515514 *
515+ * IMPORTANT: THIS CODE SHOULD BE KEPT UP TO DATE WITH _copyNpmrcFile() FROM scripts/install-run.ts
516+ */
517+ public static copyAndTrimNpmrcFile ( sourceNpmrcPath : string , targetNpmrcPath : string ) : void {
518+ console . log ( `Copying ${ sourceNpmrcPath } --> ${ targetNpmrcPath } ` ) ; // Verbose
519+ let npmrcFileLines : string [ ] = FileSystem . readFile ( sourceNpmrcPath ) . split ( '\n' ) ;
520+ npmrcFileLines = npmrcFileLines . map ( ( line ) => ( line || '' ) . trim ( ) ) ;
521+ const resultLines : string [ ] = [ ] ;
522+ // Trim out lines that reference environment variables that aren't defined
523+ for ( const line of npmrcFileLines ) {
524+ // This finds environment variable tokens that look like "${VAR_NAME}"
525+ const regex : RegExp = / \$ \{ ( [ ^ \} ] + ) \} / g;
526+ const environmentVariables : string [ ] | null = line . match ( regex ) ;
527+ let lineShouldBeTrimmed : boolean = false ;
528+ if ( environmentVariables ) {
529+ for ( const token of environmentVariables ) {
530+ // Remove the leading "${" and the trailing "}" from the token
531+ const environmentVariableName : string = token . substring ( 2 , token . length - 1 ) ;
532+ if ( ! process . env [ environmentVariableName ] ) {
533+ lineShouldBeTrimmed = true ;
534+ break ;
535+ }
536+ }
537+ }
538+
539+ if ( lineShouldBeTrimmed ) {
540+ // Example output:
541+ // "; MISSING ENVIRONMENT VARIABLE: //my-registry.com/npm/:_authToken=${MY_AUTH_TOKEN}"
542+ resultLines . push ( '; MISSING ENVIRONMENT VARIABLE: ' + line ) ;
543+ } else {
544+ resultLines . push ( line ) ;
545+ }
546+ }
547+
548+ FileSystem . writeFile ( targetNpmrcPath , resultLines . join ( os . EOL ) ) ;
549+ }
550+
551+ /**
552+ * syncNpmrc() copies the .npmrc file to the target folder, and also trims unusable lines from the .npmrc file.
553+ * If the source .npmrc file not exist, then syncNpmrc() will delete an .npmrc that is found in the target folder.
554+ *
516555 * IMPORTANT: THIS CODE SHOULD BE KEPT UP TO DATE WITH _syncNpmrc() FROM scripts/install-run.ts
517556 */
518- public static syncNpmrc ( sourceNpmrcFolder : string , targetNpmrcFolder : string ) : void {
519- const sourceNpmrcPath : string = path . join ( sourceNpmrcFolder , '.npmrc' ) ;
557+ public static syncNpmrc ( sourceNpmrcFolder : string , targetNpmrcFolder : string , useNpmrcPublish ?: boolean ) : void {
558+ const sourceNpmrcPath : string = path . join ( sourceNpmrcFolder , ! useNpmrcPublish ? '.npmrc' : '.npmrc-publish ') ;
520559 const targetNpmrcPath : string = path . join ( targetNpmrcFolder , '.npmrc' ) ;
521560 try {
522561 if ( FileSystem . exists ( sourceNpmrcPath ) ) {
523- console . log ( `Copying ${ sourceNpmrcPath } --> ${ targetNpmrcPath } ` ) ;
524- let npmrcFileLines : string [ ] = FileSystem . readFile ( sourceNpmrcPath ) . split ( '\n' ) ;
525- npmrcFileLines = npmrcFileLines . map ( ( line ) => ( line || '' ) . trim ( ) ) ;
526- const resultLines : string [ ] = [ ] ;
527- // Trim out lines that reference environment variables that aren't defined
528- for ( const line of npmrcFileLines ) {
529- // This finds environment variable tokens that look like "${VAR_NAME}"
530- const regex : RegExp = / \$ \{ ( [ ^ \} ] + ) \} / g;
531- const environmentVariables : string [ ] | null = line . match ( regex ) ;
532- let lineShouldBeTrimmed : boolean = false ;
533- if ( environmentVariables ) {
534- for ( const token of environmentVariables ) {
535- // Remove the leading "${" and the trailing "}" from the token
536- const environmentVariableName : string = token . substring ( 2 , token . length - 1 ) ;
537- if ( ! process . env [ environmentVariableName ] ) {
538- lineShouldBeTrimmed = true ;
539- break ;
540- }
541- }
542- }
543-
544- if ( lineShouldBeTrimmed ) {
545- // Example output:
546- // "; MISSING ENVIRONMENT VARIABLE: //my-registry.com/npm/:_authToken=${MY_AUTH_TOKEN}"
547- resultLines . push ( '; MISSING ENVIRONMENT VARIABLE: ' + line ) ;
548- } else {
549- resultLines . push ( line ) ;
550- }
551- }
552-
553- FileSystem . writeFile ( targetNpmrcPath , resultLines . join ( os . EOL ) ) ;
562+ Utilities . copyAndTrimNpmrcFile ( sourceNpmrcPath , targetNpmrcPath ) ;
554563 } else if ( FileSystem . exists ( targetNpmrcPath ) ) {
555564 // If the source .npmrc doesn't exist and there is one in the target, delete the one in the target
556- console . log ( `Deleting ${ targetNpmrcPath } ` ) ;
565+ console . log ( `Deleting ${ targetNpmrcPath } ` ) ; // Verbose
557566 FileSystem . deleteFile ( targetNpmrcPath ) ;
558567 }
559568 } catch ( e ) {
0 commit comments