Skip to content

Commit 6a4bcba

Browse files
committedMar 23, 2023
fix: clean up man sorting
glob doesn't sort and returns things in the os specific representation. Also a lot of the edge cases here covered non-reality. Our current man setup already has all of the `npm-` prefixed things in `man1`.
1 parent 8a96b65 commit 6a4bcba

File tree

4 files changed

+24
-39
lines changed

4 files changed

+24
-39
lines changed
 

‎lib/commands/help.js

+4-26
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const BaseCommand = require('../base-command.js')
1111
// We don't currently compress our man pages but if we ever did this would
1212
// seamlessly continue supporting it
1313
const manNumberRegex = /\.(\d+)(\.[^/\\]*)?$/
14-
// Searches for the "npm-" prefix in page names, to prefer those.
15-
const manNpmPrefixRegex = /\/npm-/
1614
// hardcoded names for mansections
1715
// XXX: these are used in the docs workspace and should be exported
1816
// from npm so section names can changed more easily
@@ -66,33 +64,13 @@ class Help extends BaseCommand {
6664
const f = globify(path.resolve(this.npm.npmRoot, `man/${manSearch}/?(npm-)${arg}.[0-9]*`))
6765

6866
const [man] = await glob(f).then(r => r.sort((a, b) => {
69-
// Prefer the page with an npm prefix, if there's only one.
70-
const aHasPrefix = manNpmPrefixRegex.test(a)
71-
const bHasPrefix = manNpmPrefixRegex.test(b)
72-
if (aHasPrefix !== bHasPrefix) {
73-
/* istanbul ignore next */
74-
return aHasPrefix ? -1 : 1
75-
}
76-
7767
// Because the glob is (subtly) different from manNumberRegex,
7868
// we can't rely on it passing.
79-
const aManNumberMatch = a.match(manNumberRegex)
80-
const bManNumberMatch = b.match(manNumberRegex)
81-
if (aManNumberMatch) {
82-
/* istanbul ignore next */
83-
if (!bManNumberMatch) {
84-
return -1
85-
}
86-
// man number sort first so that 1 aka commands are preferred
87-
if (aManNumberMatch[1] !== bManNumberMatch[1]) {
88-
return aManNumberMatch[1] - bManNumberMatch[1]
89-
}
90-
/* istanbul ignore next */
91-
} else if (bManNumberMatch) {
92-
/* istanbul ignore next */
93-
return 1
69+
const aManNumberMatch = a.match(manNumberRegex)?.[1] || 999
70+
const bManNumberMatch = b.match(manNumberRegex)?.[1] || 999
71+
if (aManNumberMatch !== bManNumberMatch) {
72+
return aManNumberMatch - bManNumberMatch
9473
}
95-
9674
return localeCompare(a, b)
9775
}))
9876

‎test/lib/commands/help.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const genManPages = (obj) => {
2929

3030
const mockHelp = async (t, {
3131
man = {
32-
1: ['whoami', 'install', 'star', 'unstar', 'uninstall', 'unpublish'].map(p => `npm-${p}`),
3332
5: ['npmrc', 'install', 'package-json'],
33+
1: ['whoami', 'install', 'star', 'unstar', 'uninstall', 'unpublish'].map(p => `npm-${p}`),
3434
7: ['disputes', 'config'],
3535
},
3636
browser = false,
@@ -113,7 +113,7 @@ t.test('npm help whoami', async t => {
113113
const [spawnBin, spawnArgs] = getArgs()
114114
t.equal(spawnBin, 'man', 'calls man by default')
115115
t.equal(spawnArgs.length, 1)
116-
t.match(spawnArgs[0], /\/man\/man1\/npm-whoami\.1$/)
116+
t.match(spawnArgs[0], /npm-whoami\.1$/)
117117
})
118118

119119
t.test('npm help 1 install', async t => {
@@ -155,7 +155,7 @@ t.test('npm help package.json redirects to package-json', async t => {
155155
const [spawnBin, spawnArgs] = getArgs()
156156
t.equal(spawnBin, 'man', 'calls man by default')
157157
t.equal(spawnArgs.length, 1)
158-
t.match(spawnArgs[0], /\/man\/man5\/package-json\.5$/)
158+
t.match(spawnArgs[0], /package-json\.5$/)
159159
})
160160

161161
t.test('npm help ?(un)star', async t => {
@@ -168,7 +168,7 @@ t.test('npm help ?(un)star', async t => {
168168
t.equal(spawnBin, 'emacsclient', 'maps woman to emacs correctly')
169169
t.equal(spawnArgs.length, 2)
170170
t.match(spawnArgs[1], /^\(woman-find-file '/)
171-
t.match(spawnArgs[1], /\/man\/man1\/npm-star.1'\)$/)
171+
t.match(spawnArgs[1], /npm-star.1'\)$/)
172172
})
173173

174174
t.test('npm help un*', async t => {
@@ -179,39 +179,40 @@ t.test('npm help un*', async t => {
179179
const [spawnBin, spawnArgs] = getArgs()
180180
t.equal(spawnBin, 'man', 'calls man by default')
181181
t.equal(spawnArgs.length, 1)
182-
t.match(spawnArgs[0], /\/man\/man1\/npm-uninstall\.1$/)
182+
t.match(spawnArgs[0], /npm-uninstall\.1$/)
183183
})
184184

185-
t.test('npm help - prefers npm help pages', async t => {
185+
t.test('npm help - prefers lowest numbered npm prefixed help pages', async t => {
186186
const { getArgs } = await mockHelp(t, {
187187
man: {
188188
6: ['npm-install'],
189-
1: ['install'],
190-
5: ['install', 'npm-install'],
189+
1: ['npm-install'],
190+
5: ['install'],
191+
7: ['npm-install'],
191192
},
192193
exec: ['install'],
193194
})
194195

195196
const [spawnBin, spawnArgs] = getArgs()
196197
t.equal(spawnBin, 'man', 'calls man by default')
197198
t.equal(spawnArgs.length, 1)
198-
t.match(spawnArgs[0], /\/man\/man5\/npm-install\.5$/)
199+
t.match(spawnArgs[0], /npm-install\.1$/)
199200
})
200201

201202
t.test('npm help - works in the presence of strange man pages', async t => {
202203
const { getArgs } = await mockHelp(t, {
203204
man: {
204-
'6strange': ['config'],
205-
1: ['config'],
206-
'5ssl': ['config'],
205+
'1strange': ['config'],
206+
5: ['config'],
207+
'6ssl': ['config'],
207208
},
208209
exec: ['config'],
209210
})
210211

211212
const [spawnBin, spawnArgs] = getArgs()
212213
t.equal(spawnBin, 'man', 'calls man by default')
213214
t.equal(spawnArgs.length, 1)
214-
t.match(spawnArgs[0], /\/man\/man1\/config\.1$/)
215+
t.match(spawnArgs[0], /config\.5$/)
215216
})
216217

217218
t.test('rejects with code', async t => {

‎workspaces/config/tap-snapshots/test/type-description.js.test.cjs

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Object {
1010
"_exit": Array [
1111
"boolean value (true or false)",
1212
],
13+
"_single": Array [
14+
Object {
15+
"exotic": "not part of normal typedefs",
16+
},
17+
],
1318
"access": Array [
1419
null,
1520
"restricted",

‎workspaces/config/test/fixtures/types.js

+1
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,5 @@ module.exports = {
148148
versions: Boolean,
149149
viewer: String,
150150
_exit: Boolean,
151+
_single: { exotic: 'not part of normal typedefs' },
151152
}

1 commit comments

Comments
 (1)

keithlee96 commented on Apr 11, 2023

@keithlee96

Thanks for this. It fixed an issue I had with npm@8.5.1

npm help config
npm ERR! Cannot read property '1' of null


Please sign in to comment.