Skip to content

Commit cc0a2ec

Browse files
d0sbootslukekarrys
authored andcommitted
fix: work better with system manpages (#4610)
In certain edge cases, the glob could find files that manNumberRegex wouldn't match. A possible solution would be to try to bring the two closer, but since globs and regexes are different syntaxes, the two will never be exactly the same. It's always asking for bugs; better to just handle the issue directly. In addition, when npm is installed globally in a system directory, the glob can find other manpages with similar names. For instance "install.1", "init.1", etc. Preferring low-numbered sections isn't enough in these cases; it's also necessary to prefer the pages prefixed with "npm-".
1 parent 5b4cbb2 commit cc0a2ec

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

lib/commands/help.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const BaseCommand = require('../base-command.js')
1111
// We don't currently compress our man pages but if we ever did this would
1212
// seemlessly continue supporting it
1313
const manNumberRegex = /\.(\d+)(\.[^/\\]*)?$/
14+
// Searches for the "npm-" prefix in page names, to prefer those.
15+
const manNpmPrefixRegex = /\/npm-/
1416

1517
class Help extends BaseCommand {
1618
static description = 'Get help on npm'
@@ -61,13 +63,27 @@ class Help extends BaseCommand {
6163
const f = `${manroot}/${manSearch}/?(npm-)${section}.[0-9]*`
6264
let mans = await glob(f)
6365
mans = mans.sort((a, b) => {
64-
// Because of the glob we know the manNumberRegex will pass
65-
const aManNumber = a.match(manNumberRegex)[1]
66-
const bManNumber = b.match(manNumberRegex)[1]
66+
// Prefer the page with an npm prefix, if there's only one.
67+
const aHasPrefix = manNpmPrefixRegex.test(a)
68+
const bHasPrefix = manNpmPrefixRegex.test(b)
69+
if (aHasPrefix !== bHasPrefix) {
70+
return aHasPrefix ? -1 : 1
71+
}
6772

68-
// man number sort first so that 1 aka commands are preferred
69-
if (aManNumber !== bManNumber) {
70-
return aManNumber - bManNumber
73+
// Because the glob is (subtly) different from manNumberRegex,
74+
// we can't rely on it passing.
75+
const aManNumberMatch = a.match(manNumberRegex)
76+
const bManNumberMatch = b.match(manNumberRegex)
77+
if (aManNumberMatch) {
78+
if (!bManNumberMatch) {
79+
return -1
80+
}
81+
// man number sort first so that 1 aka commands are preferred
82+
if (aManNumberMatch[1] !== bManNumberMatch[1]) {
83+
return aManNumberMatch[1] - bManNumberMatch[1]
84+
}
85+
} else if (bManNumberMatch) {
86+
return 1
7187
}
7288

7389
return localeCompare(a, b)

test/lib/commands/help.js

+38
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,41 @@ t.test('npm help with complex installation path finds proper help file', async t
310310

311311
t.match(openUrlArg, /commands(\/|\\)npm-install.html$/, 'attempts to open the correct url')
312312
})
313+
314+
t.test('npm help - prefers npm help pages', async t => {
315+
// Unusual ordering is to get full test coverage of all branches inside the
316+
// sort function.
317+
globResult = [
318+
'/root/man/man6/npm-install.6',
319+
'/root/man/man1/install.1',
320+
'/root/man/man5/npm-install.5',
321+
]
322+
t.teardown(() => {
323+
globResult = globDefaults
324+
spawnBin = null
325+
spawnArgs = null
326+
})
327+
await help.exec(['install'])
328+
329+
t.equal(spawnBin, 'man', 'calls man by default')
330+
t.strictSame(spawnArgs, ['/root/man/man5/npm-install.5'], 'passes the correct arguments')
331+
})
332+
333+
t.test('npm help - works in the presence of strange man pages', async t => {
334+
// Unusual ordering is to get full test coverage of all branches inside the
335+
// sort function.
336+
globResult = [
337+
'/root/man/man6/config.6strange',
338+
'/root/man/man1/config.1',
339+
'/root/man/man5/config.5ssl',
340+
]
341+
t.teardown(() => {
342+
globResult = globDefaults
343+
spawnBin = null
344+
spawnArgs = null
345+
})
346+
await help.exec(['config'])
347+
348+
t.equal(spawnBin, 'man', 'calls man by default')
349+
t.strictSame(spawnArgs, ['/root/man/man1/config.1'], 'passes the correct arguments')
350+
})

0 commit comments

Comments
 (0)