Skip to content

Commit

Permalink
feat: do all ouput over proc-log events
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Apr 15, 2024
1 parent 60cafe4 commit d9985cc
Show file tree
Hide file tree
Showing 60 changed files with 644 additions and 523 deletions.
4 changes: 2 additions & 2 deletions lib/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { relative } = require('path')

const { definitions } = require('@npmcli/config/lib/definitions')
const { aliases: cmdAliases } = require('./utils/cmd-list')
const { log } = require('proc-log')
const { log, output } = require('proc-log')

class BaseCommand {
static workspaces = false
Expand Down Expand Up @@ -119,7 +119,7 @@ class BaseCommand {
const { config } = this.npm

if (config.get('usage')) {
return this.npm.output(this.usage)
return output.standard(this.usage)
}

const hasWsConfig = config.get('workspaces') || config.get('workspace').length
Expand Down
10 changes: 5 additions & 5 deletions lib/cli-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = async (process, validateEngines) => {
exitHandler.setNpm(npm)

// only log node and npm paths in argv initially since argv can contain sensitive info. a cleaned version will be logged later
const { log } = require('proc-log')
const { log, output } = require('proc-log')
log.verbose('cli', process.argv.slice(0, 2).join(' '))
log.info('using', 'npm@%s', npm.version)
log.info('using', 'node@%s', process.version)
Expand All @@ -41,7 +41,7 @@ module.exports = async (process, validateEngines) => {

// npm -v
if (npm.config.get('version', 'cli')) {
npm.output(npm.version)
output.standard(npm.version)
return exitHandler()
}

Expand All @@ -53,7 +53,7 @@ module.exports = async (process, validateEngines) => {

cmd = npm.argv.shift()
if (!cmd) {
npm.output(npm.usage)
output.standard(npm.usage)
process.exitCode = 1
return exitHandler()
}
Expand All @@ -64,8 +64,8 @@ module.exports = async (process, validateEngines) => {
if (err.code === 'EUNKNOWNCOMMAND') {
const didYouMean = require('./utils/did-you-mean.js')
const suggestions = await didYouMean(npm.localPrefix, cmd)
npm.output(`Unknown command: "${cmd}"${suggestions}\n`)
npm.output('To see a list of supported npm commands, run:\n npm help')
output.standard(`Unknown command: "${cmd}"${suggestions}\n`)
output.standard('To see a list of supported npm commands, run:\n npm help')
process.exitCode = 1
return exitHandler()
}
Expand Down
11 changes: 6 additions & 5 deletions lib/commands/access.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const libnpmaccess = require('libnpmaccess')
const npa = require('npm-package-arg')
const { output } = require('proc-log')
const pkgJson = require('@npmcli/package-json')
const localeCompare = require('@isaacs/string-locale-compare')('en')

Expand Down Expand Up @@ -197,22 +198,22 @@ class Access extends BaseCommand {
}

#output (items, limiter) {
const output = {}
const outputs = {}
const lookup = {
__proto__: null,
read: 'read-only',
write: 'read-write',
}
for (const item in items) {
const val = items[item]
output[item] = lookup[val] || val
outputs[item] = lookup[val] || val
}
if (this.npm.config.get('json')) {
this.npm.output(JSON.stringify(output, null, 2))
output.standard(JSON.stringify(outputs, null, 2))
} else {
for (const item of Object.keys(output).sort(localeCompare)) {
for (const item of Object.keys(outputs).sort(localeCompare)) {
if (!limiter || limiter === item) {
this.npm.output(`${item}: ${output[item]}`)
output.standard(`${item}: ${outputs[item]}`)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/adduser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { log } = require('proc-log')
const { log, output } = require('proc-log')
const { redactLog: replaceInfo } = require('@npmcli/redact')
const auth = require('../utils/auth.js')

Expand Down Expand Up @@ -44,7 +44,7 @@ class AddUser extends BaseCommand {

await this.npm.config.save('user')

this.npm.output(message)
output.standard(message)
}
}
module.exports = AddUser
58 changes: 29 additions & 29 deletions lib/commands/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const tufClient = require('@sigstore/tuf')

const ArboristWorkspaceCmd = require('../arborist-cmd.js')
const auditError = require('../utils/audit-error.js')
const { log } = require('proc-log')
const { log, output } = require('proc-log')
const reifyFinish = require('../utils/reify-finish.js')

const sortAlphabetically = (a, b) => localeCompare(a.name, b.name)
Expand Down Expand Up @@ -65,7 +65,7 @@ class VerifySignatures {
}

if (this.npm.config.get('json')) {
this.npm.output(JSON.stringify({
output.standard(JSON.stringify({
invalid,
missing,
}, null, 2))
Expand All @@ -77,91 +77,91 @@ class VerifySignatures {
const auditedPlural = this.auditedWithKeysCount > 1 ? 's' : ''
const timing = `audited ${this.auditedWithKeysCount} package${auditedPlural} in ` +
`${Math.floor(Number(elapsed) / 1e9)}s`
this.npm.output(timing)
this.npm.output('')
output.standard(timing)
output.standard('')

const verifiedBold = this.npm.chalk.bold('verified')
if (this.verifiedSignatureCount) {
if (this.verifiedSignatureCount === 1) {
/* eslint-disable-next-line max-len */
this.npm.output(`${this.verifiedSignatureCount} package has a ${verifiedBold} registry signature`)
output.standard(`${this.verifiedSignatureCount} package has a ${verifiedBold} registry signature`)
} else {
/* eslint-disable-next-line max-len */
this.npm.output(`${this.verifiedSignatureCount} packages have ${verifiedBold} registry signatures`)
output.standard(`${this.verifiedSignatureCount} packages have ${verifiedBold} registry signatures`)
}
this.npm.output('')
output.standard('')
}

if (this.verifiedAttestationCount) {
if (this.verifiedAttestationCount === 1) {
/* eslint-disable-next-line max-len */
this.npm.output(`${this.verifiedAttestationCount} package has a ${verifiedBold} attestation`)
output.standard(`${this.verifiedAttestationCount} package has a ${verifiedBold} attestation`)
} else {
/* eslint-disable-next-line max-len */
this.npm.output(`${this.verifiedAttestationCount} packages have ${verifiedBold} attestations`)
output.standard(`${this.verifiedAttestationCount} packages have ${verifiedBold} attestations`)
}
this.npm.output('')
output.standard('')
}

if (missing.length) {
const missingClr = this.npm.chalk.bold(this.npm.chalk.red('missing'))
if (missing.length === 1) {
/* eslint-disable-next-line max-len */
this.npm.output(`1 package has a ${missingClr} registry signature but the registry is providing signing keys:`)
output.standard(`1 package has a ${missingClr} registry signature but the registry is providing signing keys:`)
} else {
/* eslint-disable-next-line max-len */
this.npm.output(`${missing.length} packages have ${missingClr} registry signatures but the registry is providing signing keys:`)
output.standard(`${missing.length} packages have ${missingClr} registry signatures but the registry is providing signing keys:`)
}
this.npm.output('')
output.standard('')
missing.map(m =>
this.npm.output(`${this.npm.chalk.red(`${m.name}@${m.version}`)} (${m.registry})`)
output.standard(`${this.npm.chalk.red(`${m.name}@${m.version}`)} (${m.registry})`)
)
}

if (invalid.length) {
if (missing.length) {
this.npm.output('')
output.standard('')
}
const invalidClr = this.npm.chalk.bold(this.npm.chalk.red('invalid'))
// We can have either invalid signatures or invalid provenance
const invalidSignatures = this.invalid.filter(i => i.code === 'EINTEGRITYSIGNATURE')
if (invalidSignatures.length) {
if (invalidSignatures.length === 1) {
this.npm.output(`1 package has an ${invalidClr} registry signature:`)
output.standard(`1 package has an ${invalidClr} registry signature:`)
} else {
/* eslint-disable-next-line max-len */
this.npm.output(`${invalidSignatures.length} packages have ${invalidClr} registry signatures:`)
output.standard(`${invalidSignatures.length} packages have ${invalidClr} registry signatures:`)
}
this.npm.output('')
output.standard('')
invalidSignatures.map(i =>
this.npm.output(`${this.npm.chalk.red(`${i.name}@${i.version}`)} (${i.registry})`)
output.standard(`${this.npm.chalk.red(`${i.name}@${i.version}`)} (${i.registry})`)
)
this.npm.output('')
output.standard('')
}

const invalidAttestations = this.invalid.filter(i => i.code === 'EATTESTATIONVERIFY')
if (invalidAttestations.length) {
if (invalidAttestations.length === 1) {
this.npm.output(`1 package has an ${invalidClr} attestation:`)
output.standard(`1 package has an ${invalidClr} attestation:`)
} else {
/* eslint-disable-next-line max-len */
this.npm.output(`${invalidAttestations.length} packages have ${invalidClr} attestations:`)
output.standard(`${invalidAttestations.length} packages have ${invalidClr} attestations:`)
}
this.npm.output('')
output.standard('')
invalidAttestations.map(i =>
this.npm.output(`${this.npm.chalk.red(`${i.name}@${i.version}`)} (${i.registry})`)
output.standard(`${this.npm.chalk.red(`${i.name}@${i.version}`)} (${i.registry})`)
)
this.npm.output('')
output.standard('')
}

if (invalid.length === 1) {
/* eslint-disable-next-line max-len */
this.npm.output(`Someone might have tampered with this package since it was published on the registry!`)
output.standard(`Someone might have tampered with this package since it was published on the registry!`)
} else {
/* eslint-disable-next-line max-len */
this.npm.output(`Someone might have tampered with these packages since they were published on the registry!`)
output.standard(`Someone might have tampered with these packages since they were published on the registry!`)
}
this.npm.output('')
output.standard('')
}
}

Expand Down Expand Up @@ -463,7 +463,7 @@ class Audit extends ArboristWorkspaceCmd {
chalk: this.npm.chalk,
})
process.exitCode = process.exitCode || result.exitCode
this.npm.output(result.report)
output.standard(result.report)
}
}

Expand Down
22 changes: 11 additions & 11 deletions lib/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const BaseCommand = require('../base-command.js')
const npa = require('npm-package-arg')
const jsonParse = require('json-parse-even-better-errors')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const { log } = require('proc-log')
const { log, output } = require('proc-log')

const searchCachePackage = async (path, parsed, cacheKeys) => {
/* eslint-disable-next-line max-len */
Expand Down Expand Up @@ -135,7 +135,7 @@ class Cache extends BaseCommand {
log.warn(`Not Found: ${key}`)
break
}
this.npm.output(`Deleted: ${key}`)
output.standard(`Deleted: ${key}`)
await cacache.rm.entry(cachePath, key)
// XXX this could leave other entries without content!
await cacache.rm.content(cachePath, entry.integrity)
Expand Down Expand Up @@ -170,20 +170,20 @@ class Cache extends BaseCommand {
? `~${cache.slice(process.env.HOME.length)}`
: cache
const stats = await cacache.verify(cache)
this.npm.output(`Cache verified and compressed (${prefix})`)
this.npm.output(`Content verified: ${stats.verifiedContent} (${stats.keptSize} bytes)`)
output.standard(`Cache verified and compressed (${prefix})`)
output.standard(`Content verified: ${stats.verifiedContent} (${stats.keptSize} bytes)`)
if (stats.badContentCount) {
this.npm.output(`Corrupted content removed: ${stats.badContentCount}`)
output.standard(`Corrupted content removed: ${stats.badContentCount}`)
}
if (stats.reclaimedCount) {
/* eslint-disable-next-line max-len */
this.npm.output(`Content garbage-collected: ${stats.reclaimedCount} (${stats.reclaimedSize} bytes)`)
output.standard(`Content garbage-collected: ${stats.reclaimedCount} (${stats.reclaimedSize} bytes)`)
}
if (stats.missingContent) {
this.npm.output(`Missing content: ${stats.missingContent}`)
output.standard(`Missing content: ${stats.missingContent}`)
}
this.npm.output(`Index entries: ${stats.totalEntries}`)
this.npm.output(`Finished in ${stats.runTime.total / 1000}s`)
output.standard(`Index entries: ${stats.totalEntries}`)
output.standard(`Finished in ${stats.runTime.total / 1000}s`)
}

// npm cache ls [--package <spec> ...]
Expand All @@ -203,10 +203,10 @@ class Cache extends BaseCommand {
results.add(key)
}
}
[...results].sort(localeCompare).forEach(key => this.npm.output(key))
[...results].sort(localeCompare).forEach(key => output.standard(key))
return
}
cacheKeys.sort(localeCompare).forEach(key => this.npm.output(key))
cacheKeys.sort(localeCompare).forEach(key => output.standard(key))
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
const fs = require('fs/promises')
const nopt = require('nopt')
const { resolve } = require('path')
const { output } = require('proc-log')

const Npm = require('../npm.js')
const { definitions, shorthands } = require('@npmcli/config/lib/definitions')
Expand Down Expand Up @@ -185,7 +186,7 @@ class Completion extends BaseCommand {
}

if (compls.length > 0) {
this.npm.output(compls.join('\n'))
output.standard(compls.join('\n'))
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const ini = require('ini')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const pkgJson = require('@npmcli/package-json')
const { defaults, definitions } = require('@npmcli/config/lib/definitions')
const { log } = require('proc-log')
const { log, output } = require('proc-log')

// These are the configs that we can nerf-dart. Not all of them currently even
// *have* config definitions so we have to explicitly validate them here
Expand Down Expand Up @@ -185,7 +185,7 @@ class Config extends BaseCommand {
const pref = keys.length > 1 ? `${key}=` : ''
out.push(pref + this.npm.config.get(key))
}
this.npm.output(out.join('\n'))
output.standard(out.join('\n'))
}

async del (keys) {
Expand Down Expand Up @@ -282,7 +282,7 @@ ${defData}
this.npm.config.repair(problems)
const locations = []

this.npm.output('The following configuration problems have been repaired:\n')
output.standard('The following configuration problems have been repaired:\n')
const summary = problems.map(({ action, from, to, key, where }) => {
// coverage disabled for else branch because it is intentionally omitted
// istanbul ignore else
Expand All @@ -295,7 +295,7 @@ ${defData}
return `- \`${key}\` deleted from ${where} config`
}
}).join('\n')
this.npm.output(summary)
output.standard(summary)

return await Promise.all(locations.map((location) => this.npm.config.save(location)))
}
Expand Down Expand Up @@ -354,7 +354,7 @@ ${defData}
}
}

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions lib/commands/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const libnpmdiff = require('libnpmdiff')
const npa = require('npm-package-arg')
const pacote = require('pacote')
const pickManifest = require('npm-pick-manifest')
const { log } = require('proc-log')
const { log, output } = require('proc-log')
const pkgJson = require('@npmcli/package-json')
const BaseCommand = require('../base-command.js')

Expand Down Expand Up @@ -64,7 +64,7 @@ class Diff extends BaseCommand {
diffFiles: args,
where: this.top,
})
return this.npm.output(res)
return output.standard(res)
}

async execWorkspaces (args) {
Expand Down
Loading

0 comments on commit d9985cc

Please sign in to comment.