From 56698e5e15ee2769f6f8c2b96d5bd1f14da4108c 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. --- lib/access.js | 48 +++++------- lib/adduser.js | 16 ++-- lib/audit.js | 21 +++-- lib/base-command.js | 34 ++++++++ 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 | 24 +++--- lib/init.js | 22 +++--- lib/install-ci-test.js | 17 ++-- lib/install-test.js | 21 ++--- lib/install.js | 39 +++++----- 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 | 13 ++-- 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 +++--- tap-snapshots/test-lib-publish.js-TAP.test.js | 3 - .../test-lib-utils-npm-usage.js-TAP.test.js | 78 +++++++------------ test/lib/access.js | 2 +- test/lib/config.js | 13 +--- test/lib/load-all-commands.js | 4 +- test/lib/owner.js | 36 ++------- test/lib/utils/lifecycle-cmd.js | 7 +- 73 files changed, 625 insertions(+), 711 deletions(-) create mode 100644 lib/base-command.js diff --git a/lib/access.js b/lib/access.js index e11934af43ebc..e77ed3c6d7ac6 100644 --- a/lib/access.js +++ b/lib/access.js @@ -5,8 +5,8 @@ const readPackageJson = require('read-package-json-fast') const output = require('./utils/output.js') 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', @@ -20,24 +20,25 @@ const subcommands = [ '2fa-not-required', ] -class Access { - constructor (npm) { - this.npm = npm +class Access extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + 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 []' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'public []', + 'restricted []', + 'grant []', + 'revoke []', + '2fa-required []', + '2fa-not-required []', + 'ls-packages [||]', + 'ls-collaborators [ []]', + 'edit []', + ] } async completion (opts) { @@ -67,12 +68,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]) { @@ -203,12 +199,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 dac0f5a46840d..dcb0de8fb225f 100644 --- a/lib/adduser.js +++ b/lib/adduser.js @@ -1,7 +1,7 @@ const log = require('npmlog') const output = require('./utils/output.js') -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'), @@ -9,17 +9,15 @@ const authTypes = { sso: require('./auth/sso.js'), } -class AddUser { - constructor (npm) { - this.npm = npm +class AddUser extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + 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 dfa01cb2709fa..c134d858ada22 100644 --- a/lib/audit.js +++ b/lib/audit.js @@ -3,21 +3,20 @@ const auditReport = require('npm-audit-report') const output = require('./utils/output.js') 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..0f0487cb0402a --- /dev/null +++ b/lib/base-command.js @@ -0,0 +1,34 @@ +// Base class for npm.commands[cmd] +const usageUtil = require('./utils/usage.js') + +class BaseCommand { + constructor (npm) { + this.npm = npm + } + + get usage () { + let usage = '' + if (this.constructor.description) + usage = `${this.constructor.description}\n\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')}` + + return usageUtil(this.constructor.name, 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 11490c41cbcc5..f033f7e1c0edc 100644 --- a/lib/bin.js +++ b/lib/bin.js @@ -1,15 +1,16 @@ const output = require('./utils/output.js') 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 { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + 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..7c542f2790bc4 100644 --- a/lib/bugs.js +++ b/lib/bugs.js @@ -1,17 +1,18 @@ 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 { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + 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 8469559764fb3..0f473ad4b9a5b 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -5,23 +5,25 @@ const output = require('./utils/output.js') 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 4c37e6ef354ef..8663a48c79049 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -42,17 +42,18 @@ const isWindowsShell = require('./utils/is-windows-shell.js') const output = require('./utils/output.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 2805db9b80ec7..d14db41992b6f 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 output = require('./utils/output.js') const mkdirp = require('mkdirp-infer-owner') @@ -29,22 +28,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) { @@ -254,10 +253,6 @@ ${defData} } 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 859e6f76feeef..2ec92c0467bd7 100644 --- a/lib/diff.js +++ b/lib/diff.js @@ -8,24 +8,25 @@ const npmlog = require('npmlog') const pacote = require('pacote') const pickManifest = require('npm-pick-manifest') -const usageUtil = require('./utils/usage.js') const output = require('./utils/output.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 171a88c527e5d..283b1e3bf5f15 100644 --- a/lib/dist-tag.js +++ b/lib/dist-tag.js @@ -6,20 +6,21 @@ const semver = require('semver') const output = require('./utils/output.js') 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 81860004e344e..b09b8132d37d4 100644 --- a/lib/doctor.js +++ b/lib/doctor.js @@ -12,7 +12,6 @@ const ansiTrim = require('./utils/ansi-trim.js') const isWindows = require('./utils/is-windows.js') const output = require('./utils/output.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) @@ -32,14 +31,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 d1db49128587e..8ea6d8ebb6804 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -1,5 +1,4 @@ const output = require('./utils/output.js') -const usageUtil = require('./utils/usage.js') const { promisify } = require('util') const read = promisify(require('read')) const mkdirp = require('mkdirp-infer-owner') @@ -13,6 +12,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: // @@ -39,31 +39,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 01541040ef649..bd3d1359e67f2 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 output = require('./utils/output.js') @@ -7,15 +6,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 fdfe6e1bcf7c8..cf9c5774fb7d7 100644 --- a/lib/explore.js +++ b/lib/explore.js @@ -6,16 +6,17 @@ const runScript = require('@npmcli/run-script') const { join, resolve, relative } = require('path') const completion = require('./utils/completion/installed-shallow.js') const output = require('./utils/output.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 1e9724266401f..7456411772683 100644 --- a/lib/fund.js +++ b/lib/fund.js @@ -14,25 +14,23 @@ const { const completion = require('./utils/completion/installed-deep.js') const output = require('./utils/output.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 ed2bc23b9109d..568fb27b7300c 100644 --- a/lib/help-search.js +++ b/lib/help-search.js @@ -2,21 +2,23 @@ const fs = require('fs') const path = require('path') const color = require('ansicolors') const output = require('./utils/output.js') -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 d7897326f3118..7ceab6472de63 100644 --- a/lib/help.js +++ b/lib/help.js @@ -6,16 +6,17 @@ const openUrl = require('./utils/open-url.js') const glob = require('glob') const output = require('./utils/output.js') -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 312f542d7cff6..e4b9389d89391 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -3,20 +3,22 @@ const output = require('./utils/output.js') 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 { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + 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')) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'add [--type=]', + 'ls [pkg]', + 'rm ', + 'update ', + ] } exec (args, cb) { diff --git a/lib/init.js b/lib/init.js index af97a9614e368..a81b7e4f7ded7 100644 --- a/lib/init.js +++ b/lib/init.js @@ -1,22 +1,22 @@ const initJson = require('init-package-json') const npa = require('npm-package-arg') -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') const output = require('./utils/output.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..77a305018a107 100644 --- a/lib/install.js +++ b/lib/install.js @@ -3,35 +3,34 @@ 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 [ + '(with no args, in package dir)', + '[<@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 359fe21e6f8cc..788a258b4eaf5 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 output = require('./utils/output.js') @@ -22,18 +21,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 054e1833dba4b..9f0348e65929c 100644 --- a/lib/org.js +++ b/lib/org.js @@ -1,21 +1,22 @@ const liborg = require('libnpmorg') -const usageUtil = require('./utils/usage.js') const output = require('./utils/output.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 fc6967faf60fe..5a953008a4f5b 100644 --- a/lib/outdated.js +++ b/lib/outdated.js @@ -10,19 +10,18 @@ const pickManifest = require('npm-pick-manifest') const Arborist = require('@npmcli/arborist') const output = require('./utils/output.js') -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 6cb9904880dc2..962a972801759 100644 --- a/lib/owner.js +++ b/lib/owner.js @@ -6,24 +6,21 @@ const pacote = require('pacote') const output = require('./utils/output.js') 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) { @@ -70,7 +67,7 @@ class Owner { case 'remove': return this.rm(args[0], args[1], opts) default: - throw this.usageError + throw this.usageError() } } @@ -78,7 +75,7 @@ class Owner { if (!pkg) { const pkgName = await readLocalPkg(this.npm) if (!pkgName) - throw this.usageError + throw this.usageError() pkg = pkgName } @@ -102,12 +99,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 cf1e77f48ee69..141784a9a8b1e 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -9,16 +9,17 @@ const { getContents, logTar } = require('./utils/tar.js') const writeFile = util.promisify(require('fs').writeFile) const output = require('./utils/output.js') -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 e43f0640f212b..b3df6abefb1d4 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -1,16 +1,17 @@ const log = require('npmlog') const output = require('./utils/output.js') -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 e46f9c4cdd94a..d44b4fd189b3b 100644 --- a/lib/prefix.js +++ b/lib/prefix.js @@ -1,14 +1,15 @@ const output = require('./utils/output.js') -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 dab99092b0a0f..2a823a1eb7508 100644 --- a/lib/profile.js +++ b/lib/profile.js @@ -10,7 +10,6 @@ const otplease = require('./utils/otplease.js') const output = require('./utils/output.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)) @@ -38,19 +37,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 c8e82c44c5a3c..ff45d6a395e1a 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -11,7 +11,6 @@ const npmFetch = require('npm-registry-fetch') const { flatten } = require('./utils/flat-options.js') const output = require('./utils/output.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 @@ -19,16 +18,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 1091b01589389..56f811c558a1e 100644 --- a/lib/rebuild.js +++ b/lib/rebuild.js @@ -2,18 +2,19 @@ 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 output = require('./utils/output.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 8e5ac63d7b9b8..f9dad07ef6ca8 100644 --- a/lib/root.js +++ b/lib/root.js @@ -1,14 +1,15 @@ const output = require('./utils/output.js') -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 cdfd88f10f7b8..ce535088b602e 100644 --- a/lib/run-script.js +++ b/lib/run-script.js @@ -4,7 +4,6 @@ const readJson = require('read-package-json-fast') const { resolve } = require('path') const output = require('./utils/output.js') 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') @@ -19,17 +18,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 e0922b9846cdb..5470a086b654d 100644 --- a/lib/search.js +++ b/lib/search.js @@ -6,7 +6,6 @@ const log = require('npmlog') const formatPackageStream = require('./search/format-package-stream.js') const packageFilter = require('./search/package-filter.js') const output = require('./utils/output.js') -const usageUtil = require('./utils/usage.js') function prepareIncludes (args) { return args @@ -26,17 +25,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 [