Skip to content
This repository was archived by the owner on Feb 19, 2026. It is now read-only.

Commit 08eaf6d

Browse files
committed
Use env.npm_package_resolved for npm 7
1 parent 22175b8 commit 08eaf6d

File tree

4 files changed

+71
-30
lines changed

4 files changed

+71
-30
lines changed

.travis.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ node_js:
99
- 8
1010
- 10
1111
- 12
12-
- 14
12+
- 14
13+
14+
jobs:
15+
include:
16+
- name: npm 7
17+
os: linux
18+
node_js: 14
19+
before_install: npm i npm@7 -g

log.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var log = require('npmlog')
2+
var fs = require('fs')
3+
var path = require('path')
24

35
module.exports = function (rc, env) {
46
log.heading = 'prebuild-install'
@@ -9,5 +11,15 @@ module.exports = function (rc, env) {
911
log.level = env.npm_config_loglevel || 'notice'
1012
}
1113

14+
// Temporary workaround for npm 7 which swallows our output
15+
if (process.env.npm_config_prebuild_install_logfile) {
16+
var fp = path.resolve(process.env.npm_config_prebuild_install_logfile)
17+
18+
log.on('log', function (msg) {
19+
// Only for tests, don't care about performance
20+
fs.appendFileSync(fp, [log.heading, msg.level, msg.prefix, msg.message].join(' ') + '\n')
21+
})
22+
}
23+
1224
return log
1325
}

test/skip-test.js

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,42 @@ var execFileSync = require('child_process').execFileSync
55
var fs = require('fs')
66
var tempy = require('tempy') // Locked to 0.2.1 for node 6 support
77
var cleanEnv = require('./util/clean-env')
8-
9-
// Old npm (v3?) doesn't support the mechanisms of this test
108
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
11-
var supported = execFileSync(npm, ['-v']).toString().match(/^(\d+)\./)[1] > 3
129

