From 205c8c3e7f60a1b68cd7f95fa8dd45b0273dbfbf Mon Sep 17 00:00:00 2001 From: nlf Date: Wed, 1 Sep 2021 10:07:23 -0700 Subject: [PATCH] fix: use new env vars from @npmcli/config --- bin/postinstall.js | 28 +++++++++++++++----------- test/postinstall.js | 49 +++++++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/bin/postinstall.js b/bin/postinstall.js index 3807f294..02bf3632 100755 --- a/bin/postinstall.js +++ b/bin/postinstall.js @@ -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 }) diff --git a/test/postinstall.js b/test/postinstall.js index 0bc67f06..9d91b327 100644 --- a/test/postinstall.js +++ b/test/postinstall.js @@ -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) => { @@ -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 @@ -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) => {