| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9dd0942 commit 4b9a41b
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -131,7 +131,7 @@ function readOptions( | |||
| 131 | 131 | logger.info(`Locating potential ${baseFilename} files:`); | |
| 132 | 132 | } | |
| 133 | 133 | ||
| 134 | - const options: PackageManagerOptions = {}; | ||
| 134 | + let options: PackageManagerOptions = {}; | ||
| 135 | 135 | for (const location of [...defaultConfigLocations, ...projectConfigLocations]) { | |
| 136 | 136 | if (existsSync(location)) { | |
| 137 | 137 | if (showPotentials) { | |
@@ -142,58 +142,84 @@ function readOptions( | |||
| 142 | 142 | // Normalize RC options that are needed by 'npm-registry-fetch'. | |
| 143 | 143 | // See: https://github.com/npm/npm-registry-fetch/blob/ebddbe78a5f67118c1f7af2e02c8a22bcaf9e850/index.js#L99-L126 | |
| 144 | 144 | const rcConfig: PackageManagerOptions = yarn ? lockfile.parse(data) : ini.parse(data); | |
| 145 | - for (const [key, value] of Object.entries(rcConfig)) { | ||
| 146 | - let substitutedValue = value; | ||
| 147 | 145 | ||
| 148 | - // Substitute any environment variable references. | ||
| 149 | - if (typeof value === 'string') { | ||
| 150 | - substitutedValue = value.replace(/\$\{([^\}]+)\}/, (_, name) => process.env[name] || ''); | ||
| 151 | - } | ||
| 146 | + options = normalizeOptions(rcConfig, location); | ||
| 147 | + } | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + for (const [key, value] of Object.entries(process.env)) { | ||
| 151 | + if (!value || !key.toLowerCase().startsWith('npm_config_')) { | ||
| 152 | + continue; | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + const normalizedName = key | ||
| 156 | + .substr(11) | ||
| 157 | + .replace(/(?!^)_/g, '-') // don't replace _ at the start of the key | ||
| 158 | + .toLowerCase(); | ||
| 159 | + options[normalizedName] = value; | ||
| 160 | + } | ||
| 152 | 161 | ||
| 153 | - switch (key) { | ||
| 154 | - // Unless auth options are scope with the registry url it appears that npm-registry-fetch ignores them, | ||
| 155 | - // even though they are documented. | ||
| 156 | - // https://github.com/npm/npm-registry-fetch/blob/8954f61d8d703e5eb7f3d93c9b40488f8b1b62ac/README.md | ||
| 157 | - // https://github.com/npm/npm-registry-fetch/blob/8954f61d8d703e5eb7f3d93c9b40488f8b1b62ac/auth.js#L45-L91 | ||
| 158 | - case '_authToken': | ||
| 159 | - case 'token': | ||
| 160 | - case 'username': | ||
| 161 | - case 'password': | ||
| 162 | - case '_auth': | ||
| 163 | - case 'auth': | ||
| 164 | - options['forceAuth'] ??= {}; | ||
| 165 | - options['forceAuth'][key] = substitutedValue; | ||
| 166 | - break; | ||
| 167 | - case 'noproxy': | ||
| 168 | - case 'no-proxy': | ||
| 169 | - options['noProxy'] = substitutedValue; | ||
| 170 | - break; | ||
| 171 | - case 'maxsockets': | ||
| 172 | - options['maxSockets'] = substitutedValue; | ||
| 173 | - break; | ||
| 174 | - case 'https-proxy': | ||
| 175 | - case 'proxy': | ||
| 176 | - options['proxy'] = substitutedValue; | ||
| 177 | - break; | ||
| 178 | - case 'strict-ssl': | ||
| 179 | - options['strictSSL'] = substitutedValue; | ||
| 180 | - break; | ||
| 181 | - case 'local-address': | ||
| 182 | - options['localAddress'] = substitutedValue; | ||
| 183 | - break; | ||
| 184 | - case 'cafile': | ||
| 185 | - if (typeof substitutedValue === 'string') { | ||
| 186 | - const cafile = path.resolve(path.dirname(location), substitutedValue); | ||
| 187 | - try { | ||
| 188 | - options['ca'] = readFileSync(cafile, 'utf8').replace(/\r?\n/g, '\n'); | ||
| 189 | - } catch {} | ||
| 190 | - } | ||
| 191 | - break; | ||
| 192 | - default: | ||
| 193 | - options[key] = substitutedValue; | ||
| 194 | - break; | ||
| 162 | + options = normalizeOptions(options); | ||
| 163 | + | ||
| 164 | + return options; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + function normalizeOptions( | ||
| 168 | + rawOptions: PackageManagerOptions, | ||
| 169 | + location = process.cwd(), | ||
| 170 | + ): PackageManagerOptions { | ||
| 171 | + const options: PackageManagerOptions = {}; | ||
| 172 | + | ||
| 173 | + for (const [key, value] of Object.entries(rawOptions)) { | ||
| 174 | + let substitutedValue = value; | ||
| 175 | + | ||
| 176 | + // Substitute any environment variable references. | ||
| 177 | + if (typeof value === 'string') { | ||
| 178 | + substitutedValue = value.replace(/\$\{([^\}]+)\}/, (_, name) => process.env[name] || ''); | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + switch (key) { | ||
| 182 | + // Unless auth options are scope with the registry url it appears that npm-registry-fetch ignores them, | ||
| 183 | + // even though they are documented. | ||
| 184 | + // https://github.com/npm/npm-registry-fetch/blob/8954f61d8d703e5eb7f3d93c9b40488f8b1b62ac/README.md | ||
| 185 | + // https://github.com/npm/npm-registry-fetch/blob/8954f61d8d703e5eb7f3d93c9b40488f8b1b62ac/auth.js#L45-L91 | ||
| 186 | + case '_authToken': | ||
| 187 | + case 'token': | ||
| 188 | + case 'username': | ||
| 189 | + case 'password': | ||
| 190 | + case '_auth': | ||
| 191 | + case 'auth': | ||
| 192 | + options['forceAuth'] ??= {}; | ||
| 193 | + options['forceAuth'][key] = substitutedValue; | ||
| 194 | + break; | ||
| 195 | + case 'noproxy': | ||
| 196 | + case 'no-proxy': | ||
| 197 | + options['noProxy'] = substitutedValue; | ||
| 198 | + break; | ||
| 199 | + case 'maxsockets': | ||
| 200 | + options['maxSockets'] = substitutedValue; | ||
| 201 | + break; | ||
| 202 | + case 'https-proxy': | ||
| 203 | + case 'proxy': | ||
| 204 | + options['proxy'] = substitutedValue; | ||
| 205 | + break; | ||
| 206 | + case 'strict-ssl': | ||
| 207 | + options['strictSSL'] = substitutedValue; | ||
| 208 | + break; | ||
| 209 | + case 'local-address': | ||
| 210 | + options['localAddress'] = substitutedValue; | ||
| 211 | + break; | ||
| 212 | + case 'cafile': | ||
| 213 | + if (typeof substitutedValue === 'string') { | ||
| 214 | + const cafile = path.resolve(path.dirname(location), substitutedValue); | ||
| 215 | + try { | ||
| 216 | + options['ca'] = readFileSync(cafile, 'utf8').replace(/\r?\n/g, '\n'); | ||
| 217 | + } catch {} | ||
| 195 | 218 | } | |
| 196 | - } | ||
| 219 | + break; | ||
| 220 | + default: | ||
| 221 | + options[key] = substitutedValue; | ||
| 222 | + break; | ||
| 197 | 223 | } | |
| 198 | 224 | } | |
| 199 | 225 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + import { expectFileNotToExist, expectFileToExist } from '../../../utils/fs'; | ||
| 2 | + import { getActivePackageManager } from '../../../utils/packages'; | ||
| 3 | + import { git, ng } from '../../../utils/process'; | ||
| 4 | + import { | ||
| 5 | + createNpmConfigForAuthentication, | ||
| 6 | + setNpmEnvVarsForAuthentication, | ||
| 7 | + } from '../../../utils/registry'; | ||
| 8 | + | ||
| 9 | + export default async function () { | ||
| 10 | + const packageManager = getActivePackageManager(); | ||
| 11 | + | ||
| 12 | + if (packageManager === 'npm') { | ||
| 13 | + const originalEnvironment = { ...process.env }; | ||
| 14 | + try { | ||
| 15 | + const command = ['add', '@angular/pwa', '--skip-confirmation']; | ||
| 16 | + | ||
| 17 | + // Environment variables only | ||
| 18 | + await expectFileNotToExist('src/manifest.webmanifest'); | ||
| 19 | + setNpmEnvVarsForAuthentication(); | ||
| 20 | + await ng(...command); | ||
| 21 | + await expectFileToExist('src/manifest.webmanifest'); | ||
| 22 | + await git('clean', '-dxf'); | ||
| 23 | + | ||
| 24 | + // Mix of config file and env vars works | ||
| 25 | + await expectFileNotToExist('src/manifest.webmanifest'); | ||
| 26 | + await createNpmConfigForAuthentication(false, true); | ||
| 27 | + await ng(...command); | ||
| 28 | + await expectFileToExist('src/manifest.webmanifest'); | ||
| 29 | + } finally { | ||
| 30 | + process.env = originalEnvironment; | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,15 +11,20 @@ export default async function () { | |||
| 11 | 11 | '.npmrc': 'registry=http://127.0.0.1:9999', | |
| 12 | 12 | }); | |
| 13 | 13 | // The environment variable has priority over the .npmrc | |
| 14 | - const originalRegistryVariable = process.env['NPM_CONFIG_REGISTRY']; | ||
| 15 | - delete process.env['NPM_CONFIG_REGISTRY']; | ||
| 14 | + let originalRegistryVariable; | ||
| 15 | + if (process.env['NPM_CONFIG_REGISTRY']) { | ||
| 16 | + originalRegistryVariable = process.env['NPM_CONFIG_REGISTRY']; | ||
| 17 | + delete process.env['NPM_CONFIG_REGISTRY']; | ||
| 18 | + } | ||
| 16 | 19 | ||
| 17 | 20 | try { | |
| 18 | 21 | await expectToFail(() => ng('add', '@angular/pwa', '--skip-confirmation')); | |
| 19 | 22 | ||
| 20 | 23 | await ng('add', `--registry=${testRegistry}`, '@angular/pwa', '--skip-confirmation'); | |
| 21 | 24 | await expectFileToExist('src/manifest.webmanifest'); | |
| 22 | 25 | } finally { | |
| 23 | - process.env['NPM_CONFIG_REGISTRY'] = originalRegistryVariable; | ||
| 26 | + if (originalRegistryVariable) { | ||
| 27 | + process.env['NPM_CONFIG_REGISTRY'] = originalRegistryVariable; | ||
| 28 | + } | ||
| 24 | 29 | } | |
| 25 | 30 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,8 +5,11 @@ import { expectToFail } from '../../../utils/utils'; | |||
| 5 | 5 | ||
| 6 | 6 | export default async function () { | |
| 7 | 7 | // The environment variable has priority over the .npmrc | |
| 8 | - const originalRegistryVariable = process.env['NPM_CONFIG_REGISTRY']; | ||
| 9 | - delete process.env['NPM_CONFIG_REGISTRY']; | ||
| 8 | + let originalRegistryVariable; | ||
| 9 | + if (process.env['NPM_CONFIG_REGISTRY']) { | ||
| 10 | + originalRegistryVariable = process.env['NPM_CONFIG_REGISTRY']; | ||
| 11 | + delete process.env['NPM_CONFIG_REGISTRY']; | ||
| 12 | + } | ||
| 10 | 13 | ||
| 11 | 14 | try { | |
| 12 | 15 | const command = ['add', '@angular/pwa', '--skip-confirmation']; | |
@@ -32,6 +35,8 @@ export default async function () { | |||
| 32 | 35 | await createNpmConfigForAuthentication(true, true); | |
| 33 | 36 | await expectToFail(() => ng(...command)); | |
| 34 | 37 | } finally { | |
| 35 | - process.env['NPM_CONFIG_REGISTRY'] = originalRegistryVariable; | ||
| 38 | + if (originalRegistryVariable) { | ||
| 39 | + process.env['NPM_CONFIG_REGISTRY'] = originalRegistryVariable; | ||
| 40 | + } | ||
| 36 | 41 | } | |
| 37 | 42 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,8 +4,11 @@ import { expectToFail } from '../../utils/utils'; | |||
| 4 | 4 | ||
| 5 | 5 | export default async function () { | |
| 6 | 6 | // The environment variable has priority over the .npmrc | |
| 7 | - const originalRegistryVariable = process.env['NPM_CONFIG_REGISTRY']; | ||
| 8 | - delete process.env['NPM_CONFIG_REGISTRY']; | ||
| 7 | + let originalRegistryVariable; | ||
| 8 | + if (process.env['NPM_CONFIG_REGISTRY']) { | ||
| 9 | + originalRegistryVariable = process.env['NPM_CONFIG_REGISTRY']; | ||
| 10 | + delete process.env['NPM_CONFIG_REGISTRY']; | ||
| 11 | + } | ||
| 9 | 12 | ||
| 10 | 13 | const worksMessage = 'We analyzed your package.json'; | |
| 11 | 14 | ||
@@ -30,6 +33,8 @@ export default async function () { | |||
| 30 | 33 | await createNpmConfigForAuthentication(true, true); | |
| 31 | 34 | await expectToFail(() => ng('update')); | |
| 32 | 35 | } finally { | |
| 33 | - process.env['NPM_CONFIG_REGISTRY'] = originalRegistryVariable; | ||
| 36 | + if (originalRegistryVariable) { | ||
| 37 | + process.env['NPM_CONFIG_REGISTRY'] = originalRegistryVariable; | ||
| 38 | + } | ||
| 34 | 39 | } | |
| 35 | 40 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,10 @@ export function createNpmRegistry(withAuthentication = false): ChildProcess { | |||
| 19 | 19 | }); | |
| 20 | 20 | } | |
| 21 | 21 | ||
| 22 | + // Token was generated using `echo -n 'testing:s3cret' | openssl base64`. | ||
| 23 | + const VALID_TOKEN = `dGVzdGluZzpzM2NyZXQ=`; | ||
| 24 | + const SECURE_REGISTRY = `//localhost:4876/`; | ||
| 25 | + | ||
| 22 | 26 | export function createNpmConfigForAuthentication( | |
| 23 | 27 | /** | |
| 24 | 28 | * When true, the authentication token will be scoped to the registry URL. | |
@@ -37,9 +41,8 @@ export function createNpmConfigForAuthentication( | |||
| 37 | 41 | /** When true, an incorrect token is used. Use this to validate authentication failures. */ | |
| 38 | 42 | invalidToken = false, | |
| 39 | 43 | ): Promise<void> { | |
| 40 | - // Token was generated using `echo -n 'testing:s3cret' | openssl base64`. | ||
| 41 | - const token = invalidToken ? `invalid=` : `dGVzdGluZzpzM2NyZXQ=`; | ||
| 42 | - const registry = `//localhost:4876/`; | ||
| 44 | + const token = invalidToken ? `invalid=` : VALID_TOKEN; | ||
| 45 | + const registry = SECURE_REGISTRY; | ||
| 43 | 46 | ||
| 44 | 47 | return writeFile( | |
| 45 | 48 | '.npmrc', | |
@@ -54,3 +57,14 @@ export function createNpmConfigForAuthentication( | |||
| 54 | 57 | `, | |
| 55 | 58 | ); | |
| 56 | 59 | } | |
| 60 | + | ||
| 61 | + export function setNpmEnvVarsForAuthentication( | ||
| 62 | + /** When true, an incorrect token is used. Use this to validate authentication failures. */ | ||
| 63 | + invalidToken = false, | ||
| 64 | + ): void { | ||
| 65 | + const token = invalidToken ? `invalid=` : VALID_TOKEN; | ||
| 66 | + const registry = SECURE_REGISTRY; | ||
| 67 | + | ||
| 68 | + process.env['NPM_CONFIG_REGISTRY'] = `http:${registry}`; | ||
| 69 | + process.env['NPM_CONFIG__AUTH'] = token; | ||
| 70 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments