diff options
author | Gar <gar+gh@danger.computer> | 2021-02-25 02:54:50 +0300 |
---|---|---|
committer | Ruy Adorno <ruyadorno@hotmail.com> | 2021-03-05 00:05:08 +0300 |
commit | 4a5dd3a5a200b3f4f7b47168497d8e03dca3a2ca (patch) | |
tree | d34a1ea229b719c3cfbdce85899ceaf67b43e7ab /lib/ci.js | |
parent | b33c760cea7fe2696d35b5530abc1b455980fef1 (diff) |
fix(npm) pass npm context everywhere
Instead of files randomly requiring the npm singleton,
we pass it where it needs to go so that tests don't need
to do so much require mocking everywhere
PR-URL: https://github.com/npm/cli/pull/2772
Credit: @wraithgar
Close: #2772
Reviewed-by: @ruyadorno
Diffstat (limited to 'lib/ci.js')
-rw-r--r-- | lib/ci.js | 108 |
1 files changed, 59 insertions, 49 deletions
@@ -7,13 +7,8 @@ const fs = require('fs') const readdir = util.promisify(fs.readdir) const log = require('npmlog') -const npm = require('./npm.js') const usageUtil = require('./utils/usage.js') -const usage = usageUtil('ci', 'npm ci') - -const cmd = (args, cb) => ci().then(() => cb()).catch(cb) - const removeNodeModules = async where => { const rimrafOpts = { glob: false } process.emit('time', 'npm-ci:rm') @@ -24,55 +19,70 @@ const removeNodeModules = async where => { process.emit('timeEnd', 'npm-ci:rm') } -const ci = async () => { - if (npm.flatOptions.global) { - const err = new Error('`npm ci` does not work for global packages') - err.code = 'ECIGLOBAL' - throw err +class CI { + constructor (npm) { + this.npm = npm + } + + /* istanbul ignore next - see test/lib/load-all-commands.js */ + get usage () { + return usageUtil('ci', 'npm ci') } - const where = npm.prefix - const { scriptShell, ignoreScripts } = npm.flatOptions - const arb = new Arborist({ ...npm.flatOptions, path: where }) + exec (args, cb) { + this.ci().then(() => cb()).catch(cb) + } + + async ci () { + if (this.npm.flatOptions.global) { + const err = new Error('`npm ci` does not work for global packages') + err.code = 'ECIGLOBAL' + throw err + } + + const where = this.npm.prefix + const { scriptShell, ignoreScripts } = this.npm.flatOptions + const arb = new Arborist({ ...this.npm.flatOptions, path: where }) - await Promise.all([ - arb.loadVirtual().catch(er => { - log.verbose('loadVirtual', er.stack) - const msg = - 'The `npm ci` command can only install with an existing package-lock.json or\n' + - 'npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or\n' + - 'later to generate a package-lock.json file, then try again.' - throw new Error(msg) - }), - removeNodeModules(where), - ]) - // npm ci should never modify the lockfile or package.json - await arb.reify({ ...npm.flatOptions, save: false }) + await Promise.all([ + arb.loadVirtual().catch(er => { + log.verbose('loadVirtual', er.stack) + const msg = + 'The `npm ci` command can only install with an existing package-lock.json or\n' + + 'npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or\n' + + 'later to generate a package-lock.json file, then try again.' + throw new Error(msg) + }), + removeNodeModules(where), + ]) + // npm ci should never modify the lockfile or package.json + await arb.reify({ ...this.npm.flatOptions, save: false }) - // run the same set of scripts that `npm install` runs. - if (!ignoreScripts) { - const scripts = [ - 'preinstall', - 'install', - 'postinstall', - 'prepublish', // XXX should we remove this finally?? - 'preprepare', - 'prepare', - 'postprepare', - ] - for (const event of scripts) { - await runScript({ - path: where, - args: [], - scriptShell, - stdio: 'inherit', - stdioString: true, - banner: log.level !== 'silent', - event, - }) + // run the same set of scripts that `npm install` runs. + if (!ignoreScripts) { + const scripts = [ + 'preinstall', + 'install', + 'postinstall', + 'prepublish', // XXX should we remove this finally?? + 'preprepare', + 'prepare', + 'postprepare', + ] + for (const event of scripts) { + await runScript({ + path: where, + args: [], + scriptShell, + stdio: 'inherit', + stdioString: true, + banner: log.level !== 'silent', + event, + }) + } } + await reifyFinish(this.npm, arb) } - await reifyFinish(arb) } -module.exports = Object.assign(cmd, {usage}) +module.exports = CI |