Skip to content

Commit 205c8c3

Browse files
committed
fix: use new env vars from @npmcli/config
1 parent 3f514bf commit 205c8c3

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

bin/postinstall.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
#!/usr/bin/env node
22

3-
const { dirname } = require('path')
4-
53
const copyContent = require('../lib/content/index.js')
64
const installPackages = require('../lib/install.js')
75
const patchPackage = require('../lib/package.js')
86

97
const main = async () => {
10-
const pkgPath = process.env.npm_package_json
11-
if (!pkgPath) {
12-
throw new Error('This script must be run as an npm lifecycle event')
13-
}
8+
const {
9+
npm_config_global: globalMode,
10+
npm_config_local_prefix: root,
11+
} = process.env
1412

15-
const root = dirname(pkgPath)
13+
// do nothing in global mode or when the local prefix isn't set
14+
if (globalMode === 'true' || !root) {
15+
return
16+
}
1617

1718
const needsAction = await patchPackage(root)
18-
if (needsAction) {
19-
await copyContent(root)
20-
return installPackages(root)
19+
if (!needsAction) {
20+
return
2121
}
22+
23+
await copyContent(root)
24+
return installPackages(root)
2225
}
2326

24-
// we export the promise so it can be awaited in tests
25-
module.exports = main().catch((err) => {
27+
// we export the promise so it can be awaited in tests, coverage is disabled
28+
// for the catch handler because it does so little it's not worth testing
29+
module.exports = main().catch(/* istanbul ignore next */ (err) => {
2630
console.error(err.stack)
2731
process.exitCode = 1
2832
})

test/postinstall.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,32 @@ const patchPackage = require('../lib/package.js')
1111

1212
spawk.preventUnmatched()
1313

14-
t.test('when npm_package_json is unset logs stack and sets exitCode', async (t) => {
14+
t.test('when npm_config_global is true, does nothing', async (t) => {
1515
// this is set by virtue of running tests with npm, save it and remove it
16-
const _env = process.env.npm_package_json
17-
delete process.env.npm_package_json
16+
const _env = process.env.npm_config_global
17+
delete process.env.npm_config_global
1818

19-
const _error = console.error
20-
const logs = []
21-
console.error = (...args) => {
22-
logs.push(...args)
23-
}
19+
t.teardown(() => {
20+
process.env.npm_config_global = _env
21+
})
22+
23+
// t.mock instead of require so the cache doesn't interfere
24+
await t.mock('../bin/postinstall.js')
25+
t.equal(process.exitCode, undefined, 'exitCode is unset')
26+
})
27+
28+
t.test('when npm_config_local_prefix is unset, does nothing', async (t) => {
29+
// this is set by virtue of running tests with npm, save it and remove it
30+
const _env = process.env.npm_config_local_prefix
31+
delete process.env.npm_config_local_prefix
2432

2533
t.teardown(() => {
26-
process.env.npm_package_json = _env
27-
console.error = _error
28-
process.exitCode = undefined // yes, really
34+
process.env.npm_config_local_prefix = _env
2935
})
3036

3137
// t.mock instead of require so the cache doesn't interfere
3238
await t.mock('../bin/postinstall.js')
33-
t.match(logs[0], /must be run/, 'logged the error')
34-
t.equal(process.exitCode, 1, 'set process.exitCode')
39+
t.equal(process.exitCode, undefined, 'exitCode is unset')
3540
})
3641

3742
t.test('when patchPackage returns false no further action is taken', async (t) => {
@@ -47,11 +52,14 @@ t.test('when patchPackage returns false no further action is taken', async (t) =
4752
'package.json': JSON.stringify(pkg, null, 2),
4853
}))
4954

50-
const _env = process.env.npm_package_json
51-
process.env.npm_package_json = join(root, 'package.json')
55+
const _global = process.env.npm_config_global
56+
const _prefix = process.env.npm_config_local_prefix
57+
delete process.env.npm_config_global
58+
process.env.npm_config_local_prefix = root
5259

5360
t.teardown(() => {
54-
process.env.npm_package_json = _env
61+
process.env.npm_config_global = _global
62+
process.env.npm_config_local_prefix = _prefix
5563
})
5664

5765
// t.mock instead of require so the cache doesn't interfere
@@ -76,11 +84,14 @@ t.test('sets up a new project', async (t) => {
7684
'package.json': JSON.stringify(pkg, null, 2),
7785
}))
7886

79-
const _env = process.env.npm_package_json
80-
process.env.npm_package_json = join(root, 'package.json')
87+
const _global = process.env.npm_config_global
88+
const _prefix = process.env.npm_config_local_prefix
89+
delete process.env.npm_config_global
90+
process.env.npm_config_local_prefix = root
8191

8292
t.teardown(() => {
83-
process.env.npm_package_json = _env
93+
process.env.npm_config_global = _global
94+
process.env.npm_config_local_prefix = _prefix
8495
})
8596

8697
const uninstall = spawk.spawn('npm', (args) => {

0 commit comments

Comments
 (0)