Skip to content

Commit 5538e6c

Browse files
authored
feat: read from config case-insensitively (#3198)
1 parent 1822dff commit 5538e6c

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

lib/node-gyp.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,25 @@ class Gyp extends EventEmitter {
133133
// other tools.
134134
// The `npm_package_config_node_gyp_` prefix will take precedence over
135135
// `npm_config_` keys.
136-
const npmConfigPrefix = 'npm_config_'
137-
const npmPackageConfigPrefix = 'npm_package_config_node_gyp_'
136+
const npmConfigPrefix = /^npm_config_/i
137+
const npmPackageConfigPrefix = /^npm_package_config_node_gyp_/i
138138

139139
const configEnvKeys = Object.keys(process.env)
140-
.filter((k) => k.startsWith(npmConfigPrefix) || k.startsWith(npmPackageConfigPrefix))
140+
.filter((k) => npmConfigPrefix.test(k) || npmPackageConfigPrefix.test(k))
141141
// sort so that npm_package_config_node_gyp_ keys come last and will override
142-
.sort((a) => a.startsWith(npmConfigPrefix) ? -1 : 1)
142+
.sort((a) => npmConfigPrefix.test(a) ? -1 : 1)
143143

144144
for (const key of configEnvKeys) {
145145
// add the user-defined options to the config
146-
const name = key.startsWith(npmConfigPrefix)
147-
? key.substring(npmConfigPrefix.length)
148-
: key.substring(npmPackageConfigPrefix.length)
146+
const name = npmConfigPrefix.test(key)
147+
? key.replace(npmConfigPrefix, '')
148+
: key.replace(npmPackageConfigPrefix, '')
149149
// gyp@741b7f1 enters an infinite loop when it encounters
150150
// zero-length options so ensure those don't get through.
151151
if (name) {
152152
// convert names like force_process_config to force-process-config
153-
this.opts[name.replaceAll('_', '-')] = process.env[key]
153+
// and convert to lowercase
154+
this.opts[name.replaceAll('_', '-').toLowerCase()] = process.env[key]
154155
}
155156
}
156157

test/test-options.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ describe('options', function () {
3030
process.env.npm_config_y = '41'
3131
// Package config should take precedence over npm_config_ keys.
3232
process.env.npm_package_config_node_gyp_y = '42'
33+
// All configs should be case-insensitive.
34+
process.env.NPM_PACKAGE_CONFIG_NODE_GYP_XX = 'value'
35+
process.env.NPM_CONFIG_YY = 'value'
3336
// loglevel does not get added to opts but will change the logger's level.
3437
process.env.npm_config_loglevel = 'silly'
3538

@@ -41,10 +44,12 @@ describe('options', function () {
4144

4245
assert.strictEqual(log.logger.level.id, 'silly')
4346

44-
assert.deepStrictEqual(Object.keys(g.opts).sort(), [...keys, 'argv', 'x', 'y', 'foo'].sort())
47+
assert.deepStrictEqual(Object.keys(g.opts).sort(), [...keys, 'argv', 'x', 'y', 'foo', 'xx', 'yy'].sort())
4548
assert.strictEqual(g.opts['x'], '42')
4649
assert.strictEqual(g.opts['y'], '42')
4750
assert.strictEqual(g.opts['foo'], '42')
51+
assert.strictEqual(g.opts['xx'], 'value')
52+
assert.strictEqual(g.opts['yy'], 'value')
4853
})
4954

5055
it('options with spaces in environment', () => {

0 commit comments

Comments
 (0)