From 9fe0df5b5d7606e5841288d9931be6c04767c9ca Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 3 Mar 2021 20:26:33 -0800 Subject: [PATCH] fix(usage): clean up usage declarations Small refactor of commands to allow usage to be more programmatically generated, leading us in the direction of more tighly coupling each command to the params it accepts. PR-URL: https://github.com/npm/cli/pull/2821 Credit: @wraithgar Close: #2821 Reviewed-by: @isaacs --- lib/access.js | 46 +- lib/adduser.js | 16 +- lib/audit.js | 21 +- lib/base-command.js | 38 ++ lib/bin.js | 13 +- lib/bugs.js | 13 +- lib/cache.js | 30 +- lib/ci.js | 12 +- lib/completion.js | 13 +- lib/config.js | 33 +- lib/dedupe.js | 11 +- lib/deprecate.js | 23 +- lib/diff.js | 27 +- lib/dist-tag.js | 23 +- lib/doctor.js | 12 +- lib/edit.js | 13 +- lib/exec.js | 42 +- lib/explain.js | 13 +- lib/explore.js | 13 +- lib/find-dupes.js | 12 +- lib/fund.js | 18 +- lib/get.js | 16 +- lib/help-search.js | 14 +- lib/help.js | 13 +- lib/hook.js | 22 +- lib/init.js | 22 +- lib/install-ci-test.js | 17 +- lib/install-test.js | 21 +- lib/install.js | 38 +- lib/link.js | 20 +- lib/ll.js | 13 +- lib/logout.js | 16 +- lib/ls.js | 16 +- lib/org.js | 23 +- lib/outdated.js | 15 +- lib/owner.js | 39 +- lib/pack.js | 13 +- lib/ping.js | 13 +- lib/prefix.js | 13 +- lib/profile.js | 25 +- lib/prune.js | 15 +- lib/publish.js | 19 +- lib/rebuild.js | 13 +- lib/repo.js | 13 +- lib/restart.js | 5 +- lib/root.js | 14 +- lib/run-script.js | 16 +- lib/search.js | 16 +- lib/set-script.js | 13 +- lib/set.js | 17 +- lib/shrinkwrap.js | 13 +- lib/star.js | 18 +- lib/stars.js | 13 +- lib/start.js | 5 +- lib/stop.js | 5 +- lib/team.js | 27 +- lib/test.js | 5 +- lib/token.js | 28 +- lib/uninstall.js | 17 +- lib/unpublish.js | 14 +- lib/unstar.js | 5 + lib/update.js | 16 +- lib/utils/lifecycle-cmd.js | 15 +- lib/version.js | 21 +- lib/view.js | 17 +- lib/whoami.js | 22 +- .../test-lib-dist-tag.js-TAP.test.js | 25 + tap-snapshots/test-lib-publish.js-TAP.test.js | 6 +- .../test-lib-utils-npm-usage.js-TAP.test.js | 472 ++++++++++++++---- test/lib/access.js | 2 +- test/lib/adduser.js | 4 + test/lib/bin.js | 3 +- test/lib/bugs.js | 5 + test/lib/cache.js | 2 +- test/lib/config.js | 13 +- test/lib/deprecate.js | 4 +- test/lib/explore.js | 2 +- test/lib/load-all-commands.js | 4 +- test/lib/npm.js | 2 +- test/lib/owner.js | 36 +- test/lib/restart.js | 1 - test/lib/start.js | 1 - test/lib/stop.js | 1 - test/lib/utils/lifecycle-cmd.js | 7 +- 84 files changed, 1023 insertions(+), 760 deletions(-) create mode 100644 lib/base-command.js diff --git a/lib/access.js b/lib/access.js index 3bc21119033f5..0df36beeac15f 100644 --- a/lib/access.js +++ b/lib/access.js @@ -4,8 +4,8 @@ const libaccess = require('libnpmaccess') const readPackageJson = require('read-package-json-fast') const otplease = require('./utils/otplease.js') -const usageUtil = require('./utils/usage.js') const getIdentity = require('./utils/get-identity.js') +const BaseCommand = require('./base-command.js') const subcommands = [ 'public', @@ -19,24 +19,23 @@ const subcommands = [ '2fa-not-required', ] -class Access { - constructor (npm) { - this.npm = npm +class Access extends BaseCommand { + static get name () { + return 'access' } - get usage () { - return usageUtil( - 'access', - 'npm access public []\n' + - 'npm access restricted []\n' + - 'npm access grant []\n' + - 'npm access revoke []\n' + - 'npm access 2fa-required []\n' + - 'npm access 2fa-not-required []\n' + - 'npm access ls-packages [||]\n' + - 'npm access ls-collaborators [ []]\n' + - 'npm access edit []' - ) + static get usage () { + return [ + 'public []', + 'restricted []', + 'grant []', + 'revoke []', + '2fa-required []', + '2fa-not-required []', + 'ls-packages [||]', + 'ls-collaborators [ []]', + 'edit []', + ] } async completion (opts) { @@ -66,12 +65,7 @@ class Access { } exec (args, cb) { - this.access(args) - .then(x => cb(null, x)) - .catch(err => err.code === 'EUSAGE' - ? cb(err.message) - : cb(err) - ) + this.access(args).then(() => cb()).catch(cb) } async access ([cmd, ...args]) { @@ -202,12 +196,6 @@ class Access { return name } } - - usageError (msg) { - return Object.assign(new Error(`\nUsage: ${msg}\n\n` + this.usage), { - code: 'EUSAGE', - }) - } } module.exports = Access diff --git a/lib/adduser.js b/lib/adduser.js index 45d602fd2c844..da318a1f3feb8 100644 --- a/lib/adduser.js +++ b/lib/adduser.js @@ -1,6 +1,6 @@ const log = require('npmlog') -const usageUtil = require('./utils/usage.js') const replaceInfo = require('./utils/replace-info.js') +const BaseCommand = require('./base-command.js') const authTypes = { legacy: require('./auth/legacy.js'), oauth: require('./auth/oauth.js'), @@ -8,17 +8,13 @@ const authTypes = { sso: require('./auth/sso.js'), } -class AddUser { - constructor (npm) { - this.npm = npm +class AddUser extends BaseCommand { + static get name () { + return 'adduser' } - /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'adduser', - 'npm adduser [--registry=url] [--scope=@orgname] [--always-auth]' - ) + static get usage () { + return ['[--registry=url] [--scope=@orgname] [--always-auth]'] } exec (args, cb) { diff --git a/lib/audit.js b/lib/audit.js index b8c85605dba43..6e64987b612ae 100644 --- a/lib/audit.js +++ b/lib/audit.js @@ -2,21 +2,20 @@ const Arborist = require('@npmcli/arborist') const auditReport = require('npm-audit-report') const reifyFinish = require('./utils/reify-finish.js') const auditError = require('./utils/audit-error.js') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Audit { - constructor (npm) { - this.npm = npm +class Audit extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'audit' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'audit', - 'npm audit [--json] [--production]' + - '\nnpm audit fix ' + - '[--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)]' - ) + static get usage () { + return [ + '[--json] [--production]', + 'fix [--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)]', + ] } async completion (opts) { diff --git a/lib/base-command.js b/lib/base-command.js new file mode 100644 index 0000000000000..c31e4a4d7f1b2 --- /dev/null +++ b/lib/base-command.js @@ -0,0 +1,38 @@ +// Base class for npm.commands[cmd] +const usageUtil = require('./utils/usage.js') + +class BaseCommand { + constructor (npm) { + this.npm = npm + } + + get usage () { + let usage = `npm ${this.constructor.name}\n\n` + if (this.constructor.description) + usage = `${usage}${this.constructor.description}\n\n` + + usage = `${usage}Usage:\n` + if (!this.constructor.usage) + usage = `${usage}npm ${this.constructor.name}` + else + usage = `${usage}${this.constructor.usage.map(u => `npm ${this.constructor.name} ${u}`).join('\n')}` + + // Mostly this just appends aliases, this could be more clear + usage = usageUtil(this.constructor.name, usage) + usage = `${usage}\n\nRun "npm ${this.constructor.name} help" for more info` + return usage + } + + usageError (msg) { + if (!msg) { + return Object.assign(new Error(`\nUsage: ${this.usage}`), { + code: 'EUSAGE', + }) + } + + return Object.assign(new Error(`\nUsage: ${msg}\n\n${this.usage}`), { + code: 'EUSAGE', + }) + } +} +module.exports = BaseCommand diff --git a/lib/bin.js b/lib/bin.js index f540cc57c8cd7..1450fb539bffa 100644 --- a/lib/bin.js +++ b/lib/bin.js @@ -1,14 +1,13 @@ const envPath = require('./utils/path.js') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Bin { - constructor (npm) { - this.npm = npm +class Bin extends BaseCommand { + static get name () { + return 'bin' } - /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('bin', 'npm bin [-g]') + static get usage () { + return ['[-g]'] } exec (args, cb) { diff --git a/lib/bugs.js b/lib/bugs.js index fb0d7c92770c7..1814dd7bc461e 100644 --- a/lib/bugs.js +++ b/lib/bugs.js @@ -1,17 +1,16 @@ 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') +const BaseCommand = require('./base-command.js') -class Bugs { - constructor (npm) { - this.npm = npm +class Bugs extends BaseCommand { + static get name () { + return 'bugs' } - /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('bugs', 'npm bugs []') + static get usage () { + return ['[]'] } exec (args, cb) { diff --git a/lib/cache.js b/lib/cache.js index 3ca99fd2562e3..80a5c68ebc0e9 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -4,23 +4,25 @@ const log = require('npmlog') const pacote = require('pacote') const path = require('path') const rimraf = promisify(require('rimraf')) +const BaseCommand = require('./base-command.js') -const usageUtil = require('./utils/usage.js') -class Cache { - constructor (npm) { - this.npm = npm +class Cache extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'cache' } - get usage () { - return usageUtil('cache', - 'npm cache add ' + - '\nnpm cache add ' + - '\nnpm cache add ' + - '\nnpm cache add ' + - '\nnpm cache add @' + - '\nnpm cache clean' + - '\nnpm cache verify' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'add ', + 'add ', + 'add ', + 'add ', + 'add @', + 'clean', + 'verify', + ] } async completion (opts) { diff --git a/lib/ci.js b/lib/ci.js index 03a91a60463f2..3ea19937616e6 100644 --- a/lib/ci.js +++ b/lib/ci.js @@ -7,7 +7,6 @@ const fs = require('fs') const readdir = util.promisify(fs.readdir) const log = require('npmlog') -const usageUtil = require('./utils/usage.js') const removeNodeModules = async where => { const rimrafOpts = { glob: false } @@ -18,15 +17,12 @@ const removeNodeModules = async where => { await Promise.all(entries.map(f => rimraf(`${path}/${f}`, rimrafOpts))) process.emit('timeEnd', 'npm-ci:rm') } +const BaseCommand = require('./base-command.js') -class CI { - constructor (npm) { - this.npm = npm - } - +class CI extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('ci', 'npm ci') + static get name () { + return 'ci' } exec (args, cb) { diff --git a/lib/completion.js b/lib/completion.js index 5baf17665800d..3ee68cdacaf95 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -41,17 +41,18 @@ const allConfs = configNames.concat(shorthandNames) const isWindowsShell = require('./utils/is-windows-shell.js') const fileExists = require('./utils/file-exists.js') -const usageUtil = require('./utils/usage.js') const { promisify } = require('util') +const BaseCommand = require('./base-command.js') -class Completion { - constructor (npm) { - this.npm = npm +class Completion extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'completion' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('completion', 'source <(npm completion)') + static get description () { + return 'npm command completion script. save to ~/.bashrc or ~/.zshrc' } // completion for the completion command diff --git a/lib/config.js b/lib/config.js index 7009f46016d23..c29253e430a33 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,5 +1,4 @@ const { defaults, types } = require('./utils/config.js') -const usageUtil = require('./utils/usage.js') const mkdirp = require('mkdirp-infer-owner') const { dirname } = require('path') @@ -28,22 +27,22 @@ const keyValues = args => { const publicVar = k => !/^(\/\/[^:]+:)?_/.test(k) -class Config { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Config extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'config' } - get usage () { - return usageUtil( - 'config', - 'npm config set = [= ...]' + - '\nnpm config get [ [ ...]]' + - '\nnpm config delete [ ...]' + - '\nnpm config list [--json]' + - '\nnpm config edit' + - '\nnpm set = [= ...]' + - '\nnpm get [ [ ...]]' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'set = [= ...]', + 'get [ [ ...]]', + 'delete [ ...]', + 'list [--json]', + 'edit', + ] } async completion (opts) { @@ -253,10 +252,6 @@ ${defData} } this.npm.output(JSON.stringify(publicConf, null, 2)) } - - usageError () { - return Object.assign(new Error(this.usage), { code: 'EUSAGE' }) - } } module.exports = Config diff --git a/lib/dedupe.js b/lib/dedupe.js index 59978895effb2..50a56211fc847 100644 --- a/lib/dedupe.js +++ b/lib/dedupe.js @@ -1,16 +1,13 @@ // dedupe duplicated packages, or find them in the tree const Arborist = require('@npmcli/arborist') -const usageUtil = require('./utils/usage.js') const reifyFinish = require('./utils/reify-finish.js') -class Dedupe { - constructor (npm) { - this.npm = npm - } +const BaseCommand = require('./base-command.js') +class Dedupe extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('dedupe', 'npm dedupe') + static get name () { + return 'dedupe' } exec (args, cb) { diff --git a/lib/deprecate.js b/lib/deprecate.js index 48f27ab6c35e8..a0c67f805d2f9 100644 --- a/lib/deprecate.js +++ b/lib/deprecate.js @@ -4,18 +4,17 @@ const npa = require('npm-package-arg') const semver = require('semver') const getIdentity = require('./utils/get-identity.js') const libaccess = require('libnpmaccess') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Deprecate { - constructor (npm) { - this.npm = npm +class Deprecate extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'deprecate' } - get usage () { - return usageUtil( - 'deprecate', - 'npm deprecate [@] ' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['[@] '] } async completion (opts) { @@ -71,12 +70,6 @@ class Deprecate { ignoreBody: true, })) } - - usageError () { - return Object.assign(new Error(`\nUsage: ${this.usage}`), { - code: 'EUSAGE', - }) - } } module.exports = Deprecate diff --git a/lib/diff.js b/lib/diff.js index ed36a30673c43..0e322ec643849 100644 --- a/lib/diff.js +++ b/lib/diff.js @@ -8,23 +8,24 @@ const npmlog = require('npmlog') const pacote = require('pacote') const pickManifest = require('npm-pick-manifest') -const usageUtil = require('./utils/usage.js') const readLocalPkg = require('./utils/read-local-package.js') +const BaseCommand = require('./base-command.js') -class Diff { - constructor (npm) { - this.npm = npm +class Diff extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'diff' } - get usage () { - return usageUtil( - 'diff', - 'npm diff [...]' + - '\nnpm diff --diff= [...]' + - '\nnpm diff --diff= [--diff=] [...]' + - '\nnpm diff --diff= [--diff=] [...]' + - '\nnpm diff [--diff-ignore-all-space] [--diff-name-only] [...] [...]' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + '[...]', + '--diff= [...]', + '--diff= [--diff=] [...]', + '--diff= [--diff=] [...]', + '[--diff-ignore-all-space] [--diff-name-only] [...] [...]', + ] } get where () { diff --git a/lib/dist-tag.js b/lib/dist-tag.js index 4b7e2602043be..cdc95ac6f0cd7 100644 --- a/lib/dist-tag.js +++ b/lib/dist-tag.js @@ -5,20 +5,21 @@ const semver = require('semver') const otplease = require('./utils/otplease.js') const readLocalPkgName = require('./utils/read-local-package.js') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class DistTag { - constructor (npm) { - this.npm = npm +class DistTag extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'dist-tag' } - get usage () { - return usageUtil( - 'dist-tag', - 'npm dist-tag add @ []' + - '\nnpm dist-tag rm ' + - '\nnpm dist-tag ls []' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'add @ []', + 'rm ', + 'ls []', + ] } async completion (opts) { diff --git a/lib/doctor.js b/lib/doctor.js index 63619d0cf5377..fbe44714140aa 100644 --- a/lib/doctor.js +++ b/lib/doctor.js @@ -11,7 +11,6 @@ const { promisify } = require('util') const ansiTrim = require('./utils/ansi-trim.js') const isWindows = require('./utils/is-windows.js') const ping = require('./utils/ping.js') -const usageUtil = require('./utils/usage.js') const { defaults: { registry: defaultRegistry } } = require('./utils/config.js') const lstat = promisify(fs.lstat) const readdir = promisify(fs.readdir) @@ -31,14 +30,11 @@ const maskLabel = mask => { return label.join(', ') } -class Doctor { - constructor (npm) { - this.npm = npm - } - +const BaseCommand = require('./base-command.js') +class Doctor extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('doctor', 'npm doctor') + static get name () { + return 'doctor' } exec (args, cb) { diff --git a/lib/edit.js b/lib/edit.js index a7dbb38205b02..1dbe8e4c103ad 100644 --- a/lib/edit.js +++ b/lib/edit.js @@ -4,18 +4,19 @@ const { resolve } = require('path') const fs = require('graceful-fs') const { spawn } = require('child_process') -const usageUtil = require('./utils/usage.js') const splitPackageNames = require('./utils/split-package-names.js') const completion = require('./utils/completion/installed-shallow.js') +const BaseCommand = require('./base-command.js') -class Edit { - constructor (npm) { - this.npm = npm +class Edit extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'edit' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('edit', 'npm edit [/...]') + static get usage () { + return ['[/...]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/exec.js b/lib/exec.js index 69c3cfe75c2cc..b2443b17accd2 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -1,4 +1,3 @@ -const usageUtil = require('./utils/usage.js') const { promisify } = require('util') const read = promisify(require('read')) const mkdirp = require('mkdirp-infer-owner') @@ -12,6 +11,7 @@ const pacote = require('pacote') const npa = require('npm-package-arg') const fileExists = require('./utils/file-exists.js') const PATH = require('./utils/path.js') +const BaseCommand = require('./base-command.js') // it's like this: // @@ -38,31 +38,25 @@ const PATH = require('./utils/path.js') // runScript({ pkg, event: 'npx', ... }) // process.env.npm_lifecycle_event = 'npx' -class Exec { - constructor (npm) { - this.npm = npm +class Exec extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'exec' } - get usage () { - return usageUtil('exec', - 'Run a command from a local or remote npm package.\n\n' + - - 'npm exec -- [@] [args...]\n' + - 'npm exec --package=[@] -- [args...]\n' + - 'npm exec -c \' [args...]\'\n' + - 'npm exec --package=foo -c \' [args...]\'\n' + - '\n' + - 'npx [@] [args...]\n' + - 'npx -p [@] [args...]\n' + - 'npx -c \' [args...]\'\n' + - 'npx -p [@] -c \' [args...]\'' + - '\n' + - 'Run without --call or positional args to open interactive subshell\n', - - '\n--package= (may be specified multiple times)\n' + - '-p is a shorthand for --package only when using npx executable\n' + - '-c --call= (may not be mixed with positional arguments)' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + 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 usage () { + return [ + '-- [@] [args...]', + '--package=[@] -- [args...]', + '-c \' [args...]\'', + '--package=foo -c \' [args...]\'', + ] } exec (args, cb) { diff --git a/lib/explain.js b/lib/explain.js index f46d3b5072637..6af7611867786 100644 --- a/lib/explain.js +++ b/lib/explain.js @@ -1,4 +1,3 @@ -const usageUtil = require('./utils/usage.js') const { explainNode } = require('./utils/explain-dep.js') const completion = require('./utils/completion/installed-deep.js') const Arborist = require('@npmcli/arborist') @@ -6,15 +5,17 @@ const npa = require('npm-package-arg') const semver = require('semver') const { relative, resolve } = require('path') const validName = require('validate-npm-package-name') +const BaseCommand = require('./base-command.js') -class Explain { - constructor (npm) { - this.npm = npm +class Explain extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'explain' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('explain', 'npm explain ') + static get usage () { + return [''] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/explore.js b/lib/explore.js index e09e867406e1d..34f6d10793c7e 100644 --- a/lib/explore.js +++ b/lib/explore.js @@ -5,16 +5,17 @@ const rpj = require('read-package-json-fast') const runScript = require('@npmcli/run-script') const { join, resolve, relative } = require('path') const completion = require('./utils/completion/installed-shallow.js') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Explore { - constructor (npm) { - this.npm = npm +class Explore extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'explore' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('explore', 'npm explore [ -- ]') + static get usage () { + return [' [ -- ]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/find-dupes.js b/lib/find-dupes.js index 5061be9cc381a..ecb945f47bb41 100644 --- a/lib/find-dupes.js +++ b/lib/find-dupes.js @@ -1,14 +1,10 @@ // dedupe duplicated packages, or find them in the tree -const usageUtil = require('./utils/usage.js') - -class FindDupes { - constructor (npm) { - this.npm = npm - } +const BaseCommand = require('./base-command.js') +class FindDupes extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('find-dupes', 'npm find-dupes') + static get name () { + return 'find-dupes' } exec (args, cb) { diff --git a/lib/fund.js b/lib/fund.js index 826c3170e7e8a..a723c62d2c3c5 100644 --- a/lib/fund.js +++ b/lib/fund.js @@ -13,25 +13,23 @@ const { const completion = require('./utils/completion/installed-deep.js') const openUrl = require('./utils/open-url.js') -const usageUtil = require('./utils/usage.js') const getPrintableName = ({ name, version }) => { const printableVersion = version ? `@${version}` : '' return `${name}${printableVersion}` } -class Fund { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') + +class Fund extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'fund' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'fund', - 'npm fund', - 'npm fund [--json] [--browser] [--unicode] [[<@scope>/] [--which=]' - ) + static get usage () { + return ['[--json] [--browser] [--unicode] [[<@scope>/] [--which=]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/get.js b/lib/get.js index a5b2f5514473d..a5d58accc8307 100644 --- a/lib/get.js +++ b/lib/get.js @@ -1,16 +1,14 @@ -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Get { - constructor (npm) { - this.npm = npm +class Get extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'get' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'get', - 'npm get [ ...] (See `npm config`)' - ) + static get usage () { + return ['[ ...] (See `npm config`)'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/help-search.js b/lib/help-search.js index 9648e3b1478d5..4e727c3e72954 100644 --- a/lib/help-search.js +++ b/lib/help-search.js @@ -1,21 +1,23 @@ const fs = require('fs') const path = require('path') const color = require('ansicolors') -const usageUtil = require('./utils/usage.js') 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 { - constructor (npm) { - this.npm = npm +class HelpSearch extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'help-search' } - get usage () { - return usageUtil('help-search', 'npm help-search ') + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [''] } exec (args, cb) { diff --git a/lib/help.js b/lib/help.js index ef7e3bfd03214..93abf878ba26f 100644 --- a/lib/help.js +++ b/lib/help.js @@ -5,16 +5,17 @@ const log = require('npmlog') const openUrl = require('./utils/open-url.js') const glob = require('glob') -const usage = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Help { - constructor (npm) { - this.npm = npm +class Help extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'help' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usage('help', 'npm help []') + static get usage () { + return [' []'] } async completion (opts) { diff --git a/lib/hook.js b/lib/hook.js index a6f04d6532e50..6cda3504f43d7 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -2,20 +2,20 @@ const hookApi = require('libnpmhook') const otplease = require('./utils/otplease.js') const relativeDate = require('tiny-relative-date') const Table = require('cli-table3') -const usageUtil = require('./utils/usage.js') -class Hook { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Hook extends BaseCommand { + static get name () { + return 'hook' } - get usage () { - return usageUtil('hook', [ - 'npm hook add [--type=]', - 'npm hook ls [pkg]', - 'npm hook rm ', - 'npm hook update ', - ].join('\n')) + static get usage () { + return [ + 'add [--type=]', + 'ls [pkg]', + 'rm ', + 'update ', + ] } exec (args, cb) { diff --git a/lib/init.js b/lib/init.js index 3f9abbcdd354d..42b02dfdc6a77 100644 --- a/lib/init.js +++ b/lib/init.js @@ -1,21 +1,21 @@ const initJson = require('init-package-json') const npa = require('npm-package-arg') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Init { - constructor (npm) { - this.npm = npm +class Init extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'init' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'init', - '\nnpm init [--force|-f|--yes|-y|--scope]' + - '\nnpm init <@scope> (same as `npx <@scope>/create`)' + - '\nnpm init [<@scope>/] (same as `npx [<@scope>/]create-`)' - ) + static get usage () { + return [ + '[--force|-f|--yes|-y|--scope]', + '<@scope> (same as `npx <@scope>/create`)', + '[<@scope>/] (same as `npx [<@scope>/]create-`)', + ] } exec (args, cb) { diff --git a/lib/install-ci-test.js b/lib/install-ci-test.js index d1740999d4b67..c52b5c9e8073f 100644 --- a/lib/install-ci-test.js +++ b/lib/install-ci-test.js @@ -1,19 +1,12 @@ // npm install-ci-test // Runs `npm ci` and then runs `npm test` -const usageUtil = require('./utils/usage.js') +const CI = require('./ci.js') -class InstallCITest { - constructor (npm) { - this.npm = npm - } - - get usage () { - return usageUtil( - 'install-ci-test', - 'npm install-ci-test [args]' + - '\nSame args as `npm ci`' - ) +class InstallCITest extends CI { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'install-ci-test' } exec (args, cb) { diff --git a/lib/install-test.js b/lib/install-test.js index 487f8da00b6d3..76c6f367dd3c8 100644 --- a/lib/install-test.js +++ b/lib/install-test.js @@ -1,23 +1,12 @@ // npm install-test // Runs `npm install` and then runs `npm test` -const usageUtil = require('./utils/usage.js') +const Install = require('./install.js') -class InstallTest { - constructor (npm) { - this.npm = npm - } - - get usage () { - return usageUtil( - 'install-test', - 'npm install-test [args]' + - '\nSame args as `npm install`' - ) - } - - async completion (opts) { - return this.npm.commands.install.completion(opts) +class InstallTest extends Install { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'install-test' } exec (args, cb) { diff --git a/lib/install.js b/lib/install.js index d7fd384d5bd6f..8df63a219ef74 100644 --- a/lib/install.js +++ b/lib/install.js @@ -3,35 +3,33 @@ const fs = require('fs') const util = require('util') const readdir = util.promisify(fs.readdir) -const usageUtil = require('./utils/usage.js') const reifyFinish = require('./utils/reify-finish.js') const log = require('npmlog') const { resolve, join } = require('path') const Arborist = require('@npmcli/arborist') const runScript = require('@npmcli/run-script') -class Install { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Install extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'install' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'install', - 'npm install (with no args, in package dir)' + - '\nnpm install [<@scope>/]' + - '\nnpm install [<@scope>/]@' + - '\nnpm install [<@scope>/]@' + - '\nnpm install [<@scope>/]@' + - '\nnpm install @npm:' + - '\nnpm install ' + - '\nnpm install ' + - '\nnpm install ' + - '\nnpm install ' + - '\nnpm install /', - '[--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save]' - ) + static get usage () { + return [ + '[<@scope>/]', + '[<@scope>/]@', + '[<@scope>/]@', + '[<@scope>/]@', + '@npm:', + '', + '', + '', + '', + '/ [--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save]', + ] } async completion (opts) { diff --git a/lib/link.js b/lib/link.js index 6d5e207105825..66f83d9f5b0a7 100644 --- a/lib/link.js +++ b/lib/link.js @@ -8,21 +8,21 @@ const npa = require('npm-package-arg') const rpj = require('read-package-json-fast') const semver = require('semver') -const usageUtil = require('./utils/usage.js') const reifyFinish = require('./utils/reify-finish.js') -class Link { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Link extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'link' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'link', - 'npm link (in package dir)' + - '\nnpm link [<@scope>/][@]' - ) + static get usage () { + return [ + '(in package dir)', + '[<@scope>/][@]', + ] } async completion (opts) { diff --git a/lib/ll.js b/lib/ll.js index 7915f5d27c011..3e3428a7ff5eb 100644 --- a/lib/ll.js +++ b/lib/ll.js @@ -1,13 +1,14 @@ const LS = require('./ls.js') -const usageUtil = require('./utils/usage.js') class LL extends LS { /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'll', - 'npm ll [[<@scope>/] ...]' - ) + static get name () { + return 'll' + } + + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['[[<@scope>/] ...]'] } exec (args, cb) { diff --git a/lib/logout.js b/lib/logout.js index 9fb1eab21a152..b3f64f671d326 100644 --- a/lib/logout.js +++ b/lib/logout.js @@ -1,19 +1,17 @@ const log = require('npmlog') const getAuth = require('npm-registry-fetch/auth.js') const npmFetch = require('npm-registry-fetch') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Logout { - constructor (npm) { - this.npm = npm +class Logout extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'logout' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'logout', - 'npm logout [--registry=] [--scope=<@scope>]' - ) + static get usage () { + return ['[--registry=] [--scope=<@scope>]'] } exec (args, cb) { diff --git a/lib/ls.js b/lib/ls.js index b94684401e6f6..9ff2761c2f928 100644 --- a/lib/ls.js +++ b/lib/ls.js @@ -7,7 +7,6 @@ const Arborist = require('@npmcli/arborist') const { breadth } = require('treeverse') const npa = require('npm-package-arg') -const usageUtil = require('./utils/usage.js') const completion = require('./utils/completion/installed-deep.js') const _depth = Symbol('depth') @@ -21,18 +20,17 @@ const _parent = Symbol('parent') const _problems = Symbol('problems') const _required = Symbol('required') const _type = Symbol('type') +const BaseCommand = require('./base-command.js') -class LS { - constructor (npm) { - this.npm = npm +class LS extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'ls' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'ls', - 'npm ls [[<@scope>/] ...]' - ) + static get usage () { + return ['npm ls [[<@scope>/] ...]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/org.js b/lib/org.js index 2a08941a83f3a..b9f84b060f8a8 100644 --- a/lib/org.js +++ b/lib/org.js @@ -1,20 +1,21 @@ const liborg = require('libnpmorg') -const usageUtil = require('./utils/usage.js') const otplease = require('./utils/otplease.js') const Table = require('cli-table3') +const BaseCommand = require('./base-command.js') -class Org { - constructor (npm) { - this.npm = npm +class Org extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'org' } - get usage () { - return usageUtil( - 'org', - 'npm org set orgname username [developer | admin | owner]\n' + - 'npm org rm orgname username\n' + - 'npm org ls orgname []' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'set orgname username [developer | admin | owner]', + 'rm orgname username', + 'ls orgname []', + ] } async completion (opts) { diff --git a/lib/outdated.js b/lib/outdated.js index be5820870411c..7225577ea42d4 100644 --- a/lib/outdated.js +++ b/lib/outdated.js @@ -9,19 +9,18 @@ const pickManifest = require('npm-pick-manifest') const Arborist = require('@npmcli/arborist') -const usageUtil = require('./utils/usage.js') const ansiTrim = require('./utils/ansi-trim.js') +const BaseCommand = require('./base-command.js') -class Outdated { - constructor (npm) { - this.npm = npm +class Outdated extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'outdated' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('outdated', - 'npm outdated [[<@scope>/] ...]' - ) + static get usage () { + return ['[[<@scope>/] ...]'] } exec (args, cb) { diff --git a/lib/owner.js b/lib/owner.js index cd387e94d9eaf..b62f125ac3bb6 100644 --- a/lib/owner.js +++ b/lib/owner.js @@ -5,24 +5,21 @@ const pacote = require('pacote') const otplease = require('./utils/otplease.js') const readLocalPkg = require('./utils/read-local-package.js') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Owner { - constructor (npm) { - this.npm = npm +class Owner extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'owner' } - get usage () { - return usageUtil( - 'owner', - 'npm owner add [<@scope>/]' + - '\nnpm owner rm [<@scope>/]' + - '\nnpm owner ls [<@scope>/]' - ) - } - - get usageError () { - return Object.assign(new Error(this.usage), { code: 'EUSAGE' }) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'add [<@scope>/]', + 'rm [<@scope>/]', + 'ls [<@scope>/]', + ] } async completion (opts) { @@ -69,7 +66,7 @@ class Owner { case 'remove': return this.rm(args[0], args[1], opts) default: - throw this.usageError + throw this.usageError() } } @@ -77,7 +74,7 @@ class Owner { if (!pkg) { const pkgName = await readLocalPkg(this.npm) if (!pkgName) - throw this.usageError + throw this.usageError() pkg = pkgName } @@ -101,12 +98,12 @@ class Owner { async add (user, pkg, opts) { if (!user) - throw this.usageError + throw this.usageError() if (!pkg) { const pkgName = await readLocalPkg(this.npm) if (!pkgName) - throw this.usageError + throw this.usageError() pkg = pkgName } @@ -119,12 +116,12 @@ class Owner { async rm (user, pkg, opts) { if (!user) - throw this.usageError + throw this.usageError() if (!pkg) { const pkgName = await readLocalPkg(this.npm) if (!pkgName) - throw this.usageError + throw this.usageError() pkg = pkgName } diff --git a/lib/pack.js b/lib/pack.js index 7ffe3138ed55f..326fcc0cd1441 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -8,16 +8,17 @@ const { getContents, logTar } = require('./utils/tar.js') const writeFile = util.promisify(require('fs').writeFile) -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Pack { - constructor (npm) { - this.npm = npm +class Pack extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'pack' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('pack', 'npm pack [[<@scope>/]...] [--dry-run]') + static get usage () { + return ['[[<@scope>/]...] [--dry-run]'] } exec (args, cb) { diff --git a/lib/ping.js b/lib/ping.js index 3643fe3b621aa..e60b1f1debd80 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -1,15 +1,16 @@ const log = require('npmlog') -const usageUtil = require('./utils/usage.js') const pingUtil = require('./utils/ping.js') +const BaseCommand = require('./base-command.js') -class Ping { - constructor (npm) { - this.npm = npm +class Ping extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'ping' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('ping', 'npm ping\nping registry') + static get description () { + return 'ping registry' } exec (args, cb) { diff --git a/lib/prefix.js b/lib/prefix.js index 8ec5ab9efcf78..5ade87f6429f6 100644 --- a/lib/prefix.js +++ b/lib/prefix.js @@ -1,13 +1,14 @@ -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Prefix { - constructor (npm) { - this.npm = npm +class Prefix extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'prefix' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('prefix', 'npm prefix [-g]') + static get usage () { + return ['[-g]'] } exec (args, cb) { diff --git a/lib/profile.js b/lib/profile.js index a0a8606014da4..1c0df49888540 100644 --- a/lib/profile.js +++ b/lib/profile.js @@ -9,7 +9,6 @@ const Table = require('cli-table3') const otplease = require('./utils/otplease.js') const pulseTillDone = require('./utils/pulse-till-done.js') const readUserInfo = require('./utils/read-user-info.js') -const usageUtil = require('./utils/usage.js') const qrcode = url => new Promise((resolve) => qrcodeTerminal.generate(url, resolve)) @@ -37,19 +36,21 @@ const writableProfileKeys = [ 'github', ] -class Profile { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Profile extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'profile' } - get usage () { - return usageUtil( - 'profile', - 'npm profile enable-2fa [auth-only|auth-and-writes]\n', - 'npm profile disable-2fa\n', - 'npm profile get []\n', - 'npm profile set ' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'enable-2fa [auth-only|auth-and-writes]', + 'disable-2fa', + 'get []', + 'set ', + ] } async completion (opts) { diff --git a/lib/prune.js b/lib/prune.js index b839301d5194c..c2cddb1a22b33 100644 --- a/lib/prune.js +++ b/lib/prune.js @@ -1,18 +1,17 @@ // prune extraneous packages const Arborist = require('@npmcli/arborist') -const usageUtil = require('./utils/usage.js') const reifyFinish = require('./utils/reify-finish.js') -class Prune { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Prune extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'prune' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('prune', - 'npm prune [[<@scope>/]...] [--production]' - ) + static get usage () { + return ['[[<@scope>/]...] [--production]'] } exec (args, cb) { diff --git a/lib/publish.js b/lib/publish.js index b0bf922138ce3..f8e0eafe11886 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -10,7 +10,6 @@ const npmFetch = require('npm-registry-fetch') const { flatten } = require('./utils/flat-options.js') const otplease = require('./utils/otplease.js') -const usageUtil = require('./utils/usage.js') const { getContents, logTar } = require('./utils/tar.js') // this is the only case in the CLI where we use the old full slow @@ -18,16 +17,18 @@ const { getContents, logTar } = require('./utils/tar.js') // defaults and metadata, like git sha's and default scripts and all that. const readJson = util.promisify(require('read-package-json')) -class Publish { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Publish extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'publish' } - get usage () { - return usageUtil('publish', - 'npm publish [] [--tag ] [--access ] [--dry-run]' + - '\n\nPublishes \'.\' if no argument supplied' + - '\nSets tag `latest` if no --tag specified') + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + '[] [--tag ] [--access ] [--dry-run]', + ] } exec (args, cb) { diff --git a/lib/rebuild.js b/lib/rebuild.js index ffbdebc21fa27..74f5ae5f6eba5 100644 --- a/lib/rebuild.js +++ b/lib/rebuild.js @@ -2,17 +2,18 @@ const { resolve } = require('path') const Arborist = require('@npmcli/arborist') const npa = require('npm-package-arg') const semver = require('semver') -const usageUtil = require('./utils/usage.js') const completion = require('./utils/completion/installed-deep.js') -class Rebuild { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Rebuild extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'rebuild' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('rebuild', 'npm rebuild [[<@scope>/][@] ...]') + static get usage () { + return ['[[<@scope>/][@] ...]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/repo.js b/lib/repo.js index f0be99d4d69e9..aa07e07a819f7 100644 --- a/lib/repo.js +++ b/lib/repo.js @@ -4,16 +4,17 @@ const { URL } = require('url') const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js') const openUrl = require('./utils/open-url.js') -const usageUtil = require('./utils/usage.js') -class Repo { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Repo extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'repo' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('repo', 'npm repo [ [ ...]]') + static get usage () { + return ['[ [ ...]]'] } exec (args, cb) { diff --git a/lib/restart.js b/lib/restart.js index d5a7789ca92c0..1f3eb5af94f82 100644 --- a/lib/restart.js +++ b/lib/restart.js @@ -2,8 +2,9 @@ const LifecycleCmd = require('./utils/lifecycle-cmd.js') // This ends up calling run-script(['restart', ...args]) class Restart extends LifecycleCmd { - constructor (npm) { - super(npm, 'restart') + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'restart' } } module.exports = Restart diff --git a/lib/root.js b/lib/root.js index 7c3fa2bbb3544..1fe82c6fad773 100644 --- a/lib/root.js +++ b/lib/root.js @@ -1,13 +1,13 @@ -const usageUtil = require('./utils/usage.js') - -class Root { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Root extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'root' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('root', 'npm root [-g]') + static get usage () { + return ['[-g]'] } exec (args, cb) { diff --git a/lib/run-script.js b/lib/run-script.js index dc822668d0318..3ea85b79ffd18 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -3,7 +3,6 @@ const { isServerPackage } = runScript const readJson = require('read-package-json-fast') const { resolve } = require('path') const log = require('npmlog') -const usageUtil = require('./utils/usage.js') const didYouMean = require('./utils/did-you-mean.js') const isWindowsShell = require('./utils/is-windows-shell.js') @@ -18,17 +17,16 @@ const cmdList = [ 'version', ].reduce((l, p) => l.concat(['pre' + p, p, 'post' + p]), []) -class RunScript { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class RunScript extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'run-script' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'run-script', - 'npm run-script [-- ]' - ) + static get usage () { + return [' [-- ]'] } async completion (opts) { diff --git a/lib/search.js b/lib/search.js index 35e3eeb0e552c..c24000156f67a 100644 --- a/lib/search.js +++ b/lib/search.js @@ -5,7 +5,6 @@ const log = require('npmlog') const formatPackageStream = require('./search/format-package-stream.js') const packageFilter = require('./search/package-filter.js') -const usageUtil = require('./utils/usage.js') function prepareIncludes (args) { return args @@ -25,17 +24,16 @@ function prepareExcludes (searchexclude) { .filter(s => s) } -class Search { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Search extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'search' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'search', - 'npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...]' - ) + static get usage () { + return ['[-l|--long] [--json] [--parseable] [--no-description] [search terms ...]'] } exec (args, cb) { diff --git a/lib/set-script.js b/lib/set-script.js index 25545898e1640..6241981323c4a 100644 --- a/lib/set-script.js +++ b/lib/set-script.js @@ -1,17 +1,18 @@ const log = require('npmlog') -const usageUtil = require('./utils/usage.js') const fs = require('fs') const parseJSON = require('json-parse-even-better-errors') const rpj = require('read-package-json-fast') -class SetScript { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class SetScript extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'set-script' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('set-script', 'npm set-script [