diff options
author | Gar <gar+gh@danger.computer> | 2021-03-11 03:11:34 +0300 |
---|---|---|
committer | Ruy Adorno <ruyadorno@hotmail.com> | 2021-03-18 22:54:36 +0300 |
commit | 41facf6435ced4e416d74111d9c3ff00ee19ab7d (patch) | |
tree | 1a6644f756670ee6fad67a9a59d135dc0f3e00ef /lib | |
parent | a8d0751e4b7c7d8b808c8a49f288fc7272f729b0 (diff) |
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: https://github.com/npm/cli/pull/2859
Credit: @wraithgar
Close: #2859
Reviewed-by: @ruyadorno
Diffstat (limited to 'lib')
69 files changed, 424 insertions, 188 deletions
diff --git a/lib/access.js b/lib/access.js index 3020719a7..3df611e56 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 da318a1f3..f761ae386 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 27cebff9f..a752f202f 100644 --- a/lib/audit.js +++ b/lib/audit.js @@ -6,6 +6,11 @@ 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 8e48caa2e..b8497fd44 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 b0acefef9..23098b670 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 1814dd7bc..a0cef4c5e 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 b2d42ab23..43902f43b 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' @@ -21,6 +21,11 @@ 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 910b674ea..837d0876c 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 b11188770..fa3b5f2dd 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 7d0147712..d5ef6ec50 100644 --- a/lib/config.js +++ b/lib/config.js @@ -30,6 +30,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 4dd759dac..b80a777fc 100644 --- a/lib/dedupe.js +++ b/lib/dedupe.js @@ -6,6 +6,11 @@ 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 a0c67f805..c640bcfe0 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 221213365..c5e4b403e 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 81773a40a..13ec37fd8 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 2dad7a26d..089d77eb0 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 [<pkgname> [<pkgname> ...]]') + static get usage () { + return ['[<pkgname> [<pkgname> ...]]'] } exec (args, cb) { diff --git a/lib/doctor.js b/lib/doctor.js index ae69b6a77..99beb2527 100644 --- a/lib/doctor.js +++ b/lib/doctor.js @@ -33,6 +33,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 1dbe8e4c1..79d41462e 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 8c8606456..5b2e15831 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 6af761186..4c2908df6 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 34f6d1079..5f9820c21 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 ecb945f47..8037876f5 100644 --- a/lib/find-dupes.js +++ b/lib/find-dupes.js @@ -3,6 +3,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 a4986f3c4..302f10dcf 100644 --- a/lib/fund.js +++ b/lib/fund.js @@ -23,6 +23,11 @@ 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 a5d58accc..8cfb259a8 100644 --- a/lib/get.js +++ b/lib/get.js @@ -2,6 +2,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 62e1adb59..c1ceae441 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 93abf878b..b9ff1c957 100644 --- a/lib/help.js +++ b/lib/help.js @@ -1,14 +1,23 @@ -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 <noargs>: show basic usage - if (!section) { - npmUsage(this.npm, argv[0] === 'help') - return - } - - // npm <command> -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 <section>: 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 6cda3504f..64b1201cb 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 3d11050e4..81c673388 100644 --- a/lib/init.js +++ b/lib/init.js @@ -5,6 +5,11 @@ 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 c52b5c9e8..871f24b2f 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 76c6f367d..d5664119d 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 34d0b2353..363230d31 100644 --- a/lib/install.js +++ b/lib/install.js @@ -12,6 +12,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 9aad6dc1d..fe9cfd3a6 100644 --- a/lib/link.js +++ b/lib/link.js @@ -13,6 +13,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 3784020ec..3589d287b 100644 --- a/lib/logout.js +++ b/lib/logout.js @@ -5,6 +5,11 @@ 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' } @@ -24,6 +24,11 @@ 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 d80067a11..78b6ba034 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -38,6 +38,7 @@ const proxyCmds = new Proxy({}, { const { definitions, flatten, shorthands } = require('./utils/config/index.js') const { shellouts } = require('./utils/cmd-list.js') +const usage = require('./utils/npm-usage.js') let warnedNonDashArg = false const _runCmd = Symbol('_runCmd') @@ -100,7 +101,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 b9f84b060..ddd2b03da 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 6a7b4e7b3..9b656d2ae 100644 --- a/lib/outdated.js +++ b/lib/outdated.js @@ -14,6 +14,11 @@ 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 b62f125ac..e537d82d0 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 cdd823430..4618e767e 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -12,6 +12,11 @@ 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 53467c93b..b68ea6b56 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 5ade87f64..1298a3c5b 100644 --- a/lib/prefix.js +++ b/lib/prefix.js @@ -2,6 +2,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 3d4857df0..9789146fd 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 c2cddb1a2..e86d5c053 100644 --- a/lib/prune.js +++ b/lib/prune.js @@ -5,6 +5,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 ed05d1902..410e911f9 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 c11747b97..5910ab3d1 100644 --- a/lib/rebuild.js +++ b/lib/rebuild.js @@ -7,6 +7,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 aa07e07a8..5ab136abd 100644 --- a/lib/repo.js +++ b/lib/repo.js @@ -8,6 +8,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 1f3eb5af9..840eb2067 100644 --- a/lib/restart.js +++ b/lib/restart.js @@ -3,6 +3,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 1fe82c6fa..d24a2e20a 100644 --- a/lib/root.js +++ b/lib/root.js @@ -1,6 +1,11 @@ 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 f6ca57b79..a7202548c 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -20,6 +20,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' } @@ -70,9 +75,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 4c206498f..aca9113ce 100644 --- a/lib/search.js +++ b/lib/search.js @@ -27,6 +27,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 624198132..df101a0ac 100644 --- a/lib/set-script.js +++ b/lib/set-script.js @@ -6,6 +6,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 787a8012c..74a002cd6 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 76979b021..5d4a1ada9 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -8,6 +8,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 9a8ee8c77..4c5cf4900 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 758a3130d..db93d7b19 100644 --- a/lib/stars.js +++ b/lib/stars.js @@ -6,6 +6,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 8987bc293..099b6e61b 100644 --- a/lib/start.js +++ b/lib/start.js @@ -3,6 +3,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 a3857ab13..766d9c018 100644 --- a/lib/stop.js +++ b/lib/stop.js @@ -3,6 +3,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 f84660af4..5298bb3b2 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 991d1c873..2be2b54af 100644 --- a/lib/test.js +++ b/lib/test.js @@ -3,6 +3,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 3d7952ccf..a80988531 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 c1c36ab6b..16a49b757 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 06b2779b2..d49bb7ba4 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 5786cfce6..bc32ba617 100644 --- a/lib/unstar.js +++ b/lib/unstar.js @@ -2,6 +2,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 8ce52a442..66a668cd3 100644 --- a/lib/update.js +++ b/lib/update.js @@ -9,6 +9,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 c2bdf159d..3d8c6677c 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 b77bca7be..bc397cb4d 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 <command> + return `npm <command> + +Usage: npm install install all the dependencies in your project npm install <foo> add the <foo> dependency to your project @@ -20,7 +18,7 @@ npm help <term> search for help on <term>${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 <command> --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 30097a3c3..18b7d7d6c 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 7bbedcf65..e0df1e231 100644 --- a/lib/view.js +++ b/lib/view.js @@ -20,6 +20,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 180eb3317..6e2841359 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 */ |