Skip to content

Commit 1b93385

Browse files
committed
fix(log): pass in logger to external modules
Most of these module use npm-registry-fetch under the hood, which will log things like the `npm-notice` header if seen. Currently we aren't passing in a logger to them, which means that log message is never seen, among any other logged messages those modules may need to make. I added tests where I could. Some tests were in a state where the entire libnpm* module was an empty mocked function, so asserting that it got passed a `log` attribute was onerous.
1 parent 003d7fb commit 1b93385

25 files changed

+183
-81
lines changed

lib/commands/access.js

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

6+
const log = require('../utils/log-shim.js')
67
const otplease = require('../utils/otplease.js')
78
const getIdentity = require('../utils/get-identity.js')
89
const BaseCommand = require('../base-command.js')
@@ -76,7 +77,10 @@ class Access extends BaseCommand {
7677
throw this.usageError(`${cmd} is not a recognized subcommand.`)
7778
}
7879

79-
return this[cmd](args, this.npm.flatOptions)
80+
return this[cmd](args, {
81+
...this.npm.flatOptions,
82+
log,
83+
})
8084
}
8185

8286
public ([pkg], opts) {

lib/commands/deprecate.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fetch = require('npm-registry-fetch')
2+
const log = require('../utils/log-shim.js')
23
const otplease = require('../utils/otplease.js')
34
const npa = require('npm-package-arg')
45
const semver = require('semver')
@@ -50,6 +51,7 @@ class Deprecate extends BaseCommand {
5051
...this.npm.flatOptions,
5152
spec: p,
5253
query: { write: true },
54+
log,
5355
})
5456

5557
Object.keys(packument.versions)
@@ -64,6 +66,7 @@ class Deprecate extends BaseCommand {
6466
method: 'PUT',
6567
body: packument,
6668
ignoreBody: true,
69+
log,
6770
}))
6871
}
6972
}

lib/commands/diff.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Diff extends BaseCommand {
6161
...this.npm.flatOptions,
6262
diffFiles: args,
6363
where: this.top,
64+
log,
6465
})
6566
return this.npm.output(res)
6667
}
@@ -193,6 +194,7 @@ class Diff extends BaseCommand {
193194
const packument = await pacote.packument(spec, {
194195
...this.npm.flatOptions,
195196
preferOnline: true,
197+
log,
196198
})
197199
bSpec = pickManifest(
198200
packument,

lib/commands/dist-tag.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ class DistTag extends BaseCommand {
2929
}
3030

3131
async exec ([cmdName, pkg, tag]) {
32-
const opts = this.npm.flatOptions
32+
const opts = {
33+
...this.npm.flatOptions,
34+
log,
35+
}
3336

3437
if (['add', 'a', 'set', 's'].includes(cmdName)) {
3538
return this.add(pkg, tag, opts)

lib/commands/hook.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const hookApi = require('libnpmhook')
22
const otplease = require('../utils/otplease.js')
33
const relativeDate = require('tiny-relative-date')
44
const Table = require('cli-table3')
5+
const log = require('../utils/log-shim.js')
56

67
const BaseCommand = require('../base-command.js')
78
class Hook extends BaseCommand {
@@ -20,7 +21,10 @@ class Hook extends BaseCommand {
2021
]
2122

2223
async exec (args) {
23-
return otplease(this.npm.flatOptions, (opts) => {
24+
return otplease({
25+
...this.npm.flatOptions,
26+
log,
27+
}, (opts) => {
2428
switch (args[0]) {
2529
case 'add':
2630
return this.add(args[1], args[2], args[3], opts)

lib/commands/logout.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Logout extends BaseCommand {
2525
...this.npm.flatOptions,
2626
method: 'DELETE',
2727
ignoreBody: true,
28+
log,
2829
})
2930
} else if (auth.isBasicAuth) {
3031
log.verbose('logout', `clearing user credentials for ${reg}`)

lib/commands/owner.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ class Owner extends BaseCommand {
5757
}
5858

5959
async exec ([action, ...args]) {
60-
const opts = this.npm.flatOptions
60+
const opts = {
61+
...this.npm.flatOptions,
62+
log,
63+
}
6164
switch (action) {
6265
case 'ls':
6366
case 'list':
@@ -195,6 +198,7 @@ class Owner extends BaseCommand {
195198
method: 'PUT',
196199
body,
197200
spec,
201+
log,
198202
})
199203
})
200204

lib/commands/ping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Ping extends BaseCommand {
1010
async exec (args) {
1111
log.notice('PING', this.npm.config.get('registry'))
1212
const start = Date.now()
13-
const details = await pingUtil(this.npm.flatOptions)
13+
const details = await pingUtil({ ...this.npm.flatOptions, log })
1414
const time = Date.now() - start
1515
log.notice('PONG', `${time}ms`)
1616
if (this.npm.config.get('json')) {

lib/commands/profile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Profile extends BaseCommand {
108108
async get (args) {
109109
const tfa = 'two-factor auth'
110110
const info = await pulseTillDone.withPromise(
111-
npmProfile.get(this.npm.flatOptions)
111+
npmProfile.get({ ...this.npm.flatOptions, log })
112112
)
113113

114114
if (!info.cidr_whitelist) {
@@ -170,7 +170,7 @@ class Profile extends BaseCommand {
170170
}
171171

172172
async set (args) {
173-
const conf = this.npm.flatOptions
173+
const conf = { ...this.npm.flatOptions, log }
174174
const prop = (args[0] || '').toLowerCase().trim()
175175

176176
let value = args.length > 1 ? args.slice(1).join(' ') : null
@@ -285,7 +285,7 @@ class Profile extends BaseCommand {
285285
if (auth.basic) {
286286
log.info('profile', 'Updating authentication to bearer token')
287287
const result = await npmProfile.createToken(
288-
auth.basic.password, false, [], this.npm.flatOptions
288+
auth.basic.password, false, [], { ...this.npm.flatOptions, log }
289289
)
290290

291291
if (!result.token) {
@@ -309,7 +309,7 @@ class Profile extends BaseCommand {
309309

310310
log.info('profile', 'Determine if tfa is pending')
311311
const userInfo = await pulseTillDone.withPromise(
312-
npmProfile.get(this.npm.flatOptions)
312+
npmProfile.get({ ...this.npm.flatOptions, log })
313313
)
314314

315315
const conf = { ...this.npm.flatOptions }

lib/commands/publish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Publish extends BaseCommand {
6161
throw new Error('Tag name must not be a valid SemVer range: ' + defaultTag.trim())
6262
}
6363

64-
const opts = { ...this.npm.flatOptions }
64+
const opts = { ...this.npm.flatOptions, log }
6565

6666
// you can publish name@version, ./foo.tgz, etc.
6767
// even though the default is the 'file:.' cwd.

0 commit comments

Comments
 (0)