1+ import { readFile , writeFile } from 'node:fs/promises' ;
2+ import { join } from 'node:path' ;
3+ import { getGlobalVariable } from '../../../utils/env' ;
14import { expectFileNotToExist , expectFileToExist } from '../../../utils/fs' ;
25import { getActivePackageManager } from '../../../utils/packages' ;
36import { git , ng } from '../../../utils/process' ;
4- import {
5- createNpmConfigForAuthentication ,
6- setNpmEnvVarsForAuthentication ,
7- } from '../../../utils/registry' ;
7+ import { VALID_TOKEN } from '../../../utils/registry' ;
88
99export default async function ( ) {
1010 // Yarn specific test that tests YARN_ env variables.
@@ -14,16 +14,39 @@ export default async function () {
1414 }
1515 const command = [ 'add' , '@angular/pwa' , '--skip-confirmation' ] ;
1616
17- // Environment variables only
18- await expectFileNotToExist ( 'public/manifest.webmanifest' ) ;
19- setNpmEnvVarsForAuthentication ( false , true ) ;
20- await ng ( ...command ) ;
21- await expectFileToExist ( 'public/manifest.webmanifest' ) ;
22- await git ( 'clean' , '-dxf' ) ;
17+ // Clean up any potential env vars first
18+ delete process . env [ 'NPM_CONFIG_REGISTRY' ] ;
19+ delete process . env [ 'YARN_REGISTRY' ] ;
20+ delete process . env [ 'NPM_CONFIG__AUTH' ] ;
21+ delete process . env [ 'NPM_CONFIG_ALWAYS_AUTH' ] ;
2322
24- // Mix of config file and env vars works
2523 await expectFileNotToExist ( 'public/manifest.webmanifest' ) ;
26- await createNpmConfigForAuthentication ( false , true ) ;
27- await ng ( ...command ) ;
28- await expectFileToExist ( 'public/manifest.webmanifest' ) ;
24+
25+ // Set the registry via YARN_REGISTRY environment variable
26+ const registryUrl = getGlobalVariable ( 'package-secure-registry' ) as string ;
27+ process . env [ 'YARN_REGISTRY' ] = registryUrl ;
28+
29+ // Read the original user config to restore later
30+ const tempRoot = getGlobalVariable ( 'tmp-root' ) as string ;
31+ const userNpmrcPath = join ( tempRoot , '.npmrc' ) ;
32+ const originalUserNpmrc = await readFile ( userNpmrcPath , 'utf8' ) ;
33+
34+ // Write the scoped auth credentials to the user config .npmrc
35+ const registryHost = registryUrl . replace ( / ^ \w + : / , '' ) ;
36+ await writeFile (
37+ userNpmrcPath ,
38+ originalUserNpmrc +
39+ `\n${ registryHost } /:_auth="${ VALID_TOKEN } "` +
40+ `\n${ registryHost } /:always-auth=true` +
41+ `\nalways-auth=true\n` ,
42+ ) ;
43+
44+ try {
45+ await ng ( ...command ) ;
46+ await expectFileToExist ( 'public/manifest.webmanifest' ) ;
47+ } finally {
48+ // Clean up and restore
49+ delete process . env [ 'YARN_REGISTRY' ] ;
50+ await writeFile ( userNpmrcPath , originalUserNpmrc ) ;
51+ }
2952}
0 commit comments