-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: Do replace environment in config files even for keys (#18426)
as additional replacement for values in .npmrc for example PR-URL: npm/npm#18426 Credit: @misak113 Reviewed-By: @zkat Reviewed-By: @iarna
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const fs = require('fs') | ||
const mkdirp = require('mkdirp') | ||
const rimraf = require('rimraf') | ||
const path = require('path') | ||
const ini = require('ini') | ||
const test = require('tap').test | ||
const npmconf = require('../../lib/config/core.js') | ||
|
||
const packagePath = path.resolve(__dirname, 'config-envReplace') | ||
|
||
const packageJsonFile = JSON.stringify({ | ||
name: 'config-envReplace' | ||
}) | ||
|
||
const inputConfigFile = [ | ||
'registry=${NPM_REGISTRY_URL}', | ||
'//${NPM_REGISTRY_HOST}/:_authToken=${NPM_AUTH_TOKEN}', | ||
'always-auth=true', | ||
'' | ||
].join('\n') | ||
|
||
const expectConfigFile = [ | ||
'registry=http://my.registry.com/', | ||
'//my.registry.com/:_authToken=xxxxxxxxxxxxxxx', | ||
'always-auth=true', | ||
'' | ||
].join('\n') | ||
|
||
test('environment variables replacing in configs', function (t) { | ||
process.env = Object.assign(process.env, { | ||
NPM_REGISTRY_URL: 'http://my.registry.com/', | ||
NPM_REGISTRY_HOST: 'my.registry.com', | ||
NPM_AUTH_TOKEN: 'xxxxxxxxxxxxxxx' | ||
}) | ||
mkdirp.sync(packagePath) | ||
const packageJsonPath = path.resolve(packagePath, 'package.json') | ||
const configPath = path.resolve(packagePath, '.npmrc') | ||
fs.writeFileSync(packageJsonPath, packageJsonFile) | ||
fs.writeFileSync(configPath, inputConfigFile) | ||
|
||
const originalCwdPath = process.cwd() | ||
process.chdir(packagePath) | ||
npmconf.load(function (error, conf) { | ||
if (error) throw error | ||
|
||
const foundConfigFile = ini.stringify(conf.sources.project.data) | ||
t.same(ini.parse(foundConfigFile), ini.parse(expectConfigFile)) | ||
|
||
fs.unlinkSync(packageJsonPath) | ||
fs.unlinkSync(configPath) | ||
rimraf.sync(packagePath) | ||
process.chdir(originalCwdPath) | ||
t.end() | ||
}) | ||
}) |