Skip to content

Commit 71cbd91

Browse files
authoredApr 25, 2024··
fix: hide banner for exec and explore (#7421)
Fixes #7419
1 parent 57ebebf commit 71cbd91

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed
 

‎lib/npm.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,7 @@ class Npm {
9393
}
9494

9595
async load () {
96-
return time.start('npm:load', async () => {
97-
const { exec } = await this.#load()
98-
return {
99-
exec,
100-
command: this.argv.shift(),
101-
args: this.argv,
102-
}
103-
})
96+
return time.start('npm:load', () => this.#load())
10497
}
10598

10699
get loaded () {
@@ -165,7 +158,26 @@ class Npm {
165158

166159
await time.start('npm:load:configload', () => this.config.load())
167160

161+
// npm --versions
162+
if (this.config.get('versions', 'cli')) {
163+
this.argv = ['version']
164+
this.config.set('usage', false, 'cli')
165+
} else {
166+
this.argv = [...this.config.parsedArgv.remain]
167+
}
168+
169+
// Remove first argv since that is our command as typed
170+
// Note that this might not be the actual name of the command
171+
// due to aliases, etc. But we use the raw form of it later
172+
// in user output so it must be preserved as is.
173+
const commandArg = this.argv.shift()
174+
175+
// This is the actual name of the command that will be run or
176+
// undefined if deref could not find a match
177+
const command = deref(commandArg)
178+
168179
await this.#display.load({
180+
command,
169181
loglevel: this.config.get('loglevel'),
170182
stdoutColor: this.color,
171183
stderrColor: this.logColor,
@@ -202,9 +214,10 @@ class Npm {
202214

203215
// note: this MUST be shorter than the actual argv length, because it
204216
// uses the same memory, so node will truncate it if it's too long.
217+
// We time this because setting process.title is slow sometimes but we
218+
// have to do it for security reasons. But still helpful to know how slow it is.
205219
time.start('npm:load:setTitle', () => {
206220
const { parsedArgv: { cooked, remain } } = this.config
207-
this.argv = remain
208221
// Secrets are mostly in configs, so title is set using only the positional args
209222
// to keep those from being leaked. We still do a best effort replaceInfo.
210223
this.#title = ['npm'].concat(replaceInfo(remain)).join(' ').trim()
@@ -244,13 +257,7 @@ class Npm {
244257
log.warn('using --force', 'Recommended protections disabled.')
245258
}
246259

247-
// npm --versions
248-
if (this.config.get('versions', 'cli')) {
249-
this.argv = ['version']
250-
this.config.set('usage', false, 'cli')
251-
}
252-
253-
return { exec: true }
260+
return { exec: true, command: commandArg, args: this.argv }
254261
}
255262

256263
get isShellout () {

‎lib/utils/display.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class Display {
119119
#progress
120120

121121
// options
122+
#command
122123
#levelIndex
123124
#timing
124125
#json
@@ -159,6 +160,7 @@ class Display {
159160
}
160161

161162
async load ({
163+
command,
162164
heading,
163165
json,
164166
loglevel,
@@ -168,6 +170,7 @@ class Display {
168170
timing,
169171
unicode,
170172
}) {
173+
this.#command = command
171174
// get createSupportsColor from chalk directly if this lands
172175
// https://github.com/chalk/chalk/pull/600
173176
const [{ Chalk }, { createSupportsColor }] = await Promise.all([
@@ -272,10 +275,14 @@ class Display {
272275
return
273276
}
274277

275-
// HACK: if it looks like the banner and we are silent do not print it.
276-
// There's no other way to do this right now :(
277-
// eslint-disable-next-line max-len
278-
if (this.#silent && args.length === 1 && args[0].startsWith('\n> ') && args[0].endsWith('\n')) {
278+
// HACK: if it looks like the banner and we are in a state where we hide the
279+
// banner then dont write any output. This hack can be replaced with proc-log.META
280+
const isBanner = args.length === 1 &&
281+
typeof args[0] === 'string' &&
282+
args[0].startsWith('\n> ') &&
283+
args[0].endsWith('\n')
284+
const hideBanner = this.#silent || ['exec', 'explore'].includes(this.#command)
285+
if (isBanner && hideBanner) {
279286
return
280287
}
281288

‎test/lib/docs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ t.test('basic usage', async t => {
7474
// are generated in the following test
7575
const { npm } = await loadMockNpm(t, {
7676
mocks: {
77-
'{LIB}/utils/cmd-list.js': { commands: [] },
77+
'{LIB}/utils/cmd-list.js': { ...cmdList, commands: [] },
7878
},
7979
config: { userconfig: '/some/config/file/.npmrc' },
8080
globals: { process: { platform: 'posix' } },

0 commit comments

Comments
 (0)
Please sign in to comment.