Skip to content

Commit

Permalink
fix: get correct env from npm config
Browse files Browse the repository at this point in the history
`npm config set VAR VAL` will inject `npm_config_var=val` environment
variable. This commit will solve this issue

Closes: #24556
  • Loading branch information
abcfy2 committed Nov 24, 2022
1 parent 497c054 commit 5d33f30
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ const util = {
la(is.unemptyString(varName), 'expected environment variable name, not', varName)

const configVarName = `npm_config_${varName}`
const configVarNameLower = configVarName.toLowerCase()
const packageConfigVarName = `npm_package_config_${varName}`

let result
Expand All @@ -545,6 +546,10 @@ const util = {
debug(`Using ${varName} from npm config`)

result = process.env[configVarName]
} else if (process.env.hasOwnProperty(configVarNameLower)) {
debug(`Using ${varName.toLowerCase()} from npm config`)

result = process.env[configVarNameLower]
} else if (process.env.hasOwnProperty(packageConfigVarName)) {
debug(`Using ${varName} from package.json config`)

Expand Down
5 changes: 5 additions & 0 deletions cli/test/lib/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ describe('util', () => {
expect(util.getEnv('CYPRESS_FOO')).to.eql('')
})

it('npm config set should work', () => {
process.env.npm_config_cypress_foo_foo = 'bazz'
expect(util.getEnv('CYPRESS_FOO_FOO')).to.eql('bazz')
})

it('throws on non-string name', () => {
expect(() => {
util.getEnv()
Expand Down

0 comments on commit 5d33f30

Please sign in to comment.