From a60c5a8e016051a44c9d5e2b5ffc6342eee30c07 Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 10 Mar 2021 16:11:34 -0800 Subject: [PATCH] feat(help): refactor npm help/help-search Lots of dead code removed thanks to streamlining of logic. `npm help` `npm ` and `npm help-search` are all now separated concerns, handling their own use cases. `help` calls `help-search` as a last resort, but `npm ` 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. --- docs/content/commands/npm-dedupe.md | 5 +- docs/content/commands/npm-find-dupes.md | 24 +++ docs/content/commands/npm-init.md | 2 +- lib/access.js | 4 + lib/adduser.js | 4 + lib/audit.js | 5 + lib/base-command.js | 8 + lib/bin.js | 4 + lib/bugs.js | 4 + lib/cache.js | 4 + lib/ci.js | 5 + lib/cli.js | 22 +- lib/completion.js | 8 +- lib/config.js | 4 + lib/dedupe.js | 5 + lib/deprecate.js | 4 + lib/diff.js | 4 + lib/dist-tag.js | 4 + lib/docs.js | 18 +- lib/doctor.js | 5 + lib/edit.js | 4 + lib/exec.js | 8 +- lib/explain.js | 4 + lib/explore.js | 4 + lib/find-dupes.js | 5 + lib/fund.js | 5 + lib/get.js | 5 + lib/help-search.js | 24 +-- lib/help.js | 177 +++++----------- lib/hook.js | 4 + lib/init.js | 5 + lib/install-ci-test.js | 4 + lib/install-test.js | 4 + lib/install.js | 5 + lib/link.js | 5 + lib/logout.js | 5 + lib/ls.js | 5 + lib/npm.js | 7 +- lib/org.js | 4 + lib/outdated.js | 5 + lib/owner.js | 4 + lib/pack.js | 5 + lib/ping.js | 8 +- lib/prefix.js | 5 + lib/profile.js | 4 + lib/prune.js | 5 + lib/publish.js | 4 + lib/rebuild.js | 5 + lib/repo.js | 5 + lib/restart.js | 5 + lib/root.js | 5 + lib/run-script.js | 10 +- lib/search.js | 5 + lib/set-script.js | 5 + lib/set.js | 4 + lib/shrinkwrap.js | 5 + lib/star.js | 4 + lib/stars.js | 5 + lib/start.js | 5 + lib/stop.js | 5 + lib/team.js | 4 + lib/test.js | 5 + lib/token.js | 4 + lib/uninstall.js | 4 + lib/unpublish.js | 4 + lib/unstar.js | 5 + lib/update.js | 5 + lib/utils/did-you-mean.js | 35 ++- lib/utils/npm-usage.js | 25 +-- lib/version.js | 4 + lib/view.js | 5 + lib/whoami.js | 8 +- .../test-lib-dist-tag.js-TAP.test.js | 10 + tap-snapshots/test-lib-publish.js-TAP.test.js | 2 + .../test-lib-utils-npm-usage.js-TAP.test.js | 200 +++++++++++++----- test/lib/cli.js | 127 ++++++----- test/lib/help-search.js | 54 +---- test/lib/help.js | 88 ++------ test/lib/load-all-commands.js | 52 +++-- test/lib/npm.js | 1 + test/lib/run-script.js | 26 ++- test/lib/utils/did-you-mean.js | 34 ++- test/lib/utils/npm-usage.js | 63 +----- 83 files changed, 792 insertions(+), 508 deletions(-) create mode 100644 docs/content/commands/npm-find-dupes.md diff --git a/docs/content/commands/npm-dedupe.md b/docs/content/commands/npm-dedupe.md index 9b14e99dd14f0..c6d26126d3077 100644 --- a/docs/content/commands/npm-dedupe.md +++ b/docs/content/commands/npm-dedupe.md @@ -1,7 +1,7 @@ --- title: npm-dedupe section: 1 -description: Reduce duplication +description: Reduce duplication in the package tree --- ### Synopsis @@ -10,7 +10,7 @@ description: Reduce duplication npm dedupe npm ddp -aliases: find-dupes, ddp +aliases: ddp ``` ### Description @@ -74,6 +74,7 @@ Using `npm find-dupes` will run the command in `--dry-run` mode. ### See Also +* [npm find-dupes](/cli-commands/find-dupes) * [npm ls](/cli-commands/ls) * [npm update](/cli-commands/update) * [npm install](/cli-commands/install) diff --git a/docs/content/commands/npm-find-dupes.md b/docs/content/commands/npm-find-dupes.md new file mode 100644 index 0000000000000..6f55d47bfd7f3 --- /dev/null +++ b/docs/content/commands/npm-find-dupes.md @@ -0,0 +1,24 @@ +--- +title: npm-find-dupes +section: 1 +description: Find duplication in the package tree +--- + +### Synopsis + +```bash +npm find-dupes +``` + +### Description + +Runs `npm dedupe` in `--dry-run` mode, making npm only output the +duplications, without actually changing the package tree. + +### See Also + +* [npm dedupe](/cli-commands/dedupe) +* [npm ls](/cli-commands/ls) +* [npm update](/cli-commands/update) +* [npm install](/cli-commands/install) + diff --git a/docs/content/commands/npm-init.md b/docs/content/commands/npm-init.md index 8a40d90e8354d..4b0b8c4c43e73 100644 --- a/docs/content/commands/npm-init.md +++ b/docs/content/commands/npm-init.md @@ -1,7 +1,7 @@ --- title: npm-init section: 1 -description: create a package.json file +description: Create a package.json file --- ### Synopsis diff --git a/lib/access.js b/lib/access.js index 0df36beeac15f..8411ce0e8258a 100644 --- a/lib/access.js +++ b/lib/access.js @@ -20,6 +20,10 @@ const subcommands = [ ] class Access extends BaseCommand { + static get description () { + return 'Set access level on published packages' + } + static get name () { return 'access' } diff --git a/lib/adduser.js b/lib/adduser.js index da318a1f3feb8..f761ae38664b2 100644 --- a/lib/adduser.js +++ b/lib/adduser.js @@ -9,6 +9,10 @@ const authTypes = { } class AddUser extends BaseCommand { + static get description () { + return 'Add a registry user account' + } + static get name () { return 'adduser' } diff --git a/lib/audit.js b/lib/audit.js index 6e64987b612ae..87a003698942e 100644 --- a/lib/audit.js +++ b/lib/audit.js @@ -5,6 +5,11 @@ const auditError = require('./utils/audit-error.js') const BaseCommand = require('./base-command.js') class Audit extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Run a security audit' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'audit' diff --git a/lib/base-command.js b/lib/base-command.js index 8e48caa2ef4c7..b8497fd448780 100644 --- a/lib/base-command.js +++ b/lib/base-command.js @@ -6,6 +6,14 @@ class BaseCommand { this.npm = npm } + get name () { + return this.constructor.name + } + + get description () { + return this.constructor.description + } + get usage () { let usage = `npm ${this.constructor.name}\n\n` if (this.constructor.description) diff --git a/lib/bin.js b/lib/bin.js index 1450fb539bffa..e5a1878961731 100644 --- a/lib/bin.js +++ b/lib/bin.js @@ -2,6 +2,10 @@ const envPath = require('./utils/path.js') const BaseCommand = require('./base-command.js') class Bin extends BaseCommand { + static get description () { + return 'Display npm bin folder' + } + static get name () { return 'bin' } diff --git a/lib/bugs.js b/lib/bugs.js index 1814dd7bc461e..a0cef4c5ec4fa 100644 --- a/lib/bugs.js +++ b/lib/bugs.js @@ -5,6 +5,10 @@ const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js') const BaseCommand = require('./base-command.js') class Bugs extends BaseCommand { + static get description () { + return 'Report bugs for a package in a web browser' + } + static get name () { return 'bugs' } diff --git a/lib/cache.js b/lib/cache.js index 80a5c68ebc0e9..3da76139e2935 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -7,6 +7,10 @@ const rimraf = promisify(require('rimraf')) const BaseCommand = require('./base-command.js') class Cache extends BaseCommand { + static get description () { + return 'Manipulates packages cache' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'cache' diff --git a/lib/ci.js b/lib/ci.js index 3ea19937616e6..481a76a61eb6f 100644 --- a/lib/ci.js +++ b/lib/ci.js @@ -20,6 +20,11 @@ const removeNodeModules = async where => { const BaseCommand = require('./base-command.js') class CI extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Install a project with a clean slate' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'ci' diff --git a/lib/cli.js b/lib/cli.js index 910b674eaa790..837d0876ceb4a 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -40,11 +40,14 @@ module.exports = (process) => { npm.load(async er => { if (er) return errorHandler(er) + + // npm --version=cli if (npm.config.get('version', 'cli')) { - console.log(npm.version) + npm.output(npm.version) return errorHandler.exit(0) } + // npm --versions=cli if (npm.config.get('versions', 'cli')) { npm.argv = ['version'] npm.config.set('usage', false, 'cli') @@ -57,9 +60,20 @@ module.exports = (process) => { if (impl) impl(npm.argv, errorHandler) else { - npm.config.set('usage', false) - npm.argv.unshift(cmd) - npm.commands.help(npm.argv, errorHandler) + try { + // I don't know why this is needed but we get a cb() not called if we + // omit it + npm.log.level = 'silent' + if (cmd) { + const didYouMean = require('./utils/did-you-mean.js') + const suggestions = await didYouMean(npm, cmd) + npm.output(suggestions) + } else + npm.output(npm.usage) + process.exitCode = 1 + } catch (err) { + errorHandler(err) + } } }) } diff --git a/lib/completion.js b/lib/completion.js index 3ee68cdacaf95..da34f1bb76154 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -46,13 +46,13 @@ const BaseCommand = require('./base-command.js') class Completion extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - static get name () { - return 'completion' + static get description () { + return 'Tab Completion for npm' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - static get description () { - return 'npm command completion script. save to ~/.bashrc or ~/.zshrc' + static get name () { + return 'completion' } // completion for the completion command diff --git a/lib/config.js b/lib/config.js index c29253e430a33..0b596c759fc46 100644 --- a/lib/config.js +++ b/lib/config.js @@ -29,6 +29,10 @@ const publicVar = k => !/^(\/\/[^:]+:)?_/.test(k) const BaseCommand = require('./base-command.js') class Config extends BaseCommand { + static get description () { + return 'Manage the npm configuration files' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'config' diff --git a/lib/dedupe.js b/lib/dedupe.js index 50a56211fc847..1c8ccc9b81801 100644 --- a/lib/dedupe.js +++ b/lib/dedupe.js @@ -5,6 +5,11 @@ const reifyFinish = require('./utils/reify-finish.js') const BaseCommand = require('./base-command.js') class Dedupe extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Reduce duplication in the package tree' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'dedupe' diff --git a/lib/deprecate.js b/lib/deprecate.js index a0c67f805d2f9..c640bcfe09538 100644 --- a/lib/deprecate.js +++ b/lib/deprecate.js @@ -7,6 +7,10 @@ const libaccess = require('libnpmaccess') const BaseCommand = require('./base-command.js') class Deprecate extends BaseCommand { + static get description () { + return 'Deprecate a version of a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'deprecate' diff --git a/lib/diff.js b/lib/diff.js index 0e322ec643849..294e8d4cbd2df 100644 --- a/lib/diff.js +++ b/lib/diff.js @@ -12,6 +12,10 @@ const readLocalPkg = require('./utils/read-local-package.js') const BaseCommand = require('./base-command.js') class Diff extends BaseCommand { + static get description () { + return 'The registry diff command' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'diff' diff --git a/lib/dist-tag.js b/lib/dist-tag.js index cdc95ac6f0cd7..33af7067fbc0d 100644 --- a/lib/dist-tag.js +++ b/lib/dist-tag.js @@ -8,6 +8,10 @@ const readLocalPkgName = require('./utils/read-local-package.js') const BaseCommand = require('./base-command.js') class DistTag extends BaseCommand { + static get description () { + return 'Modify package distribution tags' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'dist-tag' diff --git a/lib/docs.js b/lib/docs.js index 2dad7a26db4e7..089d77eb046d9 100644 --- a/lib/docs.js +++ b/lib/docs.js @@ -1,17 +1,23 @@ const log = require('npmlog') const pacote = require('pacote') const openUrl = require('./utils/open-url.js') -const usageUtil = require('./utils/usage.js') const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js') -class Docs { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Docs extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Open documentation for a package in a web browser' + } + + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'docs' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('docs', 'npm docs [ [ ...]]') + static get usage () { + return ['[ [ ...]]'] } exec (args, cb) { diff --git a/lib/doctor.js b/lib/doctor.js index fbe44714140aa..6a4f1a76bbcd4 100644 --- a/lib/doctor.js +++ b/lib/doctor.js @@ -32,6 +32,11 @@ const maskLabel = mask => { const BaseCommand = require('./base-command.js') class Doctor extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Check your npm environment' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'doctor' diff --git a/lib/edit.js b/lib/edit.js index 1dbe8e4c103ad..79d41462eb47f 100644 --- a/lib/edit.js +++ b/lib/edit.js @@ -9,6 +9,10 @@ const completion = require('./utils/completion/installed-shallow.js') const BaseCommand = require('./base-command.js') class Edit extends BaseCommand { + static get description () { + return 'Edit an installed package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'edit' diff --git a/lib/exec.js b/lib/exec.js index b2443b17accd2..aca4468b264dd 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -40,13 +40,13 @@ const BaseCommand = require('./base-command.js') class Exec extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - static get name () { - return 'exec' + static get description () { + return 'Run a command from a local or remote npm package' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - static get description () { - return 'Run a command from a local or remote npm package.' + static get name () { + return 'exec' } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/explain.js b/lib/explain.js index 6af7611867786..4c2908df68918 100644 --- a/lib/explain.js +++ b/lib/explain.js @@ -8,6 +8,10 @@ const validName = require('validate-npm-package-name') const BaseCommand = require('./base-command.js') class Explain extends BaseCommand { + static get description () { + return 'Explain installed packages' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'explain' diff --git a/lib/explore.js b/lib/explore.js index 34f6d10793c7e..5f9820c2162b6 100644 --- a/lib/explore.js +++ b/lib/explore.js @@ -8,6 +8,10 @@ const completion = require('./utils/completion/installed-shallow.js') const BaseCommand = require('./base-command.js') class Explore extends BaseCommand { + static get description () { + return 'Browse an installed package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'explore' diff --git a/lib/find-dupes.js b/lib/find-dupes.js index ecb945f47bb41..8037876f5bbb6 100644 --- a/lib/find-dupes.js +++ b/lib/find-dupes.js @@ -2,6 +2,11 @@ const BaseCommand = require('./base-command.js') class FindDupes extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Find duplication in the package tree' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'find-dupes' diff --git a/lib/fund.js b/lib/fund.js index a723c62d2c3c5..1600508fb6403 100644 --- a/lib/fund.js +++ b/lib/fund.js @@ -22,6 +22,11 @@ const getPrintableName = ({ name, version }) => { const BaseCommand = require('./base-command.js') class Fund extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Retrieve funding information' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'fund' diff --git a/lib/get.js b/lib/get.js index a5d58accc8307..8cfb259a81323 100644 --- a/lib/get.js +++ b/lib/get.js @@ -1,6 +1,11 @@ const BaseCommand = require('./base-command.js') class Get extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Get a value from the npm configuration' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'get' diff --git a/lib/help-search.js b/lib/help-search.js index 4e727c3e72954..83c30fb6855c2 100644 --- a/lib/help-search.js +++ b/lib/help-search.js @@ -1,15 +1,16 @@ const fs = require('fs') const path = require('path') const color = require('ansicolors') -const npmUsage = require('./utils/npm-usage.js') const { promisify } = require('util') const glob = promisify(require('glob')) const readFile = promisify(fs.readFile) -const didYouMean = require('./utils/did-you-mean.js') -const { cmdList } = require('./utils/cmd-list.js') const BaseCommand = require('./base-command.js') class HelpSearch extends BaseCommand { + static get description () { + return 'Search npm help documentation' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'help-search' @@ -26,28 +27,17 @@ class HelpSearch extends BaseCommand { async helpSearch (args) { if (!args.length) - throw this.usage + return this.npm.output(this.usage) const docPath = path.resolve(__dirname, '..', 'docs/content') - const files = await glob(`${docPath}/*/*.md`) const data = await this.readFiles(files) const results = await this.searchFiles(args, data, files) - // if only one result, then just show that help section. - if (results.length === 1) { - return this.npm.commands.help([path.basename(results[0].file, '.md')], er => { - if (er) - throw er - }) - } - const formatted = this.formatResults(args, results) if (!formatted.trim()) - npmUsage(this.npm, false) - else { + this.npm.output(`No matches in help for: ${args.join(' ')}\n`) + else this.npm.output(formatted) - this.npm.output(didYouMean(args[0], cmdList)) - } } async readFiles (files) { diff --git a/lib/help.js b/lib/help.js index 93abf878ba26f..b9ff1c95760c6 100644 --- a/lib/help.js +++ b/lib/help.js @@ -1,13 +1,22 @@ -const npmUsage = require('./utils/npm-usage.js') const { spawn } = require('child_process') const path = require('path') -const log = require('npmlog') const openUrl = require('./utils/open-url.js') -const glob = require('glob') +const { promisify } = require('util') +const glob = promisify(require('glob')) const BaseCommand = require('./base-command.js') +// Strips out the number from foo.7 or foo.7. or foo.7.tgz +// We don't currently compress our man pages but if we ever did this would +// seemlessly continue supporting it +const manNumberRegex = /\.(\d+)(\..*)?$/ + class Help extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Get help on npm' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'help' @@ -22,13 +31,7 @@ class Help extends BaseCommand { if (opts.conf.argv.remain.length > 2) return [] const g = path.resolve(__dirname, '../man/man[0-9]/*.[0-9]') - const files = await new Promise((resolve, reject) => { - glob(g, function (er, files) { - if (er) - return reject(er) - resolve(files) - }) - }) + const files = await glob(g) return Object.keys(files.reduce(function (acc, file) { file = path.basename(file).replace(/\.[0-9]+$/, '') @@ -43,81 +46,45 @@ class Help extends BaseCommand { } async help (args) { - const argv = this.npm.config.parsedArgv.cooked + // 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 + let manSearch = 'man*' + if (/^\d+$/.test(args[0])) + manSearch = `man${args.shift()}` - let argnum = 0 - if (args.length === 2 && ~~args[0]) - argnum = ~~args.shift() + if (!args.length) + return this.npm.output(this.npm.usage) // npm help foo bar baz: search topics - if (args.length > 1 && args[0]) + if (args.length > 1) return this.helpSearch(args) - const affordances = { - 'find-dupes': 'dedupe', - } - let section = affordances[args[0]] || this.npm.deref(args[0]) || args[0] - - // npm help : show basic usage - if (!section) { - npmUsage(this.npm, argv[0] === 'help') - return - } - - // npm -h: show command usage - if (this.npm.config.get('usage') && - this.npm.commands[section] && - this.npm.commands[section].usage) { - this.npm.config.set('loglevel', 'silent') - log.level = 'silent' - this.npm.output(this.npm.commands[section].usage) - return - } + let section = this.npm.deref(args[0]) || args[0] - let pref = [1, 5, 7] - if (argnum) - pref = [argnum].concat(pref.filter(n => n !== argnum)) + // support `npm help package.json` + section = section.replace('.json', '-json') - // npm help
: Try to find the path const manroot = path.resolve(__dirname, '..', 'man') + // find either section.n or npm-section.n + const f = `${manroot}/${manSearch}/?(npm-)${section}.[0-9]*` + let mans = await glob(f) + mans = mans.sort((a, b) => { + // Because of the glob we know the manNumberRegex will pass + const aManNumber = a.match(manNumberRegex)[1] + const bManNumber = b.match(manNumberRegex)[1] - // legacy - if (section === 'global') - section = 'folders' - else if (section.match(/.*json/)) - section = section.replace('.json', '-json') - - // find either /section.n or /npm-section.n - // The glob is used in the glob. The regexp is used much - // further down. Globs and regexps are different - const compextglob = '.+(gz|bz2|lzma|[FYzZ]|xz)' - const compextre = '\\.(gz|bz2|lzma|[FYzZ]|xz)$' - const f = '+(npm-' + section + '|' + section + ').[0-9]?(' + compextglob + ')' - return new Promise((resolve, reject) => { - glob(manroot + '/*/' + f, async (er, mans) => { - if (er) - return reject(er) - - if (!mans.length) { - this.helpSearch(args).then(resolve).catch(reject) - return - } - - mans = mans.map((man) => { - const ext = path.extname(man) - if (man.match(new RegExp(compextre))) - man = path.basename(man, ext) - - return man - }) - - this.viewMan(this.pickMan(mans, pref), (err) => { - if (err) - return reject(err) - return resolve() - }) - }) + // man number sort first so that 1 aka commands are preferred + if (aManNumber !== bManNumber) + return aManNumber - bManNumber + + return a.localeCompare(b) }) + const man = mans[0] + + if (man) + await this.viewMan(man) + else + return this.helpSearch(args) } helpSearch (args) { @@ -133,32 +100,11 @@ class Help extends BaseCommand { }) } - pickMan (mans, pref_) { - const nre = /([0-9]+)$/ - const pref = {} - pref_.forEach((sect, i) => pref[sect] = i) - mans = mans.sort((a, b) => { - const an = a.match(nre)[1] - const bn = b.match(nre)[1] - return an === bn ? (a > b ? -1 : 1) - : pref[an] < pref[bn] ? -1 - : 1 - }) - return mans[0] - } - - viewMan (man, cb) { - const nre = /([0-9]+)$/ - const num = man.match(nre)[1] - const section = path.basename(man, '.' + num) - - // at this point, we know that the specified man page exists - const manpath = path.join(__dirname, '..', 'man') + async viewMan (man) { const env = {} Object.keys(process.env).forEach(function (i) { env[i] = process.env[i] }) - env.MANPATH = manpath const viewer = this.npm.config.get('viewer') const opts = { @@ -175,48 +121,39 @@ class Help extends BaseCommand { break case 'browser': - bin = false - try { - const url = this.htmlMan(man) - openUrl(this.npm, url, 'help available at the following URL').then( - () => cb() - ).catch(cb) - } catch (err) { - cb(err) - } - break + await openUrl(this.npm, this.htmlMan(man), 'help available at the following URL') + return default: - args.push(num, section) + args.push(man) break } - if (bin) { - const proc = spawn(bin, args, opts) + const proc = spawn(bin, args, opts) + return new Promise((resolve, reject) => { proc.on('exit', (code) => { if (code) - return cb(new Error(`help process exited with code: ${code}`)) + return reject(new Error(`help process exited with code: ${code}`)) - return cb() + return resolve() }) - } + }) } + // Returns the path to the html version of the man page htmlMan (man) { - let sect = +man.match(/([0-9]+)$/)[1] - const f = path.basename(man).replace(/[.]([0-9]+)$/, '') + let sect = man.match(manNumberRegex)[1] + const f = path.basename(man).replace(manNumberRegex, '') switch (sect) { - case 1: + case '1': sect = 'commands' break - case 5: + case '5': sect = 'configuring-npm' break - case 7: + case '7': sect = 'using-npm' break - default: - throw new Error('invalid man section: ' + sect) } return 'file://' + path.resolve(__dirname, '..', 'docs', 'output', sect, f + '.html') } diff --git a/lib/hook.js b/lib/hook.js index 6cda3504f43d7..64b1201cbd0de 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -5,6 +5,10 @@ const Table = require('cli-table3') const BaseCommand = require('./base-command.js') class Hook extends BaseCommand { + static get description () { + return 'Manage registry hooks' + } + static get name () { return 'hook' } diff --git a/lib/init.js b/lib/init.js index 42b02dfdc6a77..3ad32023b3e3f 100644 --- a/lib/init.js +++ b/lib/init.js @@ -4,6 +4,11 @@ const npa = require('npm-package-arg') const BaseCommand = require('./base-command.js') class Init extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Create a package.json file' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'init' diff --git a/lib/install-ci-test.js b/lib/install-ci-test.js index c52b5c9e8073f..871f24b2f32d6 100644 --- a/lib/install-ci-test.js +++ b/lib/install-ci-test.js @@ -4,6 +4,10 @@ const CI = require('./ci.js') class InstallCITest extends CI { + static get description () { + return 'Install a project with a clean slate and run tests' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'install-ci-test' diff --git a/lib/install-test.js b/lib/install-test.js index 76c6f367dd3c8..d5664119df5ce 100644 --- a/lib/install-test.js +++ b/lib/install-test.js @@ -4,6 +4,10 @@ const Install = require('./install.js') class InstallTest extends Install { + static get description () { + return 'Install package(s) and run tests' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'install-test' diff --git a/lib/install.js b/lib/install.js index 8df63a219ef74..9b4f3142082b0 100644 --- a/lib/install.js +++ b/lib/install.js @@ -11,6 +11,11 @@ const runScript = require('@npmcli/run-script') const BaseCommand = require('./base-command.js') class Install extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Install a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'install' diff --git a/lib/link.js b/lib/link.js index 66f83d9f5b0a7..6de53e082f2e4 100644 --- a/lib/link.js +++ b/lib/link.js @@ -12,6 +12,11 @@ const reifyFinish = require('./utils/reify-finish.js') const BaseCommand = require('./base-command.js') class Link extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Symlink a package folder' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'link' diff --git a/lib/logout.js b/lib/logout.js index b3f64f671d326..19621a8d7fa24 100644 --- a/lib/logout.js +++ b/lib/logout.js @@ -4,6 +4,11 @@ const npmFetch = require('npm-registry-fetch') const BaseCommand = require('./base-command.js') class Logout extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Log out of the registry' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'logout' diff --git a/lib/ls.js b/lib/ls.js index 9ff2761c2f928..fc7a55dbeaae0 100644 --- a/lib/ls.js +++ b/lib/ls.js @@ -23,6 +23,11 @@ const _type = Symbol('type') const BaseCommand = require('./base-command.js') class LS extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'List installed packages' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'ls' diff --git a/lib/npm.js b/lib/npm.js index 0534e630606e4..dacf0c8d39fa4 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -38,6 +38,7 @@ const proxyCmds = new Proxy({}, { const { types, defaults, shorthands } = require('./utils/config.js') const { shellouts } = require('./utils/cmd-list.js') +const usage = require('./utils/npm-usage.js') let warnedNonDashArg = false const _runCmd = Symbol('_runCmd') @@ -106,7 +107,7 @@ const npm = module.exports = new class extends EventEmitter { } if (this.config.get('usage')) { - console.log(impl.usage) + this.output(impl.usage) cb() } else { impl.exec(args, er => { @@ -274,6 +275,10 @@ const npm = module.exports = new class extends EventEmitter { this[k] = r } + get usage () { + return usage(this) + } + // XXX add logging to see if we actually use this get tmp () { if (!this[_tmpFolder]) { diff --git a/lib/org.js b/lib/org.js index b9f84b060f8a8..ddd2b03daee18 100644 --- a/lib/org.js +++ b/lib/org.js @@ -4,6 +4,10 @@ const Table = require('cli-table3') const BaseCommand = require('./base-command.js') class Org extends BaseCommand { + static get description () { + return 'Manage orgs' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'org' diff --git a/lib/outdated.js b/lib/outdated.js index 7225577ea42d4..e9999f5143b84 100644 --- a/lib/outdated.js +++ b/lib/outdated.js @@ -13,6 +13,11 @@ const ansiTrim = require('./utils/ansi-trim.js') const BaseCommand = require('./base-command.js') class Outdated extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Check for outdated packages' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'outdated' diff --git a/lib/owner.js b/lib/owner.js index b62f125ac3bb6..e537d82d01d8d 100644 --- a/lib/owner.js +++ b/lib/owner.js @@ -8,6 +8,10 @@ const readLocalPkg = require('./utils/read-local-package.js') const BaseCommand = require('./base-command.js') class Owner extends BaseCommand { + static get description () { + return 'Manage package owners' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'owner' diff --git a/lib/pack.js b/lib/pack.js index 326fcc0cd1441..cc8a20fe832f5 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -11,6 +11,11 @@ const writeFile = util.promisify(require('fs').writeFile) const BaseCommand = require('./base-command.js') class Pack extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Create a tarball from a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'pack' diff --git a/lib/ping.js b/lib/ping.js index e60b1f1debd80..264823292ac0e 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -4,13 +4,13 @@ const BaseCommand = require('./base-command.js') class Ping extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - static get name () { - return 'ping' + static get description () { + return 'Ping npm registry' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - static get description () { - return 'ping registry' + static get name () { + return 'ping' } exec (args, cb) { diff --git a/lib/prefix.js b/lib/prefix.js index 5ade87f6429f6..1298a3c5ba54b 100644 --- a/lib/prefix.js +++ b/lib/prefix.js @@ -1,6 +1,11 @@ const BaseCommand = require('./base-command.js') class Prefix extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Display prefix' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'prefix' diff --git a/lib/profile.js b/lib/profile.js index 1c0df49888540..49d316d4b7c3f 100644 --- a/lib/profile.js +++ b/lib/profile.js @@ -38,6 +38,10 @@ const writableProfileKeys = [ const BaseCommand = require('./base-command.js') class Profile extends BaseCommand { + static get description () { + return 'Change settings on your registry profile' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'profile' diff --git a/lib/prune.js b/lib/prune.js index c2cddb1a22b33..e86d5c05314bb 100644 --- a/lib/prune.js +++ b/lib/prune.js @@ -4,6 +4,11 @@ const reifyFinish = require('./utils/reify-finish.js') const BaseCommand = require('./base-command.js') class Prune extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Remove extraneous packages' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'prune' diff --git a/lib/publish.js b/lib/publish.js index f8e0eafe11886..1f9d840e69ae0 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -19,6 +19,10 @@ const readJson = util.promisify(require('read-package-json')) const BaseCommand = require('./base-command.js') class Publish extends BaseCommand { + static get description () { + return 'Publish a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'publish' diff --git a/lib/rebuild.js b/lib/rebuild.js index 74f5ae5f6eba5..1d08e9a6f61c8 100644 --- a/lib/rebuild.js +++ b/lib/rebuild.js @@ -6,6 +6,11 @@ const completion = require('./utils/completion/installed-deep.js') const BaseCommand = require('./base-command.js') class Rebuild extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Rebuild a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'rebuild' diff --git a/lib/repo.js b/lib/repo.js index aa07e07a819f7..5ab136abd73d8 100644 --- a/lib/repo.js +++ b/lib/repo.js @@ -7,6 +7,11 @@ const openUrl = require('./utils/open-url.js') const BaseCommand = require('./base-command.js') class Repo extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Open package repository page in the browser' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'repo' diff --git a/lib/restart.js b/lib/restart.js index 1f3eb5af94f82..840eb20673b1f 100644 --- a/lib/restart.js +++ b/lib/restart.js @@ -2,6 +2,11 @@ const LifecycleCmd = require('./utils/lifecycle-cmd.js') // This ends up calling run-script(['restart', ...args]) class Restart extends LifecycleCmd { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Restart a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'restart' diff --git a/lib/root.js b/lib/root.js index 1fe82c6fad773..d24a2e20a99de 100644 --- a/lib/root.js +++ b/lib/root.js @@ -1,5 +1,10 @@ const BaseCommand = require('./base-command.js') class Root extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Display npm root' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'root' diff --git a/lib/run-script.js b/lib/run-script.js index 3ea85b79ffd18..8efc5a6f2ff6e 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -19,6 +19,11 @@ const cmdList = [ const BaseCommand = require('./base-command.js') class RunScript extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Run arbitrary package scripts' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'run-script' @@ -68,9 +73,8 @@ class RunScript extends BaseCommand { if (this.npm.config.get('if-present')) return - const suggestions = didYouMean(event, Object.keys(scripts)) - throw new Error(`missing script: ${event}${ - suggestions ? `\n${suggestions}` : ''}`) + const suggestions = await didYouMean(this.npm, event) + throw new Error(suggestions) } // positional args only added to the main event, not pre/post diff --git a/lib/search.js b/lib/search.js index c24000156f67a..3d2889d992601 100644 --- a/lib/search.js +++ b/lib/search.js @@ -26,6 +26,11 @@ function prepareExcludes (searchexclude) { const BaseCommand = require('./base-command.js') class Search extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Search for pacakges' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'search' diff --git a/lib/set-script.js b/lib/set-script.js index 6241981323c4a..df101a0acb709 100644 --- a/lib/set-script.js +++ b/lib/set-script.js @@ -5,6 +5,11 @@ const rpj = require('read-package-json-fast') const BaseCommand = require('./base-command.js') class SetScript extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Set tasks in the scripts section of package.json' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'set-script' diff --git a/lib/set.js b/lib/set.js index 787a8012c0b6a..74a002cd638be 100644 --- a/lib/set.js +++ b/lib/set.js @@ -1,6 +1,10 @@ const BaseCommand = require('./base-command.js') class Set extends BaseCommand { + static get description () { + return 'Set a value in the npm configuration' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'set' diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index b52cf41957b2d..bd6ed208ee863 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -7,6 +7,11 @@ const log = require('npmlog') const BaseCommand = require('./base-command.js') class Shrinkwrap extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Lock down dependency versions for publication' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'shrinkwrap' diff --git a/lib/star.js b/lib/star.js index 27a3041906a40..3ff234a103047 100644 --- a/lib/star.js +++ b/lib/star.js @@ -6,6 +6,10 @@ const getIdentity = require('./utils/get-identity') const BaseCommand = require('./base-command.js') class Star extends BaseCommand { + static get description () { + return 'Mark your favorite packages' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'star' diff --git a/lib/stars.js b/lib/stars.js index 758a3130d423c..db93d7b19610a 100644 --- a/lib/stars.js +++ b/lib/stars.js @@ -5,6 +5,11 @@ const getIdentity = require('./utils/get-identity.js') const BaseCommand = require('./base-command.js') class Stars extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'View packages marked as favorites' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'stars' diff --git a/lib/start.js b/lib/start.js index 8987bc2931eb5..099b6e61b31fc 100644 --- a/lib/start.js +++ b/lib/start.js @@ -2,6 +2,11 @@ const LifecycleCmd = require('./utils/lifecycle-cmd.js') // This ends up calling run-script(['start', ...args]) class Start extends LifecycleCmd { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Start a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'start' diff --git a/lib/stop.js b/lib/stop.js index a3857ab1360be..766d9c01815cd 100644 --- a/lib/stop.js +++ b/lib/stop.js @@ -2,6 +2,11 @@ const LifecycleCmd = require('./utils/lifecycle-cmd.js') // This ends up calling run-script(['stop', ...args]) class Stop extends LifecycleCmd { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Stop a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'stop' diff --git a/lib/team.js b/lib/team.js index f84660af4d19a..5298bb3b2563e 100644 --- a/lib/team.js +++ b/lib/team.js @@ -5,6 +5,10 @@ const otplease = require('./utils/otplease.js') const BaseCommand = require('./base-command.js') class Team extends BaseCommand { + static get description () { + return 'Manage organization teams and team memberships' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'team' diff --git a/lib/test.js b/lib/test.js index 991d1c873cbfb..2be2b54af719e 100644 --- a/lib/test.js +++ b/lib/test.js @@ -2,6 +2,11 @@ const LifecycleCmd = require('./utils/lifecycle-cmd.js') // This ends up calling run-script(['test', ...args]) class Test extends LifecycleCmd { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Test a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'test' diff --git a/lib/token.js b/lib/token.js index 3d7952ccfc292..a80988531eebf 100644 --- a/lib/token.js +++ b/lib/token.js @@ -10,6 +10,10 @@ const readUserInfo = require('./utils/read-user-info.js') const BaseCommand = require('./base-command.js') class Token extends BaseCommand { + static get description () { + return 'Manage your authentication tokens' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'token' diff --git a/lib/uninstall.js b/lib/uninstall.js index ee0f338e9fc0a..29a62643dbf57 100644 --- a/lib/uninstall.js +++ b/lib/uninstall.js @@ -7,6 +7,10 @@ const completion = require('./utils/completion/installed-shallow.js') const BaseCommand = require('./base-command.js') class Uninstall extends BaseCommand { + static get description () { + return 'Remove a package' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'uninstall' diff --git a/lib/unpublish.js b/lib/unpublish.js index 68a9a0ae64ee5..a19b90a25f1b7 100644 --- a/lib/unpublish.js +++ b/lib/unpublish.js @@ -12,6 +12,10 @@ const getIdentity = require('./utils/get-identity.js') const BaseCommand = require('./base-command.js') class Unpublish extends BaseCommand { + static get description () { + return 'Remove a package from the registry' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'unpublish' diff --git a/lib/unstar.js b/lib/unstar.js index 5786cfce60f73..bc32ba617b46a 100644 --- a/lib/unstar.js +++ b/lib/unstar.js @@ -1,6 +1,11 @@ const Star = require('./star.js') class Unstar extends Star { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Remove an item from your favorite packages' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'unstar' diff --git a/lib/update.js b/lib/update.js index 87540b96e07e5..38776a7f1b5f2 100644 --- a/lib/update.js +++ b/lib/update.js @@ -8,6 +8,11 @@ const completion = require('./utils/completion/installed-deep.js') const BaseCommand = require('./base-command.js') class Update extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'Update packages' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'update' diff --git a/lib/utils/did-you-mean.js b/lib/utils/did-you-mean.js index c2bdf159dd118..3d8c6677cc002 100644 --- a/lib/utils/did-you-mean.js +++ b/lib/utils/did-you-mean.js @@ -1,12 +1,33 @@ const leven = require('leven') +const readJson = require('read-package-json-fast') +const { cmdList } = require('./cmd-list.js') -const didYouMean = (scmd, commands) => { - const best = commands +const didYouMean = async (npm, scmd) => { + const bestCmd = cmdList .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4) - .map(str => ` ${str}`) - return best.length === 0 ? '' - : best.length === 1 ? `\nDid you mean this?\n${best[0]}` - : `\nDid you mean one of these?\n${best.slice(0, 3).join('\n')}` -} + .map(str => ` npm ${str} # ${npm.commands[str].description}`) + + const path = npm.localPrefix + const pkg = await readJson(`${path}/package.json`) + const { scripts } = pkg + // We would already be suggesting this in `npm x` so omit them here + const runScripts = ['stop', 'start', 'test', 'restart'] + const bestRun = Object.keys(scripts) + .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4 && + !runScripts.includes(cmd)) + .map(str => ` npm run ${str} # run the "${str}" package script`) + + const { bin } = pkg + const bestBin = Object.keys(bin) + .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4) + .map(str => ` npm exec ${str} # run the "${str}" command from either this or a remote npm package`) + + const best = [...bestCmd, ...bestRun, ...bestBin] + const suggestion = best.length === 0 ? '' + : best.length === 1 ? `\n\nDid you mean this?\n${best[0]}` + : `\n\nDid you mean one of these?\n${best.slice(0, 3).join('\n')}` + const result = `Unknown command: "${scmd}"${suggestion}` + return result +} module.exports = didYouMean diff --git a/lib/utils/npm-usage.js b/lib/utils/npm-usage.js index b77bca7bec1a8..bc397cb4d95e6 100644 --- a/lib/utils/npm-usage.js +++ b/lib/utils/npm-usage.js @@ -1,14 +1,12 @@ -const didYouMean = require('./did-you-mean.js') const { dirname } = require('path') const { cmdList } = require('./cmd-list') -module.exports = (npm, valid = true) => { - npm.config.set('loglevel', 'silent') +module.exports = (npm) => { const usesBrowser = npm.config.get('viewer') === 'browser' ? ' (in a browser)' : '' - npm.log.level = 'silent' - npm.output(` -Usage: npm + return `npm + +Usage: npm install install all the dependencies in your project npm install add the dependency to your project @@ -20,7 +18,7 @@ npm help search for help on ${usesBrowser} npm help npm more involved overview${usesBrowser} All commands: -${npm.config.get('long') ? usages(npm) : ('\n ' + wrap(cmdList))} +${allCommands(npm)} Specify configs in the ini-formatted file: ${npm.config.get('userconfig')} @@ -29,14 +27,13 @@ or on the command line via: npm --key=value More configuration info: npm help config Configuration fields: npm help 7 config -npm@${npm.version} ${dirname(dirname(__dirname))} -`) - - if (npm.argv.length >= 1) - npm.output(didYouMean(npm.argv[0], cmdList)) +npm@${npm.version} ${dirname(dirname(__dirname))}` +} - if (!valid) - process.exitCode = 1 +const allCommands = (npm) => { + if (npm.config.get('long')) + return usages(npm) + return ('\n ' + wrap(cmdList)) } const wrap = (arr) => { diff --git a/lib/version.js b/lib/version.js index 2eda9d11b737c..18d79bc3a3877 100644 --- a/lib/version.js +++ b/lib/version.js @@ -2,6 +2,10 @@ const libversion = require('libnpmversion') const BaseCommand = require('./base-command.js') class Version extends BaseCommand { + static get description () { + return 'Bump a package version' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'version' diff --git a/lib/view.js b/lib/view.js index 0a6688fc2b13d..fd1611c3b5f1d 100644 --- a/lib/view.js +++ b/lib/view.js @@ -19,6 +19,11 @@ const readJson = async file => jsonParse(await readFile(file, 'utf8')) const BaseCommand = require('./base-command.js') class View extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'View registry info' + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'view' diff --git a/lib/whoami.js b/lib/whoami.js index 2322c5fd80e5d..2bc27ccdc214b 100644 --- a/lib/whoami.js +++ b/lib/whoami.js @@ -3,13 +3,13 @@ const getIdentity = require('./utils/get-identity.js') const BaseCommand = require('./base-command.js') class Whoami extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - static get name () { - return 'whoami' + static get description () { + return 'Display npm username' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - static get description () { - return 'prints username according to given registry' + static get name () { + return 'whoami' } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/tap-snapshots/test-lib-dist-tag.js-TAP.test.js b/tap-snapshots/test-lib-dist-tag.js-TAP.test.js index 89a87ae64e137..06936795bcf03 100644 --- a/tap-snapshots/test-lib-dist-tag.js-TAP.test.js +++ b/tap-snapshots/test-lib-dist-tag.js-TAP.test.js @@ -8,6 +8,8 @@ exports[`test/lib/dist-tag.js TAP add missing args > should exit usage error message 1`] = ` npm dist-tag +Modify package distribution tags + Usage: npm dist-tag add @ [] npm dist-tag rm @@ -21,6 +23,8 @@ Run "npm help dist-tag" for more info exports[`test/lib/dist-tag.js TAP add missing pkg name > should exit usage error message 1`] = ` npm dist-tag +Modify package distribution tags + Usage: npm dist-tag add @ [] npm dist-tag rm @@ -43,6 +47,8 @@ dist-tag add 1.0.0 to @scoped/another@7.7.7 exports[`test/lib/dist-tag.js TAP borked cmd usage > should show usage error 1`] = ` npm dist-tag +Modify package distribution tags + Usage: npm dist-tag add @ [] npm dist-tag rm @@ -62,6 +68,8 @@ latest: 1.0.0 exports[`test/lib/dist-tag.js TAP ls on missing name in current package > should throw usage error message 1`] = ` npm dist-tag +Modify package distribution tags + Usage: npm dist-tag add @ [] npm dist-tag rm @@ -111,6 +119,8 @@ exports[`test/lib/dist-tag.js TAP remove existing tag > should return success ms exports[`test/lib/dist-tag.js TAP remove missing pkg name > should exit usage error message 1`] = ` npm dist-tag +Modify package distribution tags + Usage: npm dist-tag add @ [] npm dist-tag rm diff --git a/tap-snapshots/test-lib-publish.js-TAP.test.js b/tap-snapshots/test-lib-publish.js-TAP.test.js index 97ce7a7733384..e777797d70b27 100644 --- a/tap-snapshots/test-lib-publish.js-TAP.test.js +++ b/tap-snapshots/test-lib-publish.js-TAP.test.js @@ -8,6 +8,8 @@ exports[`test/lib/publish.js TAP shows usage with wrong set of arguments > should print usage 1`] = ` npm publish +Publish a package + Usage: npm publish [] [--tag ] [--access ] [--dry-run] diff --git a/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js b/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js index caabec66bc829..11f7ff02408ae 100644 --- a/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js +++ b/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js @@ -6,8 +6,9 @@ */ 'use strict' exports[`test/lib/utils/npm-usage.js TAP usage basic usage > must match snapshot 1`] = ` +npm -Usage: npm +Usage: npm install install all the dependencies in your project npm install add the dependency to your project @@ -38,55 +39,12 @@ More configuration info: npm help config Configuration fields: npm help 7 config npm@{VERSION} {BASEDIR} - -` - -exports[`test/lib/utils/npm-usage.js TAP usage did you mean? > must match snapshot 1`] = ` - -Usage: npm - -npm install install all the dependencies in your project -npm install add the dependency to your project -npm test run this project's tests -npm run run the script named -npm -h quick help on -npm -l display usage info for all commands -npm help search for help on -npm help npm more involved overview - -All commands: - - access, adduser, audit, bin, bugs, cache, ci, completion, - config, dedupe, deprecate, diff, dist-tag, docs, doctor, - edit, exec, explain, explore, find-dupes, fund, get, help, - hook, init, install, install-ci-test, install-test, link, - ll, login, logout, ls, org, outdated, owner, pack, ping, - prefix, profile, prune, publish, rebuild, repo, restart, - root, run-script, search, set, set-script, shrinkwrap, star, - stars, start, stop, team, test, token, uninstall, unpublish, - unstar, update, version, view, whoami - -Specify configs in the ini-formatted file: - /some/config/file/.npmrc -or on the command line via: npm --key=value - -More configuration info: npm help config -Configuration fields: npm help 7 config - -npm@{VERSION} {BASEDIR} - -` - -exports[`test/lib/utils/npm-usage.js TAP usage did you mean? > must match snapshot 2`] = ` - -Did you mean one of these? - install - uninstall ` exports[`test/lib/utils/npm-usage.js TAP usage set process.stdout.columns columns=0 > must match snapshot 1`] = ` +npm -Usage: npm +Usage: npm install install all the dependencies in your project npm install add the dependency to your project @@ -117,12 +75,12 @@ More configuration info: npm help config Configuration fields: npm help 7 config npm@{VERSION} {BASEDIR} - ` exports[`test/lib/utils/npm-usage.js TAP usage set process.stdout.columns columns=90 > must match snapshot 1`] = ` +npm -Usage: npm +Usage: npm install install all the dependencies in your project npm install add the dependency to your project @@ -153,12 +111,12 @@ More configuration info: npm help config Configuration fields: npm help 7 config npm@{VERSION} {BASEDIR} - ` exports[`test/lib/utils/npm-usage.js TAP usage with browser > must match snapshot 1`] = ` +npm -Usage: npm +Usage: npm install install all the dependencies in your project npm install add the dependency to your project @@ -189,12 +147,12 @@ More configuration info: npm help config Configuration fields: npm help 7 config npm@{VERSION} {BASEDIR} - ` exports[`test/lib/utils/npm-usage.js TAP usage with long > must match snapshot 1`] = ` +npm -Usage: npm +Usage: npm install install all the dependencies in your project npm install add the dependency to your project @@ -209,6 +167,8 @@ All commands: access npm access + Set access level on published packages + Usage: npm access public [] npm access restricted [] @@ -224,6 +184,8 @@ All commands: adduser npm adduser + Add a registry user account + Usage: npm adduser [--registry=url] [--scope=@orgname] [--always-auth] @@ -233,6 +195,8 @@ All commands: audit npm audit + Run a security audit + Usage: npm audit [--json] [--production] npm audit fix [--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)] @@ -241,6 +205,8 @@ All commands: bin npm bin + Display npm bin folder + Usage: npm bin [-g] @@ -248,6 +214,8 @@ All commands: bugs npm bugs + Report bugs for a package in a web browser + Usage: npm bugs [] @@ -257,6 +225,8 @@ All commands: cache npm cache + Manipulates packages cache + Usage: npm cache add npm cache add @@ -270,6 +240,8 @@ All commands: ci npm ci + Install a project with a clean slate + Usage: npm ci @@ -279,7 +251,7 @@ All commands: completion npm completion - npm command completion script. save to ~/.bashrc or ~/.zshrc + Tab Completion for npm Usage: npm completion @@ -288,6 +260,8 @@ All commands: config npm config + Manage the npm configuration files + Usage: npm config set = [= ...] npm config get [ [ ...]] @@ -301,6 +275,8 @@ All commands: dedupe npm dedupe + Reduce duplication in the package tree + Usage: npm dedupe @@ -310,6 +286,8 @@ All commands: deprecate npm deprecate + Deprecate a version of a package + Usage: npm deprecate [@] @@ -317,6 +295,8 @@ All commands: diff npm diff + The registry diff command + Usage: npm diff [...] npm diff --diff= [...] @@ -328,6 +308,8 @@ All commands: dist-tag npm dist-tag + Modify package distribution tags + Usage: npm dist-tag add @ [] npm dist-tag rm @@ -337,12 +319,21 @@ All commands: Run "npm help dist-tag" for more info - docs npm docs [ [ ...]] + docs npm docs + + Open documentation for a package in a web browser + + Usage: + npm docs [ [ ...]] alias: home + + Run "npm help docs" for more info doctor npm doctor + Check your npm environment + Usage: npm doctor @@ -350,6 +341,8 @@ All commands: edit npm edit + Edit an installed package + Usage: npm edit [/...] @@ -357,7 +350,7 @@ All commands: exec npm exec - Run a command from a local or remote npm package. + Run a command from a local or remote npm package Usage: npm exec -- [@] [args...] @@ -371,6 +364,8 @@ All commands: explain npm explain + Explain installed packages + Usage: npm explain @@ -380,6 +375,8 @@ All commands: explore npm explore + Browse an installed package + Usage: npm explore [ -- ] @@ -387,6 +384,8 @@ All commands: find-dupes npm find-dupes + Find duplication in the package tree + Usage: npm find-dupes @@ -394,6 +393,8 @@ All commands: fund npm fund + Retrieve funding information + Usage: npm fund [--json] [--browser] [--unicode] [[<@scope>/] [--which=] @@ -401,6 +402,8 @@ All commands: get npm get + Get a value from the npm configuration + Usage: npm get [ ...] (See \`npm config\`) @@ -408,6 +411,8 @@ All commands: help npm help + Get help on npm + Usage: npm help [] @@ -417,6 +422,8 @@ All commands: hook npm hook + Manage registry hooks + Usage: npm hook add [--type=] npm hook ls [pkg] @@ -427,6 +434,8 @@ All commands: init npm init + Create a package.json file + Usage: npm init [--force|-f|--yes|-y|--scope] npm init <@scope> (same as \`npx <@scope>/create\`) @@ -438,6 +447,8 @@ All commands: install npm install + Install a package + Usage: npm install [<@scope>/] npm install [<@scope>/]@ @@ -456,6 +467,8 @@ All commands: install-ci-test npm install-ci-test + Install a project with a clean slate and run tests + Usage: npm install-ci-test @@ -465,6 +478,8 @@ All commands: install-test npm install-test + Install package(s) and run tests + Usage: npm install-test [<@scope>/] npm install-test [<@scope>/]@ @@ -483,6 +498,8 @@ All commands: link npm link + Symlink a package folder + Usage: npm link (in package dir) npm link [<@scope>/][@] @@ -493,6 +510,8 @@ All commands: ll npm ll + List installed packages + Usage: npm ll [[<@scope>/] ...] @@ -502,6 +521,8 @@ All commands: login npm adduser + Add a registry user account + Usage: npm adduser [--registry=url] [--scope=@orgname] [--always-auth] @@ -511,6 +532,8 @@ All commands: logout npm logout + Log out of the registry + Usage: npm logout [--registry=] [--scope=<@scope>] @@ -518,6 +541,8 @@ All commands: ls npm ls + List installed packages + Usage: npm ls npm ls [[<@scope>/] ...] @@ -527,6 +552,8 @@ All commands: org npm org + Manage orgs + Usage: npm org set orgname username [developer | admin | owner] npm org rm orgname username @@ -538,6 +565,8 @@ All commands: outdated npm outdated + Check for outdated packages + Usage: npm outdated [[<@scope>/] ...] @@ -545,6 +574,8 @@ All commands: owner npm owner + Manage package owners + Usage: npm owner add [<@scope>/] npm owner rm [<@scope>/] @@ -556,6 +587,8 @@ All commands: pack npm pack + Create a tarball from a package + Usage: npm pack [[<@scope>/]...] [--dry-run] @@ -563,7 +596,7 @@ All commands: ping npm ping - ping registry + Ping npm registry Usage: npm ping @@ -572,6 +605,8 @@ All commands: prefix npm prefix + Display prefix + Usage: npm prefix [-g] @@ -579,6 +614,8 @@ All commands: profile npm profile + Change settings on your registry profile + Usage: npm profile enable-2fa [auth-only|auth-and-writes] npm profile disable-2fa @@ -589,6 +626,8 @@ All commands: prune npm prune + Remove extraneous packages + Usage: npm prune [[<@scope>/]...] [--production] @@ -596,6 +635,8 @@ All commands: publish npm publish + Publish a package + Usage: npm publish [] [--tag ] [--access ] [--dry-run] @@ -603,6 +644,8 @@ All commands: rebuild npm rebuild + Rebuild a package + Usage: npm rebuild [[<@scope>/][@] ...] @@ -612,6 +655,8 @@ All commands: repo npm repo + Open package repository page in the browser + Usage: npm repo [ [ ...]] @@ -619,6 +664,8 @@ All commands: restart npm restart + Restart a package + Usage: npm restart [-- ] @@ -626,6 +673,8 @@ All commands: root npm root + Display npm root + Usage: npm root [-g] @@ -633,6 +682,8 @@ All commands: run-script npm run-script + Run arbitrary package scripts + Usage: npm run-script [-- ] @@ -642,6 +693,8 @@ All commands: search npm search + Search for pacakges + Usage: npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...] @@ -651,6 +704,8 @@ All commands: set npm set + Set a value in the npm configuration + Usage: npm set = [= ...] (See \`npm config\`) @@ -658,6 +713,8 @@ All commands: set-script npm set-script + Set tasks in the scripts section of package.json + Usage: npm set-script [