Skip to content

Commit 71777be

Browse files
lukekarrysnlf
authored andcommitted
feat: display publishConfig during config list
Closes: npm/statusboard#417 If the file at `$NPM_CONFIG_PREFIX/package.json` contains a `publishConfig`, the key/value pairs will be printed along with the rest of the output from `npm config ls`. PR-URL: #4146 Credit: @lukekarrys Close: #4146 Reviewed-by: @nlf
1 parent 6b80faa commit 71777be

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

lib/commands/config.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const configDefs = require('../utils/config/index.js')
33

44
const mkdirp = require('mkdirp-infer-owner')
5-
const { dirname } = require('path')
5+
const { dirname, resolve } = require('path')
66
const { promisify } = require('util')
77
const fs = require('fs')
88
const readFile = promisify(fs.readFile)
@@ -11,6 +11,7 @@ const { spawn } = require('child_process')
1111
const { EOL } = require('os')
1212
const ini = require('ini')
1313
const localeCompare = require('@isaacs/string-locale-compare')('en')
14+
const rpj = require('read-package-json-fast')
1415
const log = require('../utils/log-shim.js')
1516

1617
// take an array of `[key, value, k2=v2, k3, v3, ...]` and turn into
@@ -267,6 +268,23 @@ ${defData}
267268
`; HOME = ${process.env.HOME}`,
268269
'; Run `npm config ls -l` to show all defaults.'
269270
)
271+
msg.push('')
272+
}
273+
274+
if (!this.npm.config.get('global')) {
275+
const pkgPath = resolve(this.npm.prefix, 'package.json')
276+
const pkg = await rpj(pkgPath).catch(() => ({}))
277+
278+
if (pkg.publishConfig) {
279+
msg.push(`; "publishConfig" from ${pkgPath}`)
280+
msg.push('; This set of config values will be used at publish-time.', '')
281+
const pkgKeys = Object.keys(pkg.publishConfig).sort(localeCompare)
282+
for (const k of pkgKeys) {
283+
const v = publicVar(k) ? JSON.stringify(pkg.publishConfig[k]) : '(protected)'
284+
msg.push(`${k} = ${v}`)
285+
}
286+
msg.push('')
287+
}
270288
}
271289

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

tap-snapshots/test/lib/commands/config.js.test.cjs

+41
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,44 @@ userconfig = "{HOME}/.npmrc"
342342
; HOME = {HOME}
343343
; Run \`npm config ls -l\` to show all defaults.
344344
`
345+
346+
exports[`test/lib/commands/config.js TAP config list with publishConfig > output matches snapshot 1`] = `
347+
; "cli" config from command line options
348+
349+
cache = "{NPMDIR}/test/lib/commands/tap-testdir-config-config-list-with-publishConfig-sandbox/cache"
350+
prefix = "{LOCALPREFIX}"
351+
userconfig = "{HOME}/.npmrc"
352+
353+
; node bin location = {EXECPATH}
354+
; cwd = {NPMDIR}
355+
; HOME = {HOME}
356+
; Run \`npm config ls -l\` to show all defaults.
357+
358+
; "publishConfig" from {LOCALPREFIX}/package.json
359+
; This set of config values will be used at publish-time.
360+
361+
_authToken = (protected)
362+
registry = "https://some.registry"
363+
; "env" config from environment
364+
365+
; cache = "{NPMDIR}/test/lib/commands/tap-testdir-config-config-list-with-publishConfig-sandbox/cache" ; overridden by cli
366+
global-prefix = "{LOCALPREFIX}"
367+
globalconfig = "{GLOBALPREFIX}/npmrc"
368+
init-module = "{HOME}/.npm-init.js"
369+
local-prefix = "{LOCALPREFIX}"
370+
; prefix = "{LOCALPREFIX}" ; overridden by cli
371+
user-agent = "npm/{NPM-VERSION} node/{NODE-VERSION} {PLATFORM} {ARCH} workspaces/false"
372+
; userconfig = "{HOME}/.npmrc" ; overridden by cli
373+
374+
; "cli" config from command line options
375+
376+
cache = "{NPMDIR}/test/lib/commands/tap-testdir-config-config-list-with-publishConfig-sandbox/cache"
377+
global = true
378+
prefix = "{LOCALPREFIX}"
379+
userconfig = "{HOME}/.npmrc"
380+
381+
; node bin location = {EXECPATH}
382+
; cwd = {NPMDIR}
383+
; HOME = {HOME}
384+
; Run \`npm config ls -l\` to show all defaults.
385+
`

test/lib/commands/config.js

+20
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ t.test('config list --json', async t => {
107107
t.matchSnapshot(sandbox.output, 'output matches snapshot')
108108
})
109109

110+
t.test('config list with publishConfig', async t => {
111+
const temp = t.testdir({
112+
project: {
113+
'package.json': JSON.stringify({
114+
publishConfig: {
115+
registry: 'https://some.registry',
116+
_authToken: 'mytoken',
117+
},
118+
}),
119+
},
120+
})
121+
const project = join(temp, 'project')
122+
123+
const sandbox = new Sandbox(t, { project })
124+
await sandbox.run('config', ['list', ''])
125+
await sandbox.run('config', ['list', '--global'])
126+
127+
t.matchSnapshot(sandbox.output, 'output matches snapshot')
128+
})
129+
110130
t.test('config delete no args', async t => {
111131
const sandbox = new Sandbox(t)
112132

0 commit comments

Comments
 (0)