Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(npm.output): make output go through npm.output
All output that anything wants to make now goes through
`npm.output()`.  This is an incremental change getting us
closer to where we want to be with testing.

PR-URL: #2795
Credit: @wraithgar
Close: #2795
Reviewed-by: @ruyadorno, @isaacs
  • Loading branch information
wraithgar committed Mar 9, 2021
1 parent 7ff152d commit 85a8694
Show file tree
Hide file tree
Showing 88 changed files with 541 additions and 585 deletions.
5 changes: 2 additions & 3 deletions lib/access.js
Expand Up @@ -3,7 +3,6 @@ const path = require('path')
const libaccess = require('libnpmaccess')
const readPackageJson = require('read-package-json-fast')

const output = require('./utils/output.js')
const otplease = require('./utils/otplease.js')
const usageUtil = require('./utils/usage.js')
const getIdentity = require('./utils/get-identity.js')
Expand Down Expand Up @@ -157,7 +156,7 @@ class Access {
const pkgs = await libaccess.lsPackages(owner, opts)

// TODO - print these out nicely (breaking change)
output(JSON.stringify(pkgs, null, 2))
this.npm.output(JSON.stringify(pkgs, null, 2))
}

get ['ls-collaborators'] () {
Expand All @@ -169,7 +168,7 @@ class Access {
const collabs = await libaccess.lsCollaborators(pkgName, usr, opts)

// TODO - print these out nicely (breaking change)
output(JSON.stringify(collabs, null, 2))
this.npm.output(JSON.stringify(collabs, null, 2))
}

async edit () {
Expand Down
3 changes: 1 addition & 2 deletions lib/adduser.js
@@ -1,5 +1,4 @@
const log = require('npmlog')
const output = require('./utils/output.js')
const usageUtil = require('./utils/usage.js')
const replaceInfo = require('./utils/replace-info.js')
const authTypes = {
Expand Down Expand Up @@ -49,7 +48,7 @@ class AddUser {
scope,
})

output(message)
this.npm.output(message)
}

getRegistry ({ scope, registry }) {
Expand Down
3 changes: 1 addition & 2 deletions lib/audit.js
@@ -1,6 +1,5 @@
const Arborist = require('@npmcli/arborist')
const auditReport = require('npm-audit-report')
const output = require('./utils/output.js')
const reifyFinish = require('./utils/reify-finish.js')
const auditError = require('./utils/audit-error.js')
const usageUtil = require('./utils/usage.js')
Expand Down Expand Up @@ -57,7 +56,7 @@ class Audit {
reporter,
})
process.exitCode = process.exitCode || result.exitCode
output(result.report)
this.npm.output(result.report)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions lib/bin.js
@@ -1,4 +1,3 @@
const output = require('./utils/output.js')
const envPath = require('./utils/path.js')
const usageUtil = require('./utils/usage.js')

Expand All @@ -18,7 +17,7 @@ class Bin {

async bin (args) {
const b = this.npm.bin
output(b)
this.npm.output(b)
if (this.npm.flatOptions.global && !envPath.includes(b))
console.error('(not in PATH env variable)')
}
Expand Down
15 changes: 7 additions & 8 deletions lib/cache.js
@@ -1,7 +1,6 @@
const cacache = require('cacache')
const { promisify } = require('util')
const log = require('npmlog')
const output = require('./utils/output.js')
const pacote = require('pacote')
const path = require('path')
const rimraf = promisify(require('rimraf'))
Expand Down Expand Up @@ -116,13 +115,13 @@ with --force.`)
? `~${cache.substr(process.env.HOME.length)}`
: cache
const stats = await cacache.verify(cache)
output(`Cache verified and compressed (${prefix})`)
output(`Content verified: ${stats.verifiedContent} (${stats.keptSize} bytes)`)
stats.badContentCount && output(`Corrupted content removed: ${stats.badContentCount}`)
stats.reclaimedCount && output(`Content garbage-collected: ${stats.reclaimedCount} (${stats.reclaimedSize} bytes)`)
stats.missingContent && output(`Missing content: ${stats.missingContent}`)
output(`Index entries: ${stats.totalEntries}`)
output(`Finished in ${stats.runTime.total / 1000}s`)
this.npm.output(`Cache verified and compressed (${prefix})`)
this.npm.output(`Content verified: ${stats.verifiedContent} (${stats.keptSize} bytes)`)
stats.badContentCount && this.npm.output(`Corrupted content removed: ${stats.badContentCount}`)
stats.reclaimedCount && this.npm.output(`Content garbage-collected: ${stats.reclaimedCount} (${stats.reclaimedSize} bytes)`)
stats.missingContent && this.npm.output(`Missing content: ${stats.missingContent}`)
this.npm.output(`Index entries: ${stats.totalEntries}`)
this.npm.output(`Finished in ${stats.runTime.total / 1000}s`)
}
}

Expand Down
49 changes: 24 additions & 25 deletions lib/completion.js
Expand Up @@ -39,7 +39,6 @@ const configNames = Object.keys(types)
const shorthandNames = Object.keys(shorthands)
const allConfs = configNames.concat(shorthandNames)
const isWindowsShell = require('./utils/is-windows-shell.js')
const output = require('./utils/output.js')
const fileExists = require('./utils/file-exists.js')

const usageUtil = require('./utils/usage.js')
Expand Down Expand Up @@ -131,14 +130,14 @@ class Completion {

if (partialWords.slice(0, -1).indexOf('--') === -1) {
if (word.charAt(0) === '-')
return wrap(opts, configCompl(opts))
return this.wrap(opts, configCompl(opts))

if (words[w - 1] &&
words[w - 1].charAt(0) === '-' &&
!isFlag(words[w - 1])) {
// awaiting a value for a non-bool config.
// don't even try to do this for now
return wrap(opts, configValueCompl(opts))
return this.wrap(opts, configValueCompl(opts))
}
}

Expand All @@ -152,7 +151,7 @@ class Completion {
// check if there's a command already.
const cmd = parsed.argv.remain[1]
if (!cmd)
return wrap(opts, cmdCompl(opts))
return this.wrap(opts, cmdCompl(opts))

Object.keys(parsed).forEach(k => this.npm.config.set(k, parsed[k]))

Expand All @@ -162,9 +161,29 @@ class Completion {
const impl = this.npm.commands[cmd]
if (impl && impl.completion) {
const comps = await impl.completion(opts)
return wrap(opts, comps)
return this.wrap(opts, comps)
}
}

// The command should respond with an array. Loop over that,
// wrapping quotes around any that have spaces, and writing
// them to stdout.
// If any of the items are arrays, then join them with a space.
// Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
// to: 'a', 'b c', or 'd' 'e'
wrap (opts, compls) {
if (!Array.isArray(compls))
compls = compls ? [compls] : []

compls = compls.map(c =>
Array.isArray(c) ? c.map(escape).join(' ') : escape(c))

if (opts.partialWord)
compls = compls.filter(c => c.startsWith(opts.partialWord))

if (compls.length > 0)
this.npm.output(compls.join('\n'))
}
}

const dumpScript = async () => {
Expand Down Expand Up @@ -214,26 +233,6 @@ const unescape = w => w.charAt(0) === '\'' ? w.replace(/^'|'$/g, '')
const escape = w => !/\s+/.test(w) ? w
: '\'' + w + '\''

// The command should respond with an array. Loop over that,
// wrapping quotes around any that have spaces, and writing
// them to stdout.
// If any of the items are arrays, then join them with a space.
// Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
// to: 'a', 'b c', or 'd' 'e'
const wrap = (opts, compls) => {
if (!Array.isArray(compls))
compls = compls ? [compls] : []

compls = compls.map(c =>
Array.isArray(c) ? c.map(escape).join(' ') : escape(c))

if (opts.partialWord)
compls = compls.filter(c => c.startsWith(opts.partialWord))

if (compls.length > 0)
output(compls.join('\n'))
}

// the current word has a dash. Return the config names,
// with the same number of dashes as the current word has.
const configCompl = opts => {
Expand Down
7 changes: 3 additions & 4 deletions lib/config.js
@@ -1,6 +1,5 @@
const { defaults, types } = require('./utils/config.js')
const usageUtil = require('./utils/usage.js')
const output = require('./utils/output.js')

const mkdirp = require('mkdirp-infer-owner')
const { dirname } = require('path')
Expand Down Expand Up @@ -142,7 +141,7 @@ class Config {
const pref = keys.length > 1 ? `${key}=` : ''
out.push(pref + this.npm.config.get(key))
}
output(out.join('\n'))
this.npm.output(out.join('\n'))
}

async del (keys) {
Expand Down Expand Up @@ -241,7 +240,7 @@ ${defData}
)
}

output(msg.join('\n').trim())
this.npm.output(msg.join('\n').trim())
}

async listJson () {
Expand All @@ -252,7 +251,7 @@ ${defData}

publicConf[key] = this.npm.config.get(key)
}
output(JSON.stringify(publicConf, null, 2))
this.npm.output(JSON.stringify(publicConf, null, 2))
}

usageError () {
Expand Down
3 changes: 1 addition & 2 deletions lib/diff.js
Expand Up @@ -9,7 +9,6 @@ const pacote = require('pacote')
const pickManifest = require('npm-pick-manifest')

const usageUtil = require('./utils/usage.js')
const output = require('./utils/output.js')
const readLocalPkg = require('./utils/read-local-package.js')

class Diff {
Expand Down Expand Up @@ -55,7 +54,7 @@ class Diff {
diffFiles: args,
where: this.where,
})
return output(res)
return this.npm.output(res)
}

async retrieveSpecs ([a, b]) {
Expand Down
7 changes: 3 additions & 4 deletions lib/dist-tag.js
Expand Up @@ -3,7 +3,6 @@ const npa = require('npm-package-arg')
const regFetch = require('npm-registry-fetch')
const semver = require('semver')

const output = require('./utils/output.js')
const otplease = require('./utils/otplease.js')
const readLocalPkgName = require('./utils/read-local-package.js')
const usageUtil = require('./utils/usage.js')
Expand Down Expand Up @@ -91,7 +90,7 @@ class DistTag {
spec,
}
await otplease(reqOpts, reqOpts => regFetch(url, reqOpts))
output(`+${t}: ${spec.name}@${version}`)
this.npm.output(`+${t}: ${spec.name}@${version}`)
}

async remove (spec, tag, opts) {
Expand All @@ -116,7 +115,7 @@ class DistTag {
spec,
}
await otplease(reqOpts, reqOpts => regFetch(url, reqOpts))
output(`-${tag}: ${spec.name}@${version}`)
this.npm.output(`-${tag}: ${spec.name}@${version}`)
}

async list (spec, opts) {
Expand All @@ -133,7 +132,7 @@ class DistTag {
const tags = await this.fetchTags(spec, opts)
const msg =
Object.keys(tags).map(k => `${k}: ${tags[k]}`).sort().join('\n')
output(msg)
this.npm.output(msg)
return tags
} catch (err) {
log.error('dist-tag ls', "Couldn't get dist-tag data for", spec)
Expand Down
3 changes: 1 addition & 2 deletions lib/doctor.js
Expand Up @@ -10,7 +10,6 @@ const semver = require('semver')
const { promisify } = require('util')
const ansiTrim = require('./utils/ansi-trim.js')
const isWindows = require('./utils/is-windows.js')
const output = require('./utils/output.js')
const ping = require('./utils/ping.js')
const usageUtil = require('./utils/usage.js')
const { defaults: { registry: defaultRegistry } } = require('./utils/config.js')
Expand Down Expand Up @@ -111,7 +110,7 @@ class Doctor {
const silent = this.npm.log.levels[this.npm.log.level] >
this.npm.log.levels.error
if (!silent) {
output(table(outTable, tableOpts))
this.npm.output(table(outTable, tableOpts))
if (!allOk)
console.error('')
}
Expand Down
3 changes: 1 addition & 2 deletions lib/exec.js
@@ -1,4 +1,3 @@
const output = require('./utils/output.js')
const usageUtil = require('./utils/usage.js')
const { promisify } = require('util')
const read = promisify(require('read'))
Expand Down Expand Up @@ -224,7 +223,7 @@ class Exec {
if (process.stdin.isTTY) {
if (ciDetect())
return this.npm.log.warn('exec', 'Interactive mode disabled in CI environment')
output(`\nEntering npm script environment\nType 'exit' or ^D when finished\n`)
this.npm.output(`\nEntering npm script environment\nType 'exit' or ^D when finished\n`)
}
}
return await runScript({
Expand Down
5 changes: 2 additions & 3 deletions lib/explain.js
@@ -1,7 +1,6 @@
const usageUtil = require('./utils/usage.js')
const { explainNode } = require('./utils/explain-dep.js')
const completion = require('./utils/completion/installed-deep.js')
const output = require('./utils/output.js')
const Arborist = require('@npmcli/arborist')
const npa = require('npm-package-arg')
const semver = require('semver')
Expand Down Expand Up @@ -59,9 +58,9 @@ class Explain {
}

if (this.npm.flatOptions.json)
output(JSON.stringify(expls, null, 2))
this.npm.output(JSON.stringify(expls, null, 2))
else {
output(expls.map(expl => {
this.npm.output(expls.map(expl => {
return explainNode(expl, Infinity, this.npm.color)
}).join('\n\n'))
}
Expand Down
3 changes: 1 addition & 2 deletions lib/explore.js
Expand Up @@ -5,7 +5,6 @@ const rpj = require('read-package-json-fast')
const runScript = require('@npmcli/run-script')
const { join, resolve, relative } = require('path')
const completion = require('./utils/completion/installed-shallow.js')
const output = require('./utils/output.js')
const usageUtil = require('./utils/usage.js')

class Explore {
Expand Down Expand Up @@ -54,7 +53,7 @@ class Explore {
}

if (!args.length)
output(`\nExploring ${path}\nType 'exit' or ^D when finished\n`)
this.npm.output(`\nExploring ${path}\nType 'exit' or ^D when finished\n`)
this.npm.log.disableProgress()
try {
return await runScript({
Expand Down
7 changes: 3 additions & 4 deletions lib/fund.js
Expand Up @@ -12,7 +12,6 @@ const {
} = require('libnpmfund')

const completion = require('./utils/completion/installed-deep.js')
const output = require('./utils/output.js')
const openUrl = require('./utils/open-url.js')
const usageUtil = require('./utils/usage.js')

Expand Down Expand Up @@ -85,7 +84,7 @@ class Fund {
? this.printJSON
: this.printHuman

output(
this.npm.output(
print(
getFundingInfo(tree),
opts
Expand Down Expand Up @@ -206,9 +205,9 @@ class Fund {
validSources.forEach(({ type, url }, i) => {
const typePrefix = type ? `${type} funding` : 'Funding'
const msg = `${typePrefix} available at the following URL`
output(`${i + 1}: ${msg}: ${url}`)
this.npm.output(`${i + 1}: ${msg}: ${url}`)
})
output('Run `npm fund [<@scope>/]<pkg> --which=1`, for example, to open the first funding URL listed in that package')
this.npm.output('Run `npm fund [<@scope>/]<pkg> --which=1`, for example, to open the first funding URL listed in that package')
} else {
const noFundingError = new Error(`No valid funding method available for: ${spec}`)
noFundingError.code = 'ENOFUND'
Expand Down
5 changes: 2 additions & 3 deletions lib/help-search.js
@@ -1,7 +1,6 @@
const fs = require('fs')
const path = require('path')
const color = require('ansicolors')
const output = require('./utils/output.js')
const usageUtil = require('./utils/usage.js')
const npmUsage = require('./utils/npm-usage.js')
const { promisify } = require('util')
Expand Down Expand Up @@ -44,8 +43,8 @@ class HelpSearch {
if (!formatted.trim())
npmUsage(this.npm, false)
else {
output(formatted)
output(didYouMean(args[0], cmdList))
this.npm.output(formatted)
this.npm.output(didYouMean(args[0], cmdList))
}
}

Expand Down

0 comments on commit 85a8694

Please sign in to comment.