@@ -52,9 +52,8 @@ function _parsePackageSpecifier(rawPackageSpecifier: string): IPackageSpecifier
5252}
5353
5454/**
55- * As a workaround, _syncNpmrc() copies the .npmrc file to the target folder, and also trims
56- * unusable lines from the .npmrc file. If the source .npmrc file not exist, then _syncNpmrc()
57- * will delete an .npmrc that is found in the target folder.
55+ * As a workaround, copyAndTrimNpmrcFile() copies the .npmrc file to the target folder, and also trims
56+ * unusable lines from the .npmrc file.
5857 *
5958 * Why are we trimming the .npmrc lines? NPM allows environment variables to be specified in
6059 * the .npmrc file to provide different authentication tokens for different registry.
@@ -63,45 +62,57 @@ function _parsePackageSpecifier(rawPackageSpecifier: string): IPackageSpecifier
6362 * we'd prefer to skip that line and continue looking in other places such as the user's
6463 * home directory.
6564 *
66- * IMPORTANT: THIS CODE SHOULD BE KEPT UP TO DATE WITH Utilities._syncNpmrc()
65+ * IMPORTANT: THIS CODE SHOULD BE KEPT UP TO DATE WITH _copyNpmrcFile() FROM scripts/install-run.ts
6766 */
68- function _syncNpmrc ( sourceNpmrcFolder : string , targetNpmrcFolder : string , useNpmrcPublish ?: boolean ) : void {
69- const sourceNpmrcPath : string = path . join ( sourceNpmrcFolder , ! useNpmrcPublish ? '.npmrc' : '.npmrc-publish' ) ;
70- const targetNpmrcPath : string = path . join ( targetNpmrcFolder , '.npmrc' ) ;
71- try {
72- if ( fs . existsSync ( sourceNpmrcPath ) ) {
73- let npmrcFileLines : string [ ] = fs . readFileSync ( sourceNpmrcPath ) . toString ( ) . split ( '\n' ) ;
74- npmrcFileLines = npmrcFileLines . map ( ( line ) => ( line || '' ) . trim ( ) ) ;
75- const resultLines : string [ ] = [ ] ;
76- // Trim out lines that reference environment variables that aren't defined
77- for ( const line of npmrcFileLines ) {
78- // This finds environment variable tokens that look like "${VAR_NAME}"
79- const regex : RegExp = / \$ \{ ( [ ^ \} ] + ) \} / g;
80- const environmentVariables : string [ ] | null = line . match ( regex ) ;
81- let lineShouldBeTrimmed : boolean = false ;
82- if ( environmentVariables ) {
83- for ( const token of environmentVariables ) {
84- // Remove the leading "${" and the trailing "}" from the token
85- const environmentVariableName : string = token . substring ( 2 , token . length - 1 ) ;
86- if ( ! process . env [ environmentVariableName ] ) {
87- lineShouldBeTrimmed = true ;
88- break ;
89- }
90- }
91- }
92-
93- if ( lineShouldBeTrimmed ) {
94- // Example output:
95- // "; MISSING ENVIRONMENT VARIABLE: //my-registry.com/npm/:_authToken=${MY_AUTH_TOKEN}"
96- resultLines . push ( '; MISSING ENVIRONMENT VARIABLE: ' + line ) ;
97- } else {
98- resultLines . push ( line ) ;
67+ function _copyAndTrimNpmrcFile ( sourceNpmrcPath : string , targetNpmrcPath : string ) : void {
68+ console . log ( `Copying ${ sourceNpmrcPath } --> ${ targetNpmrcPath } ` ) ;
69+ let npmrcFileLines : string [ ] = fs . readFileSync ( sourceNpmrcPath ) . toString ( ) . split ( '\n' ) ;
70+ npmrcFileLines = npmrcFileLines . map ( ( line ) => ( line || '' ) . trim ( ) ) ;
71+ const resultLines : string [ ] = [ ] ;
72+ // Trim out lines that reference environment variables that aren't defined
73+ for ( const line of npmrcFileLines ) {
74+ // This finds environment variable tokens that look like "${VAR_NAME}"
75+ const regex : RegExp = / \$ \{ ( [ ^ \} ] + ) \} / g;
76+ const environmentVariables : string [ ] | null = line . match ( regex ) ;
77+ let lineShouldBeTrimmed : boolean = false ;
78+ if ( environmentVariables ) {
79+ for ( const token of environmentVariables ) {
80+ // Remove the leading "${" and the trailing "}" from the token
81+ const environmentVariableName : string = token . substring ( 2 , token . length - 1 ) ;
82+ if ( ! process . env [ environmentVariableName ] ) {
83+ lineShouldBeTrimmed = true ;
84+ break ;
9985 }
10086 }
87+ }
88+
89+ if ( lineShouldBeTrimmed ) {
90+ // Example output:
91+ // "; MISSING ENVIRONMENT VARIABLE: //my-registry.com/npm/:_authToken=${MY_AUTH_TOKEN}"
92+ resultLines . push ( '; MISSING ENVIRONMENT VARIABLE: ' + line ) ;
93+ } else {
94+ resultLines . push ( line ) ;
95+ }
96+ }
10197
102- fs . writeFileSync ( targetNpmrcPath , resultLines . join ( os . EOL ) ) ;
98+ fs . writeFileSync ( targetNpmrcPath , resultLines . join ( os . EOL ) ) ;
99+ }
100+
101+ /**
102+ * syncNpmrc() copies the .npmrc file to the target folder, and also trims unusable lines from the .npmrc file.
103+ * If the source .npmrc file not exist, then syncNpmrc() will delete an .npmrc that is found in the target folder.
104+ *
105+ * IMPORTANT: THIS CODE SHOULD BE KEPT UP TO DATE WITH _syncNpmrc() FROM scripts/install-run.ts
106+ */
107+ function _syncNpmrc ( sourceNpmrcFolder : string , targetNpmrcFolder : string ) : void {
108+ const sourceNpmrcPath : string = path . join ( sourceNpmrcFolder , '.npmrc' ) ;
109+ const targetNpmrcPath : string = path . join ( targetNpmrcFolder , '.npmrc' ) ;
110+ try {
111+ if ( fs . existsSync ( sourceNpmrcPath ) ) {
112+ _copyAndTrimNpmrcFile ( sourceNpmrcPath , targetNpmrcPath ) ;
103113 } else if ( fs . existsSync ( targetNpmrcPath ) ) {
104114 // If the source .npmrc doesn't exist and there is one in the target, delete the one in the target
115+ console . log ( `Deleting ${ targetNpmrcPath } ` ) ;
105116 fs . unlinkSync ( targetNpmrcPath ) ;
106117 }
107118 } catch ( e ) {
0 commit comments