13-
supported && test('skips download in standalone package', function (t) {
14-
run(t, 'standalone', '', function (code, logs) {
15-
t.is(code, 1)
16-
t.is(logs.pop(), 'prebuild-install info install installing standalone, skipping download.')
10+
test('skips download in git dependency', function (t) {
11+
// We're not testing this flag. Just that we do hit the code paths before it
12+
run(t, 'git', '--build-from-source', function (logs) {
13+
t.is(logs.pop(), 'prebuild-install info install installing from git repository, skipping download.')
1714
t.end()
1815
})
1916
})
2017

21-
supported && test('skips download in git dependency', function (t) {
22-
run(t, 'git', '', function (code, logs) {
23-
t.is(code, 1)
24-
t.is(logs.pop(), 'prebuild-install info install installing from git repository, skipping download.')
18+
test('does not skip download in normal dependency', function (t) {
19+
// We're not testing this flag. Just that we don't hit the code paths before it
20+
run(t, 'tarball', '--build-from-source', function (logs) {
21+
t.is(logs.pop(), 'prebuild-install info install --build-from-source specified, not attempting download.')
2522
t.end()
2623
})
2724
})
2825

29-
supported && test('does not skip download in normal dependency', function (t) {
26+
test('does not skip download in standalone package', function (t) {
3027
// We're not testing this flag. Just that we don't hit the code paths before it
31-
run(t, 'tarball', '--build-from-source', function (code, logs) {
32-
t.is(code, 1)
28+
run(t, 'standalone', '--build-from-source', function (logs) {
3329
t.is(logs.pop(), 'prebuild-install info install --build-from-source specified, not attempting download.')
3430
t.end()
3531
})
3632
})
3733

3834
function run (t, mode, args, cb) {
3935
var addon = tempy.directory()
36+
var logfile = path.join(addon, 'prebuild-install.log')
4037
var cwd = addon
4138

4239
writePackage(addon, {
4340
name: 'addon',
4441
version: '1.0.0',
45-
dependencies: {
46-
'prebuild-install': 'file:' + path.dirname(__dirname)
47-
},
4842
scripts: {
49-
// TODO: npm 7 cannot find "prebuild-install" command in tarball mode
50-
install: 'prebuild-install ' + args
43+
install: 'node ' + path.resolve(__dirname, '..', 'bin.js') + ' ' + args + ' || exit 0'
5144
}
5245
})
5346

@@ -66,11 +59,22 @@ function run (t, mode, args, cb) {
6659
var env = Object.assign(cleanEnv(process.env), {
6760
// We shouldn't hit npm or github
6861
npm_config_registry: 'http://localhost:1234',
69-
npm_config_addon_binary_host: 'http://localhost:1234'
62+
npm_config_addon_binary_host: 'http://localhost:1234',
63+
npm_config_prefer_offline: 'true',
64+
npm_config_audit: 'false',
65+
66+
// Temporary workaround for npm 7 which swallows our output
67+
npm_config_prebuild_install_logfile: logfile,
68+
npm_config_loglevel: 'info'
7069
})
7170

72-
exec(npm + ' install --loglevel=info', { cwd, env, encoding: 'utf8' }, function (err, stdout, stderr) {
73-
cb(err && err.code, logs(stderr))
71+
exec(npm + ' install', { cwd, env }, function (err) {
72+
t.ifError(err, 'no install error')
73+
74+
fs.readFile(logfile, 'utf8', function (err, data) {
75+
t.ifError(err, 'no read error')
76+
cb(logs(data))
77+
})
7478
})
7579
}
7680

@@ -85,9 +89,18 @@ function prepareGit (cwd) {
8589
execFileSync('git', ['add', 'package.json'], { cwd, stdio: 'ignore' })
8690
execFileSync('git', ['commit', '-m', 'test'], { cwd, stdio: 'ignore' })
8791

92+
if (process.platform === 'win32' && npmVersion() >= 7) {
93+
// Otherwise results in invalid url error
94+
return 'git+file:///' + cwd
95+
}
96+
8897
return 'git+file://' + cwd
8998
}
9099

100+
function npmVersion () {
101+
return parseInt(execFileSync(npm, ['-v']).toString())
102+
}
103+
91104
function prepareTarball (cwd) {
92105
// Packs to <name>-<version>.tgz
93106
execFileSync(npm, ['pack'], { cwd, stdio: 'ignore' })
@@ -96,9 +109,13 @@ function prepareTarball (cwd) {
96109
}
97110

98111
function logs (stderr) {
99-
return (stderr || '').split(/\r?\n/).filter(isOurs)
112+
return (stderr || '').split(/\r?\n/).filter(isOurs).map(stripPrefix)
100113
}
101114

102115
function isOurs (line) {
103-
return /^prebuild-install /.test(line)
116+
return /^(npm ERR! )?prebuild-install /.test(line)
117+
}
118+
119+
function stripPrefix (line) {
120+
return line.replace(/^npm ERR! /, '')
104121
}

util.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,20 @@ function tempFile (cached) {
8888
}
8989

9090
function packageOrigin (env, pkg) {
91+
// npm <= 6: metadata is stored on disk in node_modules
92+
if (pkg._from) {
93+
return pkg._from
94+
}
95+
96+
// npm 7: metadata is exposed to environment by arborist
9197
if (env.npm_package_from) {
92-
// npm 7: metadata is exposed to environment by arborist
93-
// TODO: seems undefined atm (npm 7.0.2)
98+
// NOTE: seems undefined atm (npm 7.0.2)
9499
return env.npm_package_from
95100
}
96101

97-
if (pkg._from) {
98-
// npm <= 6: metadata is stored on disk in node_modules
99-
return pkg._from
102+
if (env.npm_package_resolved) {
103+
// NOTE: not sure about the difference with _from, but it's all we have
104+
return env.npm_package_resolved
100105
}
101106
}
102107

0 commit comments

Comments
 (0)