Skip to content

Commit 41facf6

Browse files
wraithgarruyadorno
authored andcommitted
feat(help): refactor npm help/help-search
Lots of dead code removed thanks to streamlining of logic. `npm help` `npm <command>` and `npm help-search` are all now separated concerns, handling their own use cases. `help` calls `help-search` as a last resort, but `npm <command>` no longer tries to wind its way through to `help-search` just to get the basic npm usage displayed. The `did you mean` output has been expanded. It now always suggests top level commands, scripts, and bins, and suggests them in the way they should be called. PR-URL: #2859 Credit: @wraithgar Close: #2859 Reviewed-by: @ruyadorno
1 parent a8d0751 commit 41facf6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+802
-516
lines changed

docs/content/commands/npm-dedupe.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: npm-dedupe
33
section: 1
4-
description: Reduce duplication
4+
description: Reduce duplication in the package tree
55
---
66

77
### Synopsis
@@ -10,7 +10,7 @@ description: Reduce duplication
1010
npm dedupe
1111
npm ddp
1212

13-
aliases: find-dupes, ddp
13+
aliases: ddp
1414
```
1515

1616
### Description
@@ -74,6 +74,7 @@ Using `npm find-dupes` will run the command in `--dry-run` mode.
7474

7575
### See Also
7676

77+
* [npm find-dupes](/cli-commands/find-dupes)
7778
* [npm ls](/cli-commands/ls)
7879
* [npm update](/cli-commands/update)
7980
* [npm install](/cli-commands/install)
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: npm-find-dupes
3+
section: 1
4+
description: Find duplication in the package tree
5+
---
6+
7+
### Synopsis
8+
9+
```bash
10+
npm find-dupes
11+
```
12+
13+
### Description
14+
15+
Runs `npm dedupe` in `--dry-run` mode, making npm only output the
16+
duplications, without actually changing the package tree.
17+
18+
### See Also
19+
20+
* [npm dedupe](/cli-commands/dedupe)
21+
* [npm ls](/cli-commands/ls)
22+
* [npm update](/cli-commands/update)
23+
* [npm install](/cli-commands/install)
24+

docs/content/commands/npm-init.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: npm-init
33
section: 1
4-
description: create a package.json file
4+
description: Create a package.json file
55
---
66

77
### Synopsis

lib/access.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const subcommands = [
2020
]
2121

2222
class Access extends BaseCommand {
23+
static get description () {
24+
return 'Set access level on published packages'
25+
}
26+
2327
static get name () {
2428
return 'access'
2529
}

lib/adduser.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const authTypes = {
99
}
1010

1111
class AddUser extends BaseCommand {
12+
static get description () {
13+
return 'Add a registry user account'
14+
}
15+
1216
static get name () {
1317
return 'adduser'
1418
}

lib/audit.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const auditError = require('./utils/audit-error.js')
55
const BaseCommand = require('./base-command.js')
66

77
class Audit extends BaseCommand {
8+
/* istanbul ignore next - see test/lib/load-all-commands.js */
9+
static get description () {
10+
return 'Run a security audit'
11+
}
12+
813
/* istanbul ignore next - see test/lib/load-all-commands.js */
914
static get name () {
1015
return 'audit'

lib/base-command.js

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ class BaseCommand {
66
this.npm = npm
77
}
88

9+
get name () {
10+
return this.constructor.name
11+
}
12+
13+
get description () {
14+
return this.constructor.description
15+
}
16+
917
get usage () {
1018
let usage = `npm ${this.constructor.name}\n\n`
1119
if (this.constructor.description)

lib/bin.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const envPath = require('./utils/path.js')
22
const BaseCommand = require('./base-command.js')
33

44
class Bin extends BaseCommand {
5+
static get description () {
6+
return 'Display npm bin folder'
7+
}
8+
59
static get name () {
610
return 'bin'
711
}

lib/bugs.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js')
55
const BaseCommand = require('./base-command.js')
66

77
class Bugs extends BaseCommand {
8+
static get description () {
9+
return 'Report bugs for a package in a web browser'
10+
}
11+
812
static get name () {
913
return 'bugs'
1014
}

lib/cache.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const rimraf = promisify(require('rimraf'))
77
const BaseCommand = require('./base-command.js')
88

99
class Cache extends BaseCommand {
10+
static get description () {
11+
return 'Manipulates packages cache'
12+
}
13+
1014
/* istanbul ignore next - see test/lib/load-all-commands.js */
1115
static get name () {
1216
return 'cache'

lib/ci.js

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const removeNodeModules = async where => {
2020
const BaseCommand = require('./base-command.js')
2121

2222
class CI extends BaseCommand {
23+
/* istanbul ignore next - see test/lib/load-all-commands.js */
24+
static get description () {
25+
return 'Install a project with a clean slate'
26+
}
27+
2328
/* istanbul ignore next - see test/lib/load-all-commands.js */
2429
static get name () {
2530
return 'ci'

lib/cli.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ module.exports = (process) => {
4040
npm.load(async er => {
4141
if (er)
4242
return errorHandler(er)
43+
44+
// npm --version=cli
4345
if (npm.config.get('version', 'cli')) {
44-
console.log(npm.version)
46+
npm.output(npm.version)
4547
return errorHandler.exit(0)
4648
}
4749

50+
// npm --versions=cli
4851
if (npm.config.get('versions', 'cli')) {
4952
npm.argv = ['version']
5053
npm.config.set('usage', false, 'cli')
@@ -57,9 +60,20 @@ module.exports = (process) => {
5760
if (impl)
5861
impl(npm.argv, errorHandler)
5962
else {
60-
npm.config.set('usage', false)
61-
npm.argv.unshift(cmd)
62-
npm.commands.help(npm.argv, errorHandler)
63+
try {
64+
// I don't know why this is needed but we get a cb() not called if we
65+
// omit it
66+
npm.log.level = 'silent'
67+
if (cmd) {
68+
const didYouMean = require('./utils/did-you-mean.js')
69+
const suggestions = await didYouMean(npm, cmd)
70+
npm.output(suggestions)
71+
} else
72+
npm.output(npm.usage)
73+
process.exitCode = 1
74+
} catch (err) {
75+
errorHandler(err)
76+
}
6377
}
6478
})
6579
}

lib/completion.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ const BaseCommand = require('./base-command.js')
4646

4747
class Completion extends BaseCommand {
4848
/* istanbul ignore next - see test/lib/load-all-commands.js */
49-
static get name () {
50-
return 'completion'
49+
static get description () {
50+
return 'Tab Completion for npm'
5151
}
5252

5353
/* istanbul ignore next - see test/lib/load-all-commands.js */
54-
static get description () {
55-
return 'npm command completion script. save to ~/.bashrc or ~/.zshrc'
54+
static get name () {
55+
return 'completion'
5656
}
5757

5858
// completion for the completion command

lib/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const publicVar = k => !/^(\/\/[^:]+:)?_/.test(k)
3030

3131
const BaseCommand = require('./base-command.js')
3232
class Config extends BaseCommand {
33+
static get description () {
34+
return 'Manage the npm configuration files'
35+
}
36+
3337
/* istanbul ignore next - see test/lib/load-all-commands.js */
3438
static get name () {
3539
return 'config'

lib/dedupe.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const reifyFinish = require('./utils/reify-finish.js')
55
const BaseCommand = require('./base-command.js')
66

77
class Dedupe extends BaseCommand {
8+
/* istanbul ignore next - see test/lib/load-all-commands.js */
9+
static get description () {
10+
return 'Reduce duplication in the package tree'
11+
}
12+
813
/* istanbul ignore next - see test/lib/load-all-commands.js */
914
static get name () {
1015
return 'dedupe'

lib/deprecate.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const libaccess = require('libnpmaccess')
77
const BaseCommand = require('./base-command.js')
88

99
class Deprecate extends BaseCommand {
10+
static get description () {
11+
return 'Deprecate a version of a package'
12+
}
13+
1014
/* istanbul ignore next - see test/lib/load-all-commands.js */
1115
static get name () {
1216
return 'deprecate'

lib/diff.js

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const readLocalPkg = require('./utils/read-local-package.js')
1212
const BaseCommand = require('./base-command.js')
1313

1414
class Diff extends BaseCommand {
15+
static get description () {
16+
return 'The registry diff command'
17+
}
18+
1519
/* istanbul ignore next - see test/lib/load-all-commands.js */
1620
static get name () {
1721
return 'diff'

lib/dist-tag.js

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const readLocalPkgName = require('./utils/read-local-package.js')
88
const BaseCommand = require('./base-command.js')
99

1010
class DistTag extends BaseCommand {
11+
static get description () {
12+
return 'Modify package distribution tags'
13+
}
14+
1115
/* istanbul ignore next - see test/lib/load-all-commands.js */
1216
static get name () {
1317
return 'dist-tag'

lib/docs.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
const log = require('npmlog')
22
const pacote = require('pacote')
33
const openUrl = require('./utils/open-url.js')
4-
const usageUtil = require('./utils/usage.js')
54
const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js')
65

7-
class Docs {
8-
constructor (npm) {
9-
this.npm = npm
6+
const BaseCommand = require('./base-command.js')
7+
class Docs extends BaseCommand {
8+
/* istanbul ignore next - see test/lib/load-all-commands.js */
9+
static get description () {
10+
return 'Open documentation for a package in a web browser'
11+
}
12+
13+
/* istanbul ignore next - see test/lib/load-all-commands.js */
14+
static get name () {
15+
return 'docs'
1016
}
1117

1218
/* istanbul ignore next - see test/lib/load-all-commands.js */
13-
get usage () {
14-
return usageUtil('docs', 'npm docs [<pkgname> [<pkgname> ...]]')
19+
static get usage () {
20+
return ['[<pkgname> [<pkgname> ...]]']
1521
}
1622

1723
exec (args, cb) {

lib/doctor.js

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ const maskLabel = mask => {
3232

3333
const BaseCommand = require('./base-command.js')
3434
class Doctor extends BaseCommand {
35+
/* istanbul ignore next - see test/lib/load-all-commands.js */
36+
static get description () {
37+
return 'Check your npm environment'
38+
}
39+
3540
/* istanbul ignore next - see test/lib/load-all-commands.js */
3641
static get name () {
3742
return 'doctor'

lib/edit.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const completion = require('./utils/completion/installed-shallow.js')
99
const BaseCommand = require('./base-command.js')
1010

1111
class Edit extends BaseCommand {
12+
static get description () {
13+
return 'Edit an installed package'
14+
}
15+
1216
/* istanbul ignore next - see test/lib/load-all-commands.js */
1317
static get name () {
1418
return 'edit'

lib/exec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ const BaseCommand = require('./base-command.js')
4040

4141
class Exec extends BaseCommand {
4242
/* istanbul ignore next - see test/lib/load-all-commands.js */
43-
static get name () {
44-
return 'exec'
43+
static get description () {
44+
return 'Run a command from a local or remote npm package'
4545
}
4646

4747
/* istanbul ignore next - see test/lib/load-all-commands.js */
48-
static get description () {
49-
return 'Run a command from a local or remote npm package.'
48+
static get name () {
49+
return 'exec'
5050
}
5151

5252
/* istanbul ignore next - see test/lib/load-all-commands.js */

lib/explain.js

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const validName = require('validate-npm-package-name')
88
const BaseCommand = require('./base-command.js')
99

1010
class Explain extends BaseCommand {
11+
static get description () {
12+
return 'Explain installed packages'
13+
}
14+
1115
/* istanbul ignore next - see test/lib/load-all-commands.js */
1216
static get name () {
1317
return 'explain'

lib/explore.js

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const completion = require('./utils/completion/installed-shallow.js')
88
const BaseCommand = require('./base-command.js')
99

1010
class Explore extends BaseCommand {
11+
static get description () {
12+
return 'Browse an installed package'
13+
}
14+
1115
/* istanbul ignore next - see test/lib/load-all-commands.js */
1216
static get name () {
1317
return 'explore'

lib/find-dupes.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
const BaseCommand = require('./base-command.js')
33

44
class FindDupes extends BaseCommand {
5+
/* istanbul ignore next - see test/lib/load-all-commands.js */
6+
static get description () {
7+
return 'Find duplication in the package tree'
8+
}
9+
510
/* istanbul ignore next - see test/lib/load-all-commands.js */
611
static get name () {
712
return 'find-dupes'

lib/fund.js

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const getPrintableName = ({ name, version }) => {
2222
const BaseCommand = require('./base-command.js')
2323

2424
class Fund extends BaseCommand {
25+
/* istanbul ignore next - see test/lib/load-all-commands.js */
26+
static get description () {
27+
return 'Retrieve funding information'
28+
}
29+
2530
/* istanbul ignore next - see test/lib/load-all-commands.js */
2631
static get name () {
2732
return 'fund'

lib/get.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const BaseCommand = require('./base-command.js')
22

33
class Get extends BaseCommand {
4+
/* istanbul ignore next - see test/lib/load-all-commands.js */
5+
static get description () {
6+
return 'Get a value from the npm configuration'
7+
}
8+
49
/* istanbul ignore next - see test/lib/load-all-commands.js */
510
static get name () {
611
return 'get'

0 commit comments

Comments
 (0)