Skip to content

Commit edfc3bb

Browse files
committed
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.
1 parent bd2545d commit edfc3bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+541
-585
lines changed

lib/access.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const path = require('path')
33
const libaccess = require('libnpmaccess')
44
const readPackageJson = require('read-package-json-fast')
55

6-
const output = require('./utils/output.js')
76
const otplease = require('./utils/otplease.js')
87
const usageUtil = require('./utils/usage.js')
98
const getIdentity = require('./utils/get-identity.js')
@@ -157,7 +156,7 @@ class Access {
157156
const pkgs = await libaccess.lsPackages(owner, opts)
158157

159158
// TODO - print these out nicely (breaking change)
160-
output(JSON.stringify(pkgs, null, 2))
159+
this.npm.output(JSON.stringify(pkgs, null, 2))
161160
}
162161

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

171170
// TODO - print these out nicely (breaking change)
172-
output(JSON.stringify(collabs, null, 2))
171+
this.npm.output(JSON.stringify(collabs, null, 2))
173172
}
174173

175174
async edit () {

lib/adduser.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const log = require('npmlog')
2-
const output = require('./utils/output.js')
32
const usageUtil = require('./utils/usage.js')
43
const replaceInfo = require('./utils/replace-info.js')
54
const authTypes = {
@@ -49,7 +48,7 @@ class AddUser {
4948
scope,
5049
})
5150

52-
output(message)
51+
this.npm.output(message)
5352
}
5453

5554
getRegistry ({ scope, registry }) {

lib/audit.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const Arborist = require('@npmcli/arborist')
22
const auditReport = require('npm-audit-report')
3-
const output = require('./utils/output.js')
43
const reifyFinish = require('./utils/reify-finish.js')
54
const auditError = require('./utils/audit-error.js')
65
const usageUtil = require('./utils/usage.js')
@@ -57,7 +56,7 @@ class Audit {
5756
reporter,
5857
})
5958
process.exitCode = process.exitCode || result.exitCode
60-
output(result.report)
59+
this.npm.output(result.report)
6160
}
6261
}
6362
}

lib/bin.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const output = require('./utils/output.js')
21
const envPath = require('./utils/path.js')
32
const usageUtil = require('./utils/usage.js')
43

@@ -18,7 +17,7 @@ class Bin {
1817

1918
async bin (args) {
2019
const b = this.npm.bin
21-
output(b)
20+
this.npm.output(b)
2221
if (this.npm.flatOptions.global && !envPath.includes(b))
2322
console.error('(not in PATH env variable)')
2423
}

lib/cache.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const cacache = require('cacache')
22
const { promisify } = require('util')
33
const log = require('npmlog')
4-
const output = require('./utils/output.js')
54
const pacote = require('pacote')
65
const path = require('path')
76
const rimraf = promisify(require('rimraf'))
@@ -116,13 +115,13 @@ with --force.`)
116115
? `~${cache.substr(process.env.HOME.length)}`
117116
: cache
118117
const stats = await cacache.verify(cache)
119-
output(`Cache verified and compressed (${prefix})`)
120-
output(`Content verified: ${stats.verifiedContent} (${stats.keptSize} bytes)`)
121-
stats.badContentCount && output(`Corrupted content removed: ${stats.badContentCount}`)
122-
stats.reclaimedCount && output(`Content garbage-collected: ${stats.reclaimedCount} (${stats.reclaimedSize} bytes)`)
123-
stats.missingContent && output(`Missing content: ${stats.missingContent}`)
124-
output(`Index entries: ${stats.totalEntries}`)
125-
output(`Finished in ${stats.runTime.total / 1000}s`)
118+
this.npm.output(`Cache verified and compressed (${prefix})`)
119+
this.npm.output(`Content verified: ${stats.verifiedContent} (${stats.keptSize} bytes)`)
120+
stats.badContentCount && this.npm.output(`Corrupted content removed: ${stats.badContentCount}`)
121+
stats.reclaimedCount && this.npm.output(`Content garbage-collected: ${stats.reclaimedCount} (${stats.reclaimedSize} bytes)`)
122+
stats.missingContent && this.npm.output(`Missing content: ${stats.missingContent}`)
123+
this.npm.output(`Index entries: ${stats.totalEntries}`)
124+
this.npm.output(`Finished in ${stats.runTime.total / 1000}s`)
126125
}
127126
}
128127

lib/completion.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const configNames = Object.keys(types)
3939
const shorthandNames = Object.keys(shorthands)
4040
const allConfs = configNames.concat(shorthandNames)
4141
const isWindowsShell = require('./utils/is-windows-shell.js')
42-
const output = require('./utils/output.js')
4342
const fileExists = require('./utils/file-exists.js')
4443

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

132131
if (partialWords.slice(0, -1).indexOf('--') === -1) {
133132
if (word.charAt(0) === '-')
134-
return wrap(opts, configCompl(opts))
133+
return this.wrap(opts, configCompl(opts))
135134

136135
if (words[w - 1] &&
137136
words[w - 1].charAt(0) === '-' &&
138137
!isFlag(words[w - 1])) {
139138
// awaiting a value for a non-bool config.
140139
// don't even try to do this for now
141-
return wrap(opts, configValueCompl(opts))
140+
return this.wrap(opts, configValueCompl(opts))
142141
}
143142
}
144143

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

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

@@ -162,9 +161,29 @@ class Completion {
162161
const impl = this.npm.commands[cmd]
163162
if (impl && impl.completion) {
164163
const comps = await impl.completion(opts)
165-
return wrap(opts, comps)
164+
return this.wrap(opts, comps)
166165
}
167166
}
167+
168+
// The command should respond with an array. Loop over that,
169+
// wrapping quotes around any that have spaces, and writing
170+
// them to stdout.
171+
// If any of the items are arrays, then join them with a space.
172+
// Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
173+
// to: 'a', 'b c', or 'd' 'e'
174+
wrap (opts, compls) {
175+
if (!Array.isArray(compls))
176+
compls = compls ? [compls] : []
177+
178+
compls = compls.map(c =>
179+
Array.isArray(c) ? c.map(escape).join(' ') : escape(c))
180+
181+
if (opts.partialWord)
182+
compls = compls.filter(c => c.startsWith(opts.partialWord))
183+
184+
if (compls.length > 0)
185+
this.npm.output(compls.join('\n'))
186+
}
168187
}
169188

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

217-
// The command should respond with an array. Loop over that,
218-
// wrapping quotes around any that have spaces, and writing
219-
// them to stdout.
220-
// If any of the items are arrays, then join them with a space.
221-
// Ie, returning ['a', 'b c', ['d', 'e']] would allow it to expand
222-
// to: 'a', 'b c', or 'd' 'e'
223-
const wrap = (opts, compls) => {
224-
if (!Array.isArray(compls))
225-
compls = compls ? [compls] : []
226-
227-
compls = compls.map(c =>
228-
Array.isArray(c) ? c.map(escape).join(' ') : escape(c))
229-
230-
if (opts.partialWord)
231-
compls = compls.filter(c => c.startsWith(opts.partialWord))
232-
233-
if (compls.length > 0)
234-
output(compls.join('\n'))
235-
}
236-
237236
// the current word has a dash. Return the config names,
238237
// with the same number of dashes as the current word has.
239238
const configCompl = opts => {

lib/config.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { defaults, types } = require('./utils/config.js')
22
const usageUtil = require('./utils/usage.js')
3-
const output = require('./utils/output.js')
43

54
const mkdirp = require('mkdirp-infer-owner')
65
const { dirname } = require('path')
@@ -142,7 +141,7 @@ class Config {
142141
const pref = keys.length > 1 ? `${key}=` : ''
143142
out.push(pref + this.npm.config.get(key))
144143
}
145-
output(out.join('\n'))
144+
this.npm.output(out.join('\n'))
146145
}
147146

148147
async del (keys) {
@@ -241,7 +240,7 @@ ${defData}
241240
)
242241
}
243242

244-
output(msg.join('\n').trim())
243+
this.npm.output(msg.join('\n').trim())
245244
}
246245

247246
async listJson () {
@@ -252,7 +251,7 @@ ${defData}
252251

253252
publicConf[key] = this.npm.config.get(key)
254253
}
255-
output(JSON.stringify(publicConf, null, 2))
254+
this.npm.output(JSON.stringify(publicConf, null, 2))
256255
}
257256

258257
usageError () {

lib/diff.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const pacote = require('pacote')
99
const pickManifest = require('npm-pick-manifest')
1010

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

1514
class Diff {
@@ -54,7 +53,7 @@ class Diff {
5453
[a, b],
5554
{ ...this.npm.flatOptions, diffFiles: args }
5655
)
57-
return output(res)
56+
return this.npm.output(res)
5857
}
5958

6059
async retrieveSpecs ([a, b]) {

lib/dist-tag.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const npa = require('npm-package-arg')
33
const regFetch = require('npm-registry-fetch')
44
const semver = require('semver')
55

6-
const output = require('./utils/output.js')
76
const otplease = require('./utils/otplease.js')
87
const readLocalPkgName = require('./utils/read-local-package.js')
98
const usageUtil = require('./utils/usage.js')
@@ -91,7 +90,7 @@ class DistTag {
9190
spec,
9291
}
9392
await otplease(reqOpts, reqOpts => regFetch(url, reqOpts))
94-
output(`+${t}: ${spec.name}@${version}`)
93+
this.npm.output(`+${t}: ${spec.name}@${version}`)
9594
}
9695

9796
async remove (spec, tag, opts) {
@@ -116,7 +115,7 @@ class DistTag {
116115
spec,
117116
}
118117
await otplease(reqOpts, reqOpts => regFetch(url, reqOpts))
119-
output(`-${tag}: ${spec.name}@${version}`)
118+
this.npm.output(`-${tag}: ${spec.name}@${version}`)
120119
}
121120

122121
async list (spec, opts) {
@@ -133,7 +132,7 @@ class DistTag {
133132
const tags = await this.fetchTags(spec, opts)
134133
const msg =
135134
Object.keys(tags).map(k => `${k}: ${tags[k]}`).sort().join('\n')
136-
output(msg)
135+
this.npm.output(msg)
137136
return tags
138137
} catch (err) {
139138
log.error('dist-tag ls', "Couldn't get dist-tag data for", spec)

lib/doctor.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const semver = require('semver')
1010
const { promisify } = require('util')
1111
const ansiTrim = require('./utils/ansi-trim.js')
1212
const isWindows = require('./utils/is-windows.js')
13-
const output = require('./utils/output.js')
1413
const ping = require('./utils/ping.js')
1514
const usageUtil = require('./utils/usage.js')
1615
const { defaults: { registry: defaultRegistry } } = require('./utils/config.js')
@@ -111,7 +110,7 @@ class Doctor {
111110
const silent = this.npm.log.levels[this.npm.log.level] >
112111
this.npm.log.levels.error
113112
if (!silent) {
114-
output(table(outTable, tableOpts))
113+
this.npm.output(table(outTable, tableOpts))
115114
if (!allOk)
116115
console.error('')
117116
}

0 commit comments

Comments
 (0)