diff options
author | Gar <gar+gh@danger.computer> | 2021-03-04 07:26:33 +0300 |
---|---|---|
committer | Gar <gar+gh@danger.computer> | 2021-03-09 22:10:06 +0300 |
commit | 9fe0df5b5d7606e5841288d9931be6c04767c9ca (patch) | |
tree | 50b916523fe4a5ebab6010b137cc931cc2a9ce70 | |
parent | 85a8694dd9b4a924a474ba75261914511a216868 (diff) |
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
84 files changed, 1023 insertions, 760 deletions
diff --git a/lib/access.js b/lib/access.js index 3bc211190..0df36beea 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 [<package>]\n' + - 'npm access restricted [<package>]\n' + - 'npm access grant <read-only|read-write> <scope:team> [<package>]\n' + - 'npm access revoke <scope:team> [<package>]\n' + - 'npm access 2fa-required [<package>]\n' + - 'npm access 2fa-not-required [<package>]\n' + - 'npm access ls-packages [<user>|<scope>|<scope:team>]\n' + - 'npm access ls-collaborators [<package> [<user>]]\n' + - 'npm access edit [<package>]' - ) + static get usage () { + return [ + 'public [<package>]', + 'restricted [<package>]', + 'grant <read-only|read-write> <scope:team> [<package>]', + 'revoke <scope:team> [<package>]', + '2fa-required [<package>]', + '2fa-not-required [<package>]', + 'ls-packages [<user>|<scope>|<scope:team>]', + 'ls-collaborators [<package> [<user>]]', + 'edit [<package>]', + ] } 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 45d602fd2..da318a1f3 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 b8c85605d..6e64987b6 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 000000000..c31e4a4d7 --- /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 f540cc57c..1450fb539 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 fb0d7c927..1814dd7bc 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 [<pkgname>]') + static get usage () { + return ['[<pkgname>]'] } exec (args, cb) { diff --git a/lib/cache.js b/lib/cache.js index 3ca99fd25..80a5c68eb 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 <tarball file>' + - '\nnpm cache add <folder>' + - '\nnpm cache add <tarball url>' + - '\nnpm cache add <git url>' + - '\nnpm cache add <name>@<version>' + - '\nnpm cache clean' + - '\nnpm cache verify' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'add <tarball file>', + 'add <folder>', + 'add <tarball url>', + 'add <git url>', + 'add <name>@<version>', + 'clean', + 'verify', + ] } async completion (opts) { @@ -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 5baf17665..3ee68cdac 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 7009f4601..c29253e43 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 <key>=<value> [<key>=<value> ...]' + - '\nnpm config get [<key> [<key> ...]]' + - '\nnpm config delete <key> [<key> ...]' + - '\nnpm config list [--json]' + - '\nnpm config edit' + - '\nnpm set <key>=<value> [<key>=<value> ...]' + - '\nnpm get [<key> [<key> ...]]' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'set <key>=<value> [<key>=<value> ...]', + 'get [<key> [<key> ...]]', + 'delete <key> [<key> ...]', + '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 59978895e..50a56211f 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 48f27ab6c..a0c67f805 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 <pkg>[@<version>] <message>' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['<pkg>[@<version>] <message>'] } 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 ed36a3067..0e322ec64 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 [...<paths>]' + - '\nnpm diff --diff=<pkg-name> [...<paths>]' + - '\nnpm diff --diff=<version-a> [--diff=<version-b>] [...<paths>]' + - '\nnpm diff --diff=<spec-a> [--diff=<spec-b>] [...<paths>]' + - '\nnpm diff [--diff-ignore-all-space] [--diff-name-only] [...<paths>] [...<paths>]' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + '[...<paths>]', + '--diff=<pkg-name> [...<paths>]', + '--diff=<version-a> [--diff=<version-b>] [...<paths>]', + '--diff=<spec-a> [--diff=<spec-b>] [...<paths>]', + '[--diff-ignore-all-space] [--diff-name-only] [...<paths>] [...<paths>]', + ] } get where () { diff --git a/lib/dist-tag.js b/lib/dist-tag.js index 4b7e26020..cdc95ac6f 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 <pkg>@<version> [<tag>]' + - '\nnpm dist-tag rm <pkg> <tag>' + - '\nnpm dist-tag ls [<pkg>]' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'add <pkg>@<version> [<tag>]', + 'rm <pkg> <tag>', + 'ls [<pkg>]', + ] } async completion (opts) { diff --git a/lib/doctor.js b/lib/doctor.js index 63619d0cf..fbe447141 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 a7dbb3820..1dbe8e4c1 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 <pkg>[/<subpkg>...]') + static get usage () { + return ['<pkg>[/<subpkg>...]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/exec.js b/lib/exec.js index 69c3cfe75..b2443b17a 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 -- <pkg>[@<version>] [args...]\n' + - 'npm exec --package=<pkg>[@<version>] -- <cmd> [args...]\n' + - 'npm exec -c \'<cmd> [args...]\'\n' + - 'npm exec --package=foo -c \'<cmd> [args...]\'\n' + - '\n' + - 'npx <pkg>[@<specifier>] [args...]\n' + - 'npx -p <pkg>[@<specifier>] <cmd> [args...]\n' + - 'npx -c \'<cmd> [args...]\'\n' + - 'npx -p <pkg>[@<specifier>] -c \'<cmd> [args...]\'' + - '\n' + - 'Run without --call or positional args to open interactive subshell\n', - - '\n--package=<pkg> (may be specified multiple times)\n' + - '-p is a shorthand for --package only when using npx executable\n' + - '-c <cmd> --call=<cmd> (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 [ + '-- <pkg>[@<version>] [args...]', + '--package=<pkg>[@<version>] -- <cmd> [args...]', + '-c \'<cmd> [args...]\'', + '--package=foo -c \'<cmd> [args...]\'', + ] } exec (args, cb) { diff --git a/lib/explain.js b/lib/explain.js index f46d3b507..6af761186 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 <folder | specifier>') + static get usage () { + return ['<folder | specifier>'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/explore.js b/lib/explore.js index e09e86740..34f6d1079 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 <pkg> [ -- <command>]') + static get usage () { + return ['<pkg> [ -- <command>]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/find-dupes.js b/lib/find-dupes.js index 5061be9cc..ecb945f47 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 826c3170e..a723c62d2 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>/]<pkg> [--which=<fundingSourceNumber>]' - ) + static get usage () { + return ['[--json] [--browser] [--unicode] [[<@scope>/]<pkg> [--which=<fundingSourceNumber>]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/get.js b/lib/get.js index a5b2f5514..a5d58accc 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 [<key> ...] (See `npm config`)' - ) + static get usage () { + return ['[<key> ...] (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 9648e3b14..4e727c3e7 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 <text>') + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['<text>'] } exec (args, cb) { diff --git a/lib/help.js b/lib/help.js index ef7e3bfd0..93abf878b 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 <term> [<terms..>]') + static get usage () { + return ['<term> [<terms..>]'] } async completion (opts) { diff --git a/lib/hook.js b/lib/hook.js index a6f04d653..6cda3504f 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 <pkg> <url> <secret> [--type=<type>]', - 'npm hook ls [pkg]', - 'npm hook rm <id>', - 'npm hook update <id> <url> <secret>', - ].join('\n')) + static get usage () { + return [ + 'add <pkg> <url> <secret> [--type=<type>]', + 'ls [pkg]', + 'rm <id>', + 'update <id> <url> <secret>', + ] } exec (args, cb) { diff --git a/lib/init.js b/lib/init.js index 3f9abbcdd..42b02dfdc 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>/]<name> (same as `npx [<@scope>/]create-<name>`)' - ) + static get usage () { + return [ + '[--force|-f|--yes|-y|--scope]', + '<@scope> (same as `npx <@scope>/create`)', + '[<@scope>/]<name> (same as `npx [<@scope>/]create-<name>`)', + ] } exec (args, cb) { diff --git a/lib/install-ci-test.js b/lib/install-ci-test.js index d1740999d..c52b5c9e8 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 487f8da00..76c6f367d 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 d7fd384d5..8df63a219 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>/]<pkg>' + - '\nnpm install [<@scope>/]<pkg>@<tag>' + - '\nnpm install [<@scope>/]<pkg>@<version>' + - '\nnpm install [<@scope>/]<pkg>@<version range>' + - '\nnpm install <alias>@npm:<name>' + - '\nnpm install <folder>' + - '\nnpm install <tarball file>' + - '\nnpm install <tarball url>' + - '\nnpm install <git:// url>' + - '\nnpm install <github username>/<github project>', - '[--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save]' - ) + static get usage () { + return [ + '[<@scope>/]<pkg>', + '[<@scope>/]<pkg>@<tag>', + '[<@scope>/]<pkg>@<version>', + '[<@scope>/]<pkg>@<version range>', + '<alias>@npm:<name>', + '<folder>', + '<tarball file>', + '<tarball url>', + '<git:// url>', + '<github username>/<github project> [--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 6d5e20710..66f83d9f5 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>/]<pkg>[@<version>]' - ) + static get usage () { + return [ + '(in package dir)', + '[<@scope>/]<pkg>[@<version>]', + ] } async completion (opts) { @@ -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>/]<pkg> ...]' - ) + static get name () { + return 'll' + } + + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['[[<@scope>/]<pkg> ...]'] } exec (args, cb) { diff --git a/lib/logout.js b/lib/logout.js index 9fb1eab21..b3f64f671 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=<url>] [--scope=<@scope>]' - ) + static get usage () { + return ['[--registry=<url>] [--scope=<@scope>]'] } exec (args, cb) { @@ -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>/]<pkg> ...]' - ) + static get usage () { + return ['npm ls [[<@scope>/]<pkg> ...]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/org.js b/lib/org.js index 2a08941a8..b9f84b060 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 [<username>]' - ) + /* 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 [<username>]', + ] } async completion (opts) { diff --git a/lib/outdated.js b/lib/outdated.js index be5820870..7225577ea 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>/]<pkg> ...]' - ) + static get usage () { + return ['[[<@scope>/]<pkg> ...]'] } exec (args, cb) { diff --git a/lib/owner.js b/lib/owner.js index cd387e94d..b62f125ac 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 <user> [<@scope>/]<pkg>' + - '\nnpm owner rm <user> [<@scope>/]<pkg>' + - '\nnpm owner ls [<@scope>/]<pkg>' - ) - } - - 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 <user> [<@scope>/]<pkg>', + 'rm <user> [<@scope>/]<pkg>', + 'ls [<@scope>/]<pkg>', + ] } 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 7ffe3138e..326fcc0cd 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>/]<pkg>...] [--dry-run]') + static get usage () { + return ['[[<@scope>/]<pkg>...] [--dry-run]'] } exec (args, cb) { diff --git a/lib/ping.js b/lib/ping.js index 3643fe3b6..e60b1f1de 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 8ec5ab9ef..5ade87f64 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 a0a860601..1c0df4988 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 [<key>]\n', - 'npm profile set <key> <value>' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'enable-2fa [auth-only|auth-and-writes]', + 'disable-2fa', + 'get [<key>]', + 'set <key> <value>', + ] } async completion (opts) { diff --git a/lib/prune.js b/lib/prune.js index b839301d5..c2cddb1a2 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>/]<pkg>...] [--production]' - ) + static get usage () { + return ['[[<@scope>/]<pkg>...] [--production]'] } exec (args, cb) { diff --git a/lib/publish.js b/lib/publish.js index b0bf92213..f8e0eafe1 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 [<folder>] [--tag <tag>] [--access <public|restricted>] [--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 [ + '[<folder>] [--tag <tag>] [--access <public|restricted>] [--dry-run]', + ] } exec (args, cb) { diff --git a/lib/rebuild.js b/lib/rebuild.js index ffbdebc21..74f5ae5f6 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>/]<name>[@<version>] ...]') + static get usage () { + return ['[[<@scope>/]<name>[@<version>] ...]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/repo.js b/lib/repo.js index f0be99d4d..aa07e07a8 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 [<pkgname> [<pkgname> ...]]') + static get usage () { + return ['[<pkgname> [<pkgname> ...]]'] } exec (args, cb) { diff --git a/lib/restart.js b/lib/restart.js index d5a7789ca..1f3eb5af9 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 7c3fa2bbb..1fe82c6fa 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 dc822668d..3ea85b79f 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 <command> [-- <args>]' - ) + static get usage () { + return ['<command> [-- <args>]'] } async completion (opts) { diff --git a/lib/search.js b/lib/search.js index 35e3eeb0e..c24000156 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 25545898e..624198132 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 [<script>] [<command>]') + static get usage () { + return ['[<script>] [<command>]'] } exec (args, cb) { diff --git a/lib/set.js b/lib/set.js index cbce1547e..787a8012c 100644 --- a/lib/set.js +++ b/lib/set.js @@ -1,15 +1,14 @@ -const usageUtil = require('./utils/usage.js') +const BaseCommand = require('./base-command.js') -class Set { - constructor (npm) { - this.npm = npm +class Set extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'set' } - get usage () { - return usageUtil( - 'set', - 'npm set <key>=<value> [<key>=<value> ...] (See `npm config`)' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['<key>=<value> [<key>=<value> ...] (See `npm config`)'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index a7516131d..b52cf4195 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -5,16 +5,11 @@ const { unlink } = fs.promises || { unlink: util.promisify(fs.unlink) } const Arborist = require('@npmcli/arborist') const log = require('npmlog') -const usageUtil = require('./utils/usage.js') - -class Shrinkwrap { - constructor (npm) { - this.npm = npm - } - +const BaseCommand = require('./base-command.js') +class Shrinkwrap extends BaseCommand { /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('shrinkwrap', 'npm shrinkwrap') + static get name () { + return 'shrinkwrap' } exec (args, cb) { diff --git a/lib/star.js b/lib/star.js index 073c93a89..27a304190 100644 --- a/lib/star.js +++ b/lib/star.js @@ -2,20 +2,18 @@ const fetch = require('npm-registry-fetch') const log = require('npmlog') const npa = require('npm-package-arg') -const usageUtil = require('./utils/usage.js') const getIdentity = require('./utils/get-identity') -class Star { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Star extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'star' } - get usage () { - return usageUtil( - 'star', - 'npm star [<pkg>...]\n' + - 'npm unstar [<pkg>...]' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['[<pkg>...]'] } exec (args, cb) { diff --git a/lib/stars.js b/lib/stars.js index e0a6a0003..758a3130d 100644 --- a/lib/stars.js +++ b/lib/stars.js @@ -2,16 +2,17 @@ const log = require('npmlog') const fetch = require('npm-registry-fetch') const getIdentity = require('./utils/get-identity.js') -const usageUtil = require('./utils/usage.js') -class Stars { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Stars extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'stars' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil('stars', 'npm stars [<user>]') + static get usage () { + return ['[<user>]'] } exec (args, cb) { diff --git a/lib/start.js b/lib/start.js index 3d46a3a7b..8987bc293 100644 --- a/lib/start.js +++ b/lib/start.js @@ -2,8 +2,9 @@ const LifecycleCmd = require('./utils/lifecycle-cmd.js') // This ends up calling run-script(['start', ...args]) class Start extends LifecycleCmd { - constructor (npm) { - super(npm, 'start') + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'start' } } module.exports = Start diff --git a/lib/stop.js b/lib/stop.js index d7df5887e..a3857ab13 100644 --- a/lib/stop.js +++ b/lib/stop.js @@ -2,8 +2,9 @@ const LifecycleCmd = require('./utils/lifecycle-cmd.js') // This ends up calling run-script(['stop', ...args]) class Stop extends LifecycleCmd { - constructor (npm) { - super(npm, 'stop') + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'stop' } } module.exports = Stop diff --git a/lib/team.js b/lib/team.js index 3ba2c023d..f84660af4 100644 --- a/lib/team.js +++ b/lib/team.js @@ -2,22 +2,23 @@ const columns = require('cli-columns') const libteam = require('libnpmteam') const otplease = require('./utils/otplease.js') -const usageUtil = require('./utils/usage.js') -class Team { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Team extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'team' } - get usage () { - return usageUtil( - 'team', - 'npm team create <scope:team> [--otp <otpcode>]\n' + - 'npm team destroy <scope:team> [--otp <otpcode>]\n' + - 'npm team add <scope:team> <user> [--otp <otpcode>]\n' + - 'npm team rm <scope:team> <user> [--otp <otpcode>]\n' + - 'npm team ls <scope>|<scope:team>\n' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'create <scope:team> [--otp <otpcode>]', + 'destroy <scope:team> [--otp <otpcode>]', + 'add <scope:team> <user> [--otp <otpcode>]', + 'rm <scope:team> <user> [--otp <otpcode>]', + 'ls <scope>|<scope:team>', + ] } async completion (opts) { diff --git a/lib/test.js b/lib/test.js index 12292d292..991d1c873 100644 --- a/lib/test.js +++ b/lib/test.js @@ -2,8 +2,9 @@ const LifecycleCmd = require('./utils/lifecycle-cmd.js') // This ends up calling run-script(['test', ...args]) class Test extends LifecycleCmd { - constructor (npm) { - super(npm, 'test') + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'test' } exec (args, cb) { diff --git a/lib/token.js b/lib/token.js index ad634c0b0..3d7952ccf 100644 --- a/lib/token.js +++ b/lib/token.js @@ -7,19 +7,21 @@ const profile = require('npm-profile') 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') -class Token { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Token extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'token' } - get usage () { - return usageUtil('token', - 'npm token list\n' + - 'npm token revoke <id|token>\n' + - 'npm token create [--read-only] [--cidr=list]' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return [ + 'list', + 'revoke <id|token>', + 'create [--read-only] [--cidr=list]', + ] } async completion (opts) { @@ -188,12 +190,6 @@ class Token { return conf } - usageError (msg) { - return Object.assign(new Error(`\nUsage: ${msg}\n\n` + this.usage), { - code: 'EUSAGE', - }) - } - invalidCIDRError (msg) { return Object.assign(new Error(msg), { code: 'EINVALIDCIDR' }) } diff --git a/lib/uninstall.js b/lib/uninstall.js index d7116e4c2..ee0f338e9 100644 --- a/lib/uninstall.js +++ b/lib/uninstall.js @@ -2,20 +2,19 @@ const { resolve } = require('path') const Arborist = require('@npmcli/arborist') const rpj = require('read-package-json-fast') -const usageUtil = require('./utils/usage.js') const reifyFinish = require('./utils/reify-finish.js') const completion = require('./utils/completion/installed-shallow.js') -class Uninstall { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Uninstall extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'uninstall' } - get usage () { - return usageUtil( - 'uninstall', - 'npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|--no-save]' - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['[<@scope>/]<pkg>[@<version>]... [-S|--save|--no-save]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/unpublish.js b/lib/unpublish.js index acba6ea52..68a9a0ae6 100644 --- a/lib/unpublish.js +++ b/lib/unpublish.js @@ -7,17 +7,19 @@ const npmFetch = require('npm-registry-fetch') const libunpub = require('libnpmpublish').unpublish const readJson = util.promisify(require('read-package-json')) -const usageUtil = require('./utils/usage.js') const otplease = require('./utils/otplease.js') const getIdentity = require('./utils/get-identity.js') -class Unpublish { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Unpublish extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'unpublish' } - get usage () { - return usageUtil('unpublish', 'npm unpublish [<@scope>/]<pkg>[@<version>]') + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['[<@scope>/]<pkg>[@<version>]'] } async completion (args) { diff --git a/lib/unstar.js b/lib/unstar.js index c814ef2b6..5786cfce6 100644 --- a/lib/unstar.js +++ b/lib/unstar.js @@ -1,6 +1,11 @@ const Star = require('./star.js') class Unstar extends Star { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'unstar' + } + exec (args, cb) { this.npm.config.set('star.unstar', true) super.exec(args, cb) diff --git a/lib/update.js b/lib/update.js index 98043e09c..87540b96e 100644 --- a/lib/update.js +++ b/lib/update.js @@ -3,21 +3,19 @@ const path = require('path') const Arborist = require('@npmcli/arborist') const log = require('npmlog') -const usageUtil = require('./utils/usage.js') const reifyFinish = require('./utils/reify-finish.js') const completion = require('./utils/completion/installed-deep.js') -class Update { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Update extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'update' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'update', - 'npm update [-g] [<pkg>...]' - ) + static get usage () { + return ['[-g] [<pkg>...]'] } /* istanbul ignore next - see test/lib/load-all-commands.js */ diff --git a/lib/utils/lifecycle-cmd.js b/lib/utils/lifecycle-cmd.js index 8be9b5a12..1917bef36 100644 --- a/lib/utils/lifecycle-cmd.js +++ b/lib/utils/lifecycle-cmd.js @@ -1,19 +1,14 @@ // The implementation of commands that are just "run a script" // restart, start, stop, test -const usageUtil = require('./usage.js') -class LifecycleCmd { - constructor (npm, stage) { - this.npm = npm - this.stage = stage - } - - get usage () { - return usageUtil(this.stage, `npm ${this.stage} [-- <args>]`) +const BaseCommand = require('../base-command.js') +class LifecycleCmd extends BaseCommand { + static get usage () { + return ['[-- <args>]'] } exec (args, cb) { - this.npm.commands['run-script']([this.stage, ...args], cb) + this.npm.commands['run-script']([this.constructor.name, ...args], cb) } } module.exports = LifecycleCmd diff --git a/lib/version.js b/lib/version.js index a7c0c1955..2eda9d11b 100644 --- a/lib/version.js +++ b/lib/version.js @@ -1,20 +1,15 @@ const libversion = require('libnpmversion') -const usageUtil = require('./utils/usage.js') -class Version { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Version extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'version' } - get usage () { - return usageUtil( - 'version', - 'npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]\n' + - '(run in package dir)\n\n' + - `'npm -v' or 'npm --version' to print npm version (${this.npm.version})\n` + - `'npm view <pkg> version' to view a package's published version\n` + - `'npm ls' to inspect current package/dependency versions\n` - ) + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get usage () { + return ['[<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]'] } async completion (opts) { diff --git a/lib/view.js b/lib/view.js index d0d5fa59d..0a6688fc2 100644 --- a/lib/view.js +++ b/lib/view.js @@ -14,22 +14,19 @@ const style = require('ansistyles') const { inspect, promisify } = require('util') const { packument } = require('pacote') -const usageUtil = require('./utils/usage.js') - const readFile = promisify(fs.readFile) const readJson = async file => jsonParse(await readFile(file, 'utf8')) -class View { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class View extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'view' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'view', - 'npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...]' - ) + static get usage () { + return ['[<@scope>/]<pkg>[@<version>] [<field>[.subfield]...]'] } async completion (opts) { diff --git a/lib/whoami.js b/lib/whoami.js index 7ce877104..2322c5fd8 100644 --- a/lib/whoami.js +++ b/lib/whoami.js @@ -1,18 +1,20 @@ const getIdentity = require('./utils/get-identity.js') -const usageUtil = require('./utils/usage.js') -class Whoami { - constructor (npm) { - this.npm = npm +const BaseCommand = require('./base-command.js') +class Whoami extends BaseCommand { + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get name () { + return 'whoami' + } + + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get description () { + return 'prints username according to given registry' } /* istanbul ignore next - see test/lib/load-all-commands.js */ - get usage () { - return usageUtil( - 'whoami', - 'npm whoami [--registry <registry>]\n' + - '(just prints username according to given registry)' - ) + static get usage () { + return ['[--registry <registry>]'] } exec (args, cb) { diff --git a/tap-snapshots/test-lib-dist-tag.js-TAP.test.js b/tap-snapshots/test-lib-dist-tag.js-TAP.test.js index b135b1001..46aa6ce91 100644 --- a/tap-snapshots/test-lib-dist-tag.js-TAP.test.js +++ b/tap-snapshots/test-lib-dist-tag.js-TAP.test.js @@ -6,19 +6,29 @@ */ 'use strict' exports[`test/lib/dist-tag.js TAP add missing args > should exit usage error message 1`] = ` +npm dist-tag + +Usage: npm dist-tag add <pkg>@<version> [<tag>] npm dist-tag rm <pkg> <tag> npm dist-tag ls [<pkg>] alias: dist-tags + +Run "npm dist-tag help" for more info ` exports[`test/lib/dist-tag.js TAP add missing pkg name > should exit usage error message 1`] = ` +npm dist-tag + +Usage: npm dist-tag add <pkg>@<version> [<tag>] npm dist-tag rm <pkg> <tag> npm dist-tag ls [<pkg>] alias: dist-tags + +Run "npm dist-tag help" for more info ` exports[`test/lib/dist-tag.js TAP add new tag > should return success msg 1`] = ` @@ -31,11 +41,16 @@ dist-tag add 1.0.0 to @scoped/another@7.7.7 ` exports[`test/lib/dist-tag.js TAP borked cmd usage > should show usage error 1`] = ` +npm dist-tag + +Usage: npm dist-tag add <pkg>@<version> [<tag>] npm dist-tag rm <pkg> <tag> npm dist-tag ls [<pkg>] alias: dist-tags + +Run "npm dist-tag help" for more info ` exports[`test/lib/dist-tag.js TAP ls in current package > should list available tags for current package 1`] = ` @@ -45,11 +60,16 @@ latest: 1.0.0 ` exports[`test/lib/dist-tag.js TAP ls on missing name in current package > should throw usage error message 1`] = ` +npm dist-tag + +Usage: npm dist-tag add <pkg>@<version> [<tag>] npm dist-tag rm <pkg> <tag> npm dist-tag ls [<pkg>] alias: dist-tags + +Run "npm dist-tag help" for more info ` exports[`test/lib/dist-tag.js TAP ls on missing package > should log no dist-tag found msg 1`] = ` @@ -89,11 +109,16 @@ exports[`test/lib/dist-tag.js TAP remove existing tag > should return success ms ` exports[`test/lib/dist-tag.js TAP remove missing pkg name > should exit usage error message 1`] = ` +npm dist-tag + +Usage: npm dist-tag add <pkg>@<version> [<tag>] npm dist-tag rm <pkg> <tag> npm dist-tag ls [<pkg>] alias: dist-tags + +Run "npm dist-tag help" for more info ` exports[`test/lib/dist-tag.js TAP remove non-existing tag > should log error msg 1`] = ` diff --git a/tap-snapshots/test-lib-publish.js-TAP.test.js b/tap-snapshots/test-lib-publish.js-TAP.test.js index 6af6c13f5..6fad3e26b 100644 --- a/tap-snapshots/test-lib-publish.js-TAP.test.js +++ b/tap-snapshots/test-lib-publish.js-TAP.test.js @@ -6,8 +6,10 @@ */ 'use strict' exports[`test/lib/publish.js TAP shows usage with wrong set of arguments > should print usage 1`] = ` +npm publish + +Usage: npm publish [<folder>] [--tag <tag>] [--access <public|restricted>] [--dry-run] -Publishes '.' if no argument supplied -Sets tag \`latest\` if no --tag specified +Run "npm publish help" for more info ` diff --git a/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js b/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js index 8e1603eb4..3ff008d07 100644 --- a/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js +++ b/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js @@ -207,7 +207,10 @@ npm help npm more involved overview All commands: - access npm access public [<package>] + access npm access + + Usage: + npm access public [<package>] npm access restricted [<package>] npm access grant <read-only|read-write> <scope:team> [<package>] npm access revoke <scope:team> [<package>] @@ -216,121 +219,226 @@ All commands: npm access ls-packages [<user>|<scope>|<scope:team>] npm access ls-collaborators [<package> [<user>]] npm access edit [<package>] + + Run "npm access help" for more info - adduser npm adduser [--registry=url] [--scope=@orgname] [--always-auth] + adduser npm adduser + + Usage: + npm adduser [--registry=url] [--scope=@orgname] [--always-auth] aliases: login, add-user + + Run "npm adduser help" for more info - audit npm audit [--json] [--production] + audit npm audit + + Usage: + npm audit [--json] [--production] npm audit fix [--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)] + + Run "npm audit help" for more info - bin npm bin [-g] + bin npm bin + + Usage: + npm bin [-g] + + Run "npm bin help" for more info - bugs npm bugs [<pkgname>] + bugs npm bugs + + Usage: + npm bugs [<pkgname>] alias: issues + + Run "npm bugs help" for more info - cache npm cache add <tarball file> + cache npm cache + + Usage: + npm cache add <tarball file> npm cache add <folder> npm cache add <tarball url> npm cache add <git url> npm cache add <name>@<version> npm cache clean npm cache verify + + Run "npm cache help" for more info ci npm ci + Usage: + npm ci + aliases: clean-install, ic, install-clean, isntall-clean + + Run "npm ci help" for more info - completion source <(npm completion) + completion npm completion + + npm command completion script. save to ~/.bashrc or ~/.zshrc + + Usage: + npm completion + + Run "npm completion help" for more info - config npm config set <key>=<value> [<key>=<value> ...] + config npm config + + Usage: + npm config set <key>=<value> [<key>=<value> ...] npm config get [<key> [<key> ...]] npm config delete <key> [<key> ...] npm config list [--json] npm config edit - npm set <key>=<value> [<key>=<value> ...] - npm get [<key> [<key> ...]] alias: c + + Run "npm config help" for more info dedupe npm dedupe + Usage: + npm dedupe + alias: ddp + + Run "npm dedupe help" for more info - deprecate npm deprecate <pkg>[@<version>] <message> + deprecate npm deprecate + + Usage: + npm deprecate <pkg>[@<version>] <message> + + Run "npm deprecate help" for more info - diff npm diff [...<paths>] + diff npm diff + + Usage: + npm diff [...<paths>] npm diff --diff=<pkg-name> [...<paths>] npm diff --diff=<version-a> [--diff=<version-b>] [...<paths>] npm diff --diff=<spec-a> [--diff=<spec-b>] [...<paths>] npm diff [--diff-ignore-all-space] [--diff-name-only] [...<paths>] [...<paths>] + + Run "npm diff help" for more info - dist-tag npm dist-tag add <pkg>@<version> [<tag>] + dist-tag npm dist-tag + + Usage: + npm dist-tag add <pkg>@<version> [<tag>] npm dist-tag rm <pkg> <tag> npm dist-tag ls [<pkg>] alias: dist-tags + + Run "npm dist-tag help" for more info docs npm docs [<pkgname> [<pkgname> ...]] alias: home doctor npm doctor + + Usage: + npm doctor + + Run "npm doctor help" for more info - edit npm edit <pkg>[/<subpkg>...] + edit npm edit + + Usage: + npm edit <pkg>[/<subpkg>...] + + Run "npm edit help" for more info - exec Run a command from a local or remote npm package. + exec npm exec + Run a command from a local or remote npm package. + + Usage: npm exec -- <pkg>[@<version>] [args...] npm exec --package=<pkg>[@<version>] -- <cmd> [args...] npm exec -c '<cmd> [args...]' npm exec --package=foo -c '<cmd> [args...]' - npx <pkg>[@<specifier>] [args...] - npx -p <pkg>[@<specifier>] <cmd> [args...] - npx -c '<cmd> [args...]' - npx -p <pkg>[@<specifier>] -c '<cmd> [args...]' - Run without --call or positional args to open interactive subshell - - alias: x - common options: - --package=<pkg> (may be specified multiple times) - -p is a shorthand for --package only when using npx executable - -c <cmd> --call=<cmd> (may not be mixed with positional arguments) + + Run "npm exec help" for more info - explain npm explain <folder | specifier> + explain npm explain + + Usage: + npm explain <folder | specifier> alias: why + + Run "npm explain help" for more info - explore npm explore <pkg> [ -- <command>] + explore npm explore + + Usage: + npm explore <pkg> [ -- <command>] + + Run "npm explore help" for more info find-dupes npm find-dupes + + Usage: + npm find-dupes + + Run "npm find-dupes help" for more info fund npm fund - common options: npm fund [--json] [--browser] [--unicode] [[<@scope>/]<pkg> [--which=<fundingSourceNumber>] + Usage: + npm fund [--json] [--browser] [--unicode] [[<@scope>/]<pkg> [--which=<fundingSourceNumber>] + + Run "npm fund help" for more info - get npm get [<key> ...] (See \`npm config\`) + get npm get + + Usage: + npm get [<key> ...] (See \`npm config\`) + + Run "npm get help" for more info - help npm help <term> [<terms..>] + help npm help + + Usage: + npm help <term> [<terms..>] alias: hlep + + Run "npm help help" for more info - hook npm hook add <pkg> <url> <secret> [--type=<type>] + hook npm hook + + Usage: + npm hook add <pkg> <url> <secret> [--type=<type>] npm hook ls [pkg] npm hook rm <id> npm hook update <id> <url> <secret> + + Run "npm hook help" for more info - init + init npm init + + Usage: npm init [--force|-f|--yes|-y|--scope] npm init <@scope> (same as \`npx <@scope>/create\`) npm init [<@scope>/]<name> (same as \`npx [<@scope>/]create-<name>\`) aliases: create, innit + + Run "npm init help" for more info - install npm install (with no args, in package dir) + install npm install + + Usage: npm install [<@scope>/]<pkg> npm install [<@scope>/]<pkg>@<tag> npm install [<@scope>/]<pkg>@<version> @@ -340,151 +448,343 @@ All commands: npm install <tarball file> npm install <tarball url> npm install <git:// url> - npm install <github username>/<github project> + npm install <github username>/<github project> [--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save] aliases: i, in, ins, inst, insta, instal, isnt, isnta, isntal, add - common options: [--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save] + + Run "npm install help" for more info - install-ci-test npm install-ci-test [args] - Same args as \`npm ci\` + install-ci-test npm install-ci-test + + Usage: + npm install-ci-test alias: cit + + Run "npm install-ci-test help" for more info - install-test npm install-test [args] - Same args as \`npm install\` + install-test npm install-test + + Usage: + npm install-test [<@scope>/]<pkg> + npm install-test [<@scope>/]<pkg>@<tag> + npm install-test [<@scope>/]<pkg>@<version> + npm install-test [<@scope>/]<pkg>@<version range> + npm install-test <alias>@npm:<name> + npm install-test <folder> + npm install-test <tarball file> + npm install-test <tarball url> + npm install-test <git:// url> + npm install-test <github username>/<github project> [--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save] alias: it + + Run "npm install-test help" for more info - link npm link (in package dir) + link npm link + + Usage: + npm link (in package dir) npm link [<@scope>/]<pkg>[@<version>] alias: ln + + Run "npm link help" for more info - ll npm ll [[<@scope>/]<pkg> ...] + ll npm ll + + Usage: + npm ll [[<@scope>/]<pkg> ...] alias: la + + Run "npm ll help" for more info - login npm adduser [--registry=url] [--scope=@orgname] [--always-auth] + login npm adduser + + Usage: + npm adduser [--registry=url] [--scope=@orgname] [--always-auth] aliases: login, add-user + + Run "npm adduser help" for more info - logout npm logout [--registry=<url>] [--scope=<@scope>] + logout npm logout + + Usage: + npm logout [--registry=<url>] [--scope=<@scope>] + + Run "npm logout help" for more info - ls npm ls [[<@scope>/]<pkg> ...] + ls npm ls + + Usage: + npm ls npm ls [[<@scope>/]<pkg> ...] alias: list + + Run "npm ls help" for more info - org npm org set orgname username [developer | admin | owner] + org npm org + + Usage: + npm org set orgname username [developer | admin | owner] npm org rm orgname username npm org ls orgname [<username>] alias: ogr + + Run "npm org help" for more info - outdated npm outdated [[<@scope>/]<pkg> ...] + outdated npm outdated + + Usage: + npm outdated [[<@scope>/]<pkg> ...] + + Run "npm outdated help" for more info - owner npm owner add <user> [<@scope>/]<pkg> + owner npm owner + + Usage: + npm owner add <user> [<@scope>/]<pkg> npm owner rm <user> [<@scope>/]<pkg> npm owner ls [<@scope>/]<pkg> alias: author + + Run "npm owner help" for more info - pack npm pack [[<@scope>/]<pkg>...] [--dry-run] + pack npm pack + + Usage: + npm pack [[<@scope>/]<pkg>...] [--dry-run] + + Run "npm pack help" for more info ping npm ping + ping registry + + Usage: + npm ping + + Run "npm ping help" for more info - prefix npm prefix [-g] - - profile npm profile enable-2fa [auth-only|auth-and-writes] + prefix npm prefix + Usage: + npm prefix [-g] - common options: npm profile disable-2fa + Run "npm prefix help" for more info + + profile npm profile + + Usage: + npm profile enable-2fa [auth-only|auth-and-writes] + npm profile disable-2fa + npm profile get [<key>] + npm profile set <key> <value> + Run "npm profile help" for more info - prune npm prune [[<@scope>/]<pkg>...] [--production] + prune npm prune + + Usage: + npm prune [[<@scope>/]<pkg>...] [--production] + + Run "npm prune help" for more info - publish npm publish [<folder>] [--tag <tag>] [--access <public|restricted>] [--dry-run] + publish npm publish - Publishes '.' if no argument supplied - Sets tag \`latest\` if no --tag specified + Usage: + npm publish [<folder>] [--tag <tag>] [--access <public|restricted>] [--dry-run] + + Run "npm publish help" for more info - rebuild npm rebuild [[<@scope>/]<name>[@<version>] ...] + rebuild npm rebuild + + Usage: + npm rebuild [[<@scope>/]<name>[@<version>] ...] alias: rb + + Run "npm rebuild help" for more info - repo npm repo [<pkgname> [<pkgname> ...]] + repo npm repo + + Usage: + npm repo [<pkgname> [<pkgname> ...]] + + Run "npm repo help" for more info - restart npm restart [-- <args>] + restart npm restart + + Usage: + npm restart [-- <args>] + + Run "npm restart help" for more info - root npm root [-g] + root npm root + + Usage: + npm root [-g] + + Run "npm root help" for more info - run-script npm run-script <command> [-- <args>] + run-script npm run-script + + Usage: + npm run-script <command> [-- <args>] aliases: run, rum, urn + + Run "npm run-script help" for more info - search npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...] + search npm search + + Usage: + npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...] aliases: s, se, find + + Run "npm search help" for more info - set npm set <key>=<value> [<key>=<value> ...] (See \`npm config\`) + set npm set + + Usage: + npm set <key>=<value> [<key>=<value> ...] (See \`npm config\`) + + Run "npm set help" for more info - set-script npm set-script [<script>] [<command>] + set-script npm set-script + + Usage: + npm set-script [<script>] [<command>] + + Run "npm set-script help" for more info shrinkwrap npm shrinkwrap + + Usage: + npm shrinkwrap + + Run "npm shrinkwrap help" for more info - star npm star [<pkg>...] - npm unstar [<pkg>...] + star npm star + + Usage: + npm star [<pkg>...] + + Run "npm star help" for more info - stars npm stars [<user>] + stars npm stars + + Usage: + npm stars [<user>] + + Run "npm stars help" for more info - start npm start [-- <args>] + start npm start + + Usage: + npm start [-- <args>] + + Run "npm start help" for more info - stop npm stop [-- <args>] + stop npm stop + + Usage: + npm stop [-- <args>] + + Run "npm stop help" for more info - team npm team create <scope:team> [--otp <otpcode>] + team npm team + + Usage: + npm team create <scope:team> [--otp <otpcode>] npm team destroy <scope:team> [--otp <otpcode>] npm team add <scope:team> <user> [--otp <otpcode>] npm team rm <scope:team> <user> [--otp <otpcode>] npm team ls <scope>|<scope:team> + Run "npm team help" for more info - test npm test [-- <args>] + test npm test + + Usage: + npm test [-- <args>] aliases: tst, t + + Run "npm test help" for more info - token npm token list + token npm token + + Usage: + npm token list npm token revoke <id|token> npm token create [--read-only] [--cidr=list] + + Run "npm token help" for more info - uninstall npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|--no-save] + uninstall npm uninstall + + Usage: + npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|--no-save] aliases: un, unlink, remove, rm, r + + Run "npm uninstall help" for more info - unpublish npm unpublish [<@scope>/]<pkg>[@<version>] + unpublish npm unpublish + + Usage: + npm unpublish [<@scope>/]<pkg>[@<version>] + + Run "npm unpublish help" for more info - unstar npm star [<pkg>...] + unstar npm unstar + + Usage: npm unstar [<pkg>...] + + Run "npm unstar help" for more info - update npm update [-g] [<pkg>...] + update npm update + + Usage: + npm update [-g] [<pkg>...] aliases: up, upgrade, udpate - - version npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git] - (run in package dir) - 'npm -v' or 'npm --version' to print npm version ({VERSION}) - 'npm view <pkg> version' to view a package's published version - 'npm ls' to inspect current package/dependency versions + Run "npm update help" for more info + + version npm version + Usage: + npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git] alias: verison + + Run "npm version help" for more info - view npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...] + view npm view + + Usage: + npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...] aliases: v, info, show + + Run "npm view help" for more info - whoami npm whoami [--registry <registry>] - (just prints username according to given registry) + whoami npm whoami + + prints username according to given registry + + Usage: + npm whoami [--registry <registry>] + + Run "npm whoami help" for more info Specify configs in the ini-formatted file: /some/config/file/.npmrc diff --git a/test/lib/access.js b/test/lib/access.js index 8134c1d91..a8e28c8eb 100644 --- a/test/lib/access.js +++ b/test/lib/access.js @@ -41,7 +41,7 @@ test('completion', t => { test('subcommand required', t => { const access = new Access({ flatOptions: {} }) access.exec([], (err) => { - t.equal(err, '\nUsage: Subcommand is required.\n\n' + access.usage) + t.match(err, access.usageError('Subcommand is required.')) t.end() }) }) diff --git a/test/lib/adduser.js b/test/lib/adduser.js index 106cd429e..d9106e1bd 100644 --- a/test/lib/adduser.js +++ b/test/lib/adduser.js @@ -78,6 +78,10 @@ const AddUser = requireInject('../../lib/adduser.js', { const adduser = new AddUser(npm) +test('usage', (t) => { + t.match(adduser.usage, 'adduser', 'usage has command name in it') + t.end() +}) test('simple login', (t) => { adduser.exec([], (err) => { t.ifError(err, 'npm adduser') diff --git a/test/lib/bin.js b/test/lib/bin.js index 512fa8d02..428b2e3ba 100644 --- a/test/lib/bin.js +++ b/test/lib/bin.js @@ -2,7 +2,7 @@ const { test } = require('tap') const requireInject = require('require-inject') test('bin', (t) => { - t.plan(3) + t.plan(4) const dir = '/bin/dir' const Bin = require('../../lib/bin.js') @@ -15,6 +15,7 @@ test('bin', (t) => { }, } const bin = new Bin(npm) + t.match(bin.usage, 'bin', 'usage has command name in it') bin.exec([], (err) => { t.ifError(err, 'npm bin') diff --git a/test/lib/bugs.js b/test/lib/bugs.js index e98131f11..21b1a9841 100644 --- a/test/lib/bugs.js +++ b/test/lib/bugs.js @@ -55,6 +55,11 @@ const Bugs = requireInject('../../lib/bugs.js', { const bugs = new Bugs({ flatOptions: {} }) +t.test('usage', (t) => { + t.match(bugs.usage, 'bugs', 'usage has command name in it') + t.end() +}) + t.test('open bugs urls', t => { const expect = { nobugs: 'https://www.npmjs.com/package/nobugs', diff --git a/test/lib/cache.js b/test/lib/cache.js index 5c2588f34..773adc6a8 100644 --- a/test/lib/cache.js +++ b/test/lib/cache.js @@ -70,7 +70,7 @@ const cache = new Cache(npm) t.test('cache no args', t => { cache.exec([], err => { - t.equal(err.message, 'usage instructions', 'should throw usage instructions') + t.match(err.message, 'usage instructions', 'should throw usage instructions') t.end() }) }) diff --git a/test/lib/config.js b/test/lib/config.js index 48934ba4e..3aeb29f8d 100644 --- a/test/lib/config.js +++ b/test/lib/config.js @@ -180,12 +180,7 @@ t.test('config list --json', t => { t.test('config delete no args', t => { config.exec(['delete'], (err) => { - t.equal( - err.message, - 'usage instructions', - 'should throw usage error' - ) - t.equal(err.code, 'EUSAGE', 'should throw expected error code') + t.match(err, { message: '\nUsage: usage instructions' }) t.end() }) }) @@ -265,11 +260,7 @@ t.test('config delete key --global', t => { t.test('config set no args', t => { config.exec(['set'], (err) => { - t.equal( - err.message, - 'usage instructions', - 'should throw usage error' - ) + t.match(err, { message: '\nUsage: usage instructions' }) t.end() }) }) diff --git a/test/lib/deprecate.js b/test/lib/deprecate.js index 03100166a..e278a3e3c 100644 --- a/test/lib/deprecate.js +++ b/test/lib/deprecate.js @@ -60,14 +60,14 @@ test('completion', async t => { test('no args', t => { deprecate.exec([], (err) => { - t.match(err, /Usage: npm deprecate/, 'logs usage') + t.match(err, 'Usage:', 'logs usage') t.end() }) }) test('only one arg', t => { deprecate.exec(['foo'], (err) => { - t.match(err, /Usage: npm deprecate/, 'logs usage') + t.match(err, 'Usage:', 'logs usage') t.end() }) }) diff --git a/test/lib/explore.js b/test/lib/explore.js index af16444ca..a0655380e 100644 --- a/test/lib/explore.js +++ b/test/lib/explore.js @@ -338,7 +338,7 @@ t.test('usage if no pkg provided', t => { for (const args of noPkg) { t.test(JSON.stringify(args), t => { posixExplore.exec(args, er => { - t.equal(er, 'npm explore <pkg> [ -- <command>]') + t.match(er, 'Usage:') t.strictSame({ ERROR_HANDLER_CALLED: null, RPJ_CALLED, diff --git a/test/lib/load-all-commands.js b/test/lib/load-all-commands.js index f6d1ae9e1..e31a2b993 100644 --- a/test/lib/load-all-commands.js +++ b/test/lib/load-all-commands.js @@ -1,6 +1,6 @@ // Thanks to nyc not working properly with proxies this -// doesn't affect coverage. but it does ensure that every command -// has a usage, and if it has completion it is a function +// doesn't affect coverage. but it does ensure that every command has a usage +// that contains its name, and if it has completion it is a function const npm = require('../../lib/npm.js') const t = require('tap') const { cmdList } = require('../../lib/utils/cmd-list.js') diff --git a/test/lib/npm.js b/test/lib/npm.js index 1f7a54e22..87cbea8f2 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -357,7 +357,7 @@ t.test('loading as main will load the cli', t => { p.on('close', (code, signal) => { t.equal(code, 0) t.equal(signal, null) - t.equal(Buffer.concat(out).toString().trim(), ls.usage) + t.match(Buffer.concat(out).toString(), ls.usage) t.end() }) }) diff --git a/test/lib/owner.js b/test/lib/owner.js index 14753689f..11aeacb7b 100644 --- a/test/lib/owner.js +++ b/test/lib/owner.js @@ -42,11 +42,7 @@ t.test('owner no args', t => { }) owner.exec([], err => { - t.equal( - err.message, - 'usage instructions', - 'should throw usage instructions' - ) + t.match(err, /usage instructions/, 'should not error out on empty locations') t.end() }) }) @@ -89,11 +85,7 @@ t.test('owner ls no args no cwd package', t => { }) owner.exec(['ls'], err => { - t.equal( - err.message, - 'usage instructions', - 'should throw usage instructions if no cwd package available' - ) + t.match(err, /usage instructions/, 'should throw usage instructions if no cwd package available') t.end() }) }) @@ -469,11 +461,7 @@ t.test('owner add no user', t => { }) owner.exec(['add'], err => { - t.equal( - err.message, - 'usage instructions', - 'should throw usage instructions if no user provided' - ) + t.match(err, /usage instructions/, 'should throw usage instructions if user provided') t.end() }) }) @@ -485,11 +473,7 @@ t.test('owner add <user> no cwd package', t => { }) owner.exec(['add', 'foo'], err => { - t.equal( - err.message, - 'usage instructions', - 'should throw usage instructions if no user provided' - ) + t.match(err, /usage instructions/, 'should throw usage instructions if no user provided') t.end() }) }) @@ -676,11 +660,7 @@ t.test('owner rm no user', t => { }) owner.exec(['rm'], err => { - t.equal( - err.message, - 'usage instructions', - 'should throw usage instructions if no user provided to rm' - ) + t.match(err, /usage instructions/, 'should throw usage instructions if no user provided to rm') t.end() }) }) @@ -692,11 +672,7 @@ t.test('owner rm <user> no cwd package', t => { }) owner.exec(['rm', 'foo'], err => { - t.equal( - err.message, - 'usage instructions', - 'should throw usage instructions if no user provided to rm' - ) + t.match(err, /usage instructions/, 'should throw usage instructions if no user provided to rm') t.end() }) }) diff --git a/test/lib/restart.js b/test/lib/restart.js index f29592d9b..9719476c4 100644 --- a/test/lib/restart.js +++ b/test/lib/restart.js @@ -10,7 +10,6 @@ const npm = { } const Restart = require('../../lib/restart.js') const restart = new Restart(npm) -t.equal(restart.usage, 'npm restart [-- <args>]') restart.exec(['foo'], () => { t.match(runArgs, ['restart', 'foo']) t.end() diff --git a/test/lib/start.js b/test/lib/start.js index 9a3328309..4e77f9691 100644 --- a/test/lib/start.js +++ b/test/lib/start.js @@ -10,7 +10,6 @@ const npm = { } const Start = require('../../lib/start.js') const start = new Start(npm) -t.equal(start.usage, 'npm start [-- <args>]') start.exec(['foo'], () => { t.match(runArgs, ['start', 'foo']) t.end() diff --git a/test/lib/stop.js b/test/lib/stop.js index e6cb193b6..92ca84bd8 100644 --- a/test/lib/stop.js +++ b/test/lib/stop.js @@ -10,7 +10,6 @@ const npm = { } const Stop = require('../../lib/stop.js') const stop = new Stop(npm) -t.equal(stop.usage, 'npm stop [-- <args>]') stop.exec(['foo'], () => { t.match(runArgs, ['stop', 'foo']) t.end() diff --git a/test/lib/utils/lifecycle-cmd.js b/test/lib/utils/lifecycle-cmd.js index 2f1f693f2..3e3a7da43 100644 --- a/test/lib/utils/lifecycle-cmd.js +++ b/test/lib/utils/lifecycle-cmd.js @@ -10,7 +10,12 @@ const npm = { }, } t.test('create a lifecycle command', t => { - const cmd = new LifecycleCmd(npm, 'test-stage') + class TestStage extends LifecycleCmd { + static get name () { + return 'test-stage' + } + } + const cmd = new TestStage(npm) t.match(cmd.usage, /test-stage/) cmd.exec(['some', 'args'], (er, result) => { t.same(runArgs, ['test-stage', 'some', 'args']) |