Skip to content

Commit

Permalink
fix: use new env vars from @npmcli/config
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Sep 1, 2021
1 parent 3f514bf commit 205c8c3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
28 changes: 16 additions & 12 deletions bin/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
#!/usr/bin/env node

const { dirname } = require('path')

const copyContent = require('../lib/content/index.js')
const installPackages = require('../lib/install.js')
const patchPackage = require('../lib/package.js')

const main = async () => {
const pkgPath = process.env.npm_package_json
if (!pkgPath) {
throw new Error('This script must be run as an npm lifecycle event')
}
const {
npm_config_global: globalMode,
npm_config_local_prefix: root,
} = process.env

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

const needsAction = await patchPackage(root)
if (needsAction) {
await copyContent(root)
return installPackages(root)
if (!needsAction) {
return
}

await copyContent(root)
return installPackages(root)
}

// we export the promise so it can be awaited in tests
module.exports = main().catch((err) => {
// we export the promise so it can be awaited in tests, coverage is disabled
// for the catch handler because it does so little it's not worth testing
module.exports = main().catch(/* istanbul ignore next */ (err) => {
console.error(err.stack)
process.exitCode = 1
})
49 changes: 30 additions & 19 deletions test/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,32 @@ const patchPackage = require('../lib/package.js')

spawk.preventUnmatched()

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

const _error = console.error
const logs = []
console.error = (...args) => {
logs.push(...args)
}
t.teardown(() => {
process.env.npm_config_global = _env
})

// t.mock instead of require so the cache doesn't interfere
await t.mock('../bin/postinstall.js')
t.equal(process.exitCode, undefined, 'exitCode is unset')
})

t.test('when npm_config_local_prefix is unset, does nothing', async (t) => {
// this is set by virtue of running tests with npm, save it and remove it
const _env = process.env.npm_config_local_prefix
delete process.env.npm_config_local_prefix

t.teardown(() => {
process.env.npm_package_json = _env
console.error = _error
process.exitCode = undefined // yes, really
process.env.npm_config_local_prefix = _env
})

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

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

const _env = process.env.npm_package_json
process.env.npm_package_json = join(root, 'package.json')
const _global = process.env.npm_config_global
const _prefix = process.env.npm_config_local_prefix
delete process.env.npm_config_global
process.env.npm_config_local_prefix = root

t.teardown(() => {
process.env.npm_package_json = _env
process.env.npm_config_global = _global
process.env.npm_config_local_prefix = _prefix
})

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

const _env = process.env.npm_package_json
process.env.npm_package_json = join(root, 'package.json')
const _global = process.env.npm_config_global
const _prefix = process.env.npm_config_local_prefix
delete process.env.npm_config_global
process.env.npm_config_local_prefix = root

t.teardown(() => {
process.env.npm_package_json = _env
process.env.npm_config_global = _global
process.env.npm_config_local_prefix = _prefix
})

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

0 comments on commit 205c8c3

Please # to comment.