From 4a5dd3a5a200b3f4f7b47168497d8e03dca3a2ca Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 24 Feb 2021 15:54:50 -0800 Subject: 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 --- lib/bugs.js | 69 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 30 deletions(-) (limited to 'lib/bugs.js') diff --git a/lib/bugs.js b/lib/bugs.js index 09856313c..fb0d7c927 100644 --- a/lib/bugs.js +++ b/lib/bugs.js @@ -1,46 +1,55 @@ const log = require('npmlog') const pacote = require('pacote') -const { promisify } = require('util') -const openUrl = promisify(require('./utils/open-url.js')) +const openUrl = require('./utils/open-url.js') const usageUtil = require('./utils/usage.js') -const npm = require('./npm.js') const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js') -const usage = usageUtil('bugs', 'npm bugs []') +class Bugs { + constructor (npm) { + this.npm = npm + } -const cmd = (args, cb) => bugs(args).then(() => cb()).catch(cb) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + get usage () { + return usageUtil('bugs', 'npm bugs []') + } -const bugs = async args => { - if (!args || !args.length) - args = ['.'] + exec (args, cb) { + this.bugs(args).then(() => cb()).catch(cb) + } - await Promise.all(args.map(pkg => getBugs(pkg))) -} + async bugs (args) { + if (!args || !args.length) + args = ['.'] -const getBugsUrl = mani => { - if (mani.bugs) { - if (typeof mani.bugs === 'string') - return mani.bugs + await Promise.all(args.map(pkg => this.getBugs(pkg))) + } - if (typeof mani.bugs === 'object' && mani.bugs.url) - return mani.bugs.url + async getBugs (pkg) { + const opts = { ...this.npm.flatOptions, fullMetadata: true } + const mani = await pacote.manifest(pkg, opts) + const url = this.getBugsUrl(mani) + log.silly('bugs', 'url', url) + await openUrl(this.npm, url, `${mani.name} bug list available at the following URL`) } - // try to get it from the repo, if possible - const info = hostedFromMani(mani) - if (info) - return info.bugs() + getBugsUrl (mani) { + if (mani.bugs) { + if (typeof mani.bugs === 'string') + return mani.bugs - // just send them to the website, hopefully that has some info! - return `https://www.npmjs.com/package/${mani.name}` -} + if (typeof mani.bugs === 'object' && mani.bugs.url) + return mani.bugs.url + } -const getBugs = async pkg => { - const opts = { ...npm.flatOptions, fullMetadata: true } - const mani = await pacote.manifest(pkg, opts) - const url = getBugsUrl(mani) - log.silly('bugs', 'url', url) - await openUrl(url, `${mani.name} bug list available at the following URL`) + // try to get it from the repo, if possible + const info = hostedFromMani(mani) + if (info) + return info.bugs() + + // just send them to the website, hopefully that has some info! + return `https://www.npmjs.com/package/${mani.name}` + } } -module.exports = Object.assign(cmd, { usage }) +module.exports = Bugs -- cgit v1.2.3