| 44 | } |
| 45 | |
| 46 | async exec (args) { |
| 47 | // By default we search all of our man subdirectories, but if the user has asked for a specific one we limit the search to just there |
| 48 | const manSearch = /^\d+$/.test(args[0]) ? `man${args.shift()}` : 'man*' |
| 49 | |
| 50 | if (!args.length) { |
| 51 | return output.standard(this.npm.usage) |
| 52 | } |
| 53 | |
| 54 | // npm help foo bar baz: search topics |
| 55 | if (args.length > 1) { |
| 56 | return this.helpSearch(args) |
| 57 | } |
| 58 | |
| 59 | // `npm help package.json` |
| 60 | const arg = (deref(args[0]) || args[0]).replace('.json', '-json') |
| 61 | |
| 62 | // find either section.n or npm-section.n |
| 63 | const f = globify(path.resolve(this.npm.npmRoot, `man/${manSearch}/?(npm-)${arg}.[0-9]*`)) |
| 64 | |
| 65 | const [man] = await glob(f).then(r => r.sort((a, b) => { |
| 66 | // Because the glob is (subtly) different from manNumberRegex, we can't rely on it passing. |
| 67 | const aManNumberMatch = a.match(manNumberRegex)?.[1] || 999 |
| 68 | const bManNumberMatch = b.match(manNumberRegex)?.[1] || 999 |
| 69 | if (aManNumberMatch !== bManNumberMatch) { |
| 70 | return aManNumberMatch - bManNumberMatch |
| 71 | } |
| 72 | return localeCompare(a, b) |
| 73 | })) |
| 74 | |
| 75 | return man ? this.viewMan(man) : this.helpSearch(args) |
| 76 | } |
| 77 | |
| 78 | helpSearch (args) { |
| 79 | return this.npm.exec('help-search', args) |