Skip to content

Commit 22175b8

Browse files
committed
Remove use of env.npm_config_argv for npm 7
Instead use npm_config_* which works as far back as npm > 0.1.7. It translates command line flags to environment variables, where: --foo becomes npm_config_foo='true' --no-foo becomes npm_config_foo='' --foo=false becomes npm_config_foo=''
1 parent 8acccac commit 22175b8

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

Diff for: rc.js

+10-27
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,32 @@ var detectLibc = require('detect-libc')
44
var napi = require('napi-build-utils')
55

66
var env = process.env
7-
87
var libc = env.LIBC || (detectLibc.isNonGlibcLinux && detectLibc.family) || ''
98

10-
// Get `prebuild-install` arguments that were passed to the `npm` command
11-
if (env.npm_config_argv) {
12-
var npmargs = ['prebuild', 'compile', 'build-from-source', 'debug', 'verbose']
13-
try {
14-
var npmArgv = JSON.parse(env.npm_config_argv).cooked
15-
for (var i = 0; i < npmargs.length; ++i) {
16-
if (npmArgv.indexOf('--' + npmargs[i]) !== -1) {
17-
process.argv.push('--' + npmargs[i])
18-
}
19-
if (npmArgv.indexOf('--no-' + npmargs[i]) !== -1) {
20-
process.argv.push('--no-' + npmargs[i])
21-
}
22-
}
23-
if ((i = npmArgv.indexOf('--download')) !== -1) {
24-
process.argv.push(npmArgv[i], npmArgv[i + 1])
25-
}
26-
} catch (e) { }
27-
}
28-
299
// Get the configuration
3010
module.exports = function (pkg) {
3111
var pkgConf = pkg.config || {}
32-
var sourceBuild = env.npm_config_build_from_source
33-
var buildFromSource = sourceBuild === pkg.name || sourceBuild === 'true'
12+
13+
// TODO: remove compile and prebuild aliases?
14+
var buildFromSource = env.npm_config_build_from_source || env.npm_config_compile
15+
3416
var rc = require('rc')('prebuild-install', {
3517
target: pkgConf.target || env.npm_config_target || process.versions.node,
3618
runtime: pkgConf.runtime || env.npm_config_runtime || 'node',
3719
arch: pkgConf.arch || env.npm_config_arch || process.arch,
3820
libc: libc,
3921
platform: env.npm_config_platform || process.platform,
40-
debug: false,
22+
debug: env.npm_config_debug === 'true',
4123
force: false,
42-
verbose: false,
43-
prebuild: true,
44-
compile: buildFromSource,
24+
verbose: env.npm_config_verbose === 'true',
25+
prebuild: env.npm_config_prebuild !== '',
26+
compile: buildFromSource === pkg.name || buildFromSource === 'true',
4527
path: '.',
4628
proxy: env.npm_config_proxy || env['http_proxy'] || env['HTTP_PROXY'],
4729
'https-proxy': env.npm_config_https_proxy || env['https_proxy'] || env['HTTPS_PROXY'],
4830
'local-address': env.npm_config_local_address,
49-
'tag-prefix': 'v'
31+
'tag-prefix': 'v',
32+
download: env.npm_config_download
5033
}, minimist(process.argv, {
5134
alias: {
5235
target: 't',

Diff for: test/rc-test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ test('npm args are passed on from npm environment into rc', function (t) {
5555
].join(' ')
5656

5757
runRc(t, args, {}, function (rc) {
58-
t.equal(rc['build-from-source'], true, '--build-from-source works')
5958
t.equal(rc.compile, true, 'compile should be true')
6059
t.equal(rc.debug, true, 'debug should be true')
6160
t.equal(rc.verbose, true, 'verbose should be true')
6261
t.equal(rc.download, 'https://foo.bar', 'download is set')
62+
t.equal(rc.prebuild, true, 'prebuild is true')
6363
t.end()
6464
})
6565
})
@@ -140,16 +140,16 @@ function runRc (t, args, env, cb) {
140140
if (err) throw err
141141

142142
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
143-
var cmd = npm + ' run --silent install'
143+
var cmd = npm + ' run install'
144144

145145
env = Object.assign(cleanEnv(process.env), env)
146146

147147
exec(cmd, { env, cwd: tmp }, function (err, stdout, stderr) {
148148
t.error(err, 'no error')
149-
t.equal(stderr.toString().trim(), '', 'no stderr')
149+
t.equal(stderr.trim(), '', 'no stderr')
150150

151151
try {
152-
var result = JSON.parse(stdout.toString())
152+
var result = JSON.parse(stdout.slice(stdout.indexOf('{')))
153153
} catch (e) {
154154
return t.fail(e)
155155
}

0 commit comments

Comments
 (0)