diff options
author | Luke Karrys <luke@lukekarrys.com> | 2021-11-04 00:53:40 +0300 |
---|---|---|
committer | Luke Karrys <luke@lukekarrys.com> | 2021-11-04 23:06:50 +0300 |
commit | 22230ef3dd590def31c274b3412106b4cfbd212f (patch) | |
tree | 8101180fa461569b8e26b05a199c7dd6487e8516 /lib/commands | |
parent | a0d35ff20aed6aab8508123eb540bc9c61fb127d (diff) |
fix: make prefixed usage errors more consistent
PR-URL: https://github.com/npm/cli/pull/3987
Credit: @lukekarrys
Close: #3987
Reviewed-by: @wraithgar
Diffstat (limited to 'lib/commands')
-rw-r--r-- | lib/commands/cache.js | 9 | ||||
-rw-r--r-- | lib/commands/diff.js | 16 | ||||
-rw-r--r-- | lib/commands/edit.js | 2 | ||||
-rw-r--r-- | lib/commands/exec.js | 2 | ||||
-rw-r--r-- | lib/commands/explore.js | 4 | ||||
-rw-r--r-- | lib/commands/help-search.js | 2 | ||||
-rw-r--r-- | lib/commands/hook.js | 2 | ||||
-rw-r--r-- | lib/commands/pkg.js | 10 | ||||
-rw-r--r-- | lib/commands/profile.js | 2 | ||||
-rw-r--r-- | lib/commands/star.js | 2 | ||||
-rw-r--r-- | lib/commands/team.js | 2 |
11 files changed, 19 insertions, 34 deletions
diff --git a/lib/commands/cache.js b/lib/commands/cache.js index 43f52e4e9..b4a932d1b 100644 --- a/lib/commands/cache.js +++ b/lib/commands/cache.js @@ -116,7 +116,7 @@ class Cache extends BaseCommand { case 'ls': return await this.ls(args) default: - throw Object.assign(new Error(this.usage), { code: 'EUSAGE' }) + throw this.usageError() } } @@ -161,14 +161,9 @@ class Cache extends BaseCommand { // npm cache add <tarball>... // npm cache add <folder>... async add (args) { - const usage = 'Usage:\n' + - ' npm cache add <tarball-url>...\n' + - ' npm cache add <pkg>@<ver>...\n' + - ' npm cache add <tarball>...\n' + - ' npm cache add <folder>...\n' log.silly('cache add', 'args', args) if (args.length === 0) - throw Object.assign(new Error(usage), { code: 'EUSAGE' }) + throw this.usageError('First argument to `add` is required') return Promise.all(args.map(spec => { log.silly('cache add', 'spec', spec) diff --git a/lib/commands/diff.js b/lib/commands/diff.js index da9b9f5d2..67d0d1505 100644 --- a/lib/commands/diff.js +++ b/lib/commands/diff.js @@ -49,12 +49,8 @@ class Diff extends BaseCommand { async exec (args) { const specs = this.npm.config.get('diff').filter(d => d) - if (specs.length > 2) { - throw new TypeError( - 'Can\'t use more than two --diff arguments.\n\n' + - `Usage:\n${this.usage}` - ) - } + if (specs.length > 2) + throw this.usageError(`Can't use more than two --diff arguments.`) // execWorkspaces may have set this already if (!this.prefix) @@ -101,7 +97,7 @@ class Diff extends BaseCommand { } if (!name) - throw this.usageError('Needs multiple arguments to compare or run from a project dir.\n') + throw this.usageError('Needs multiple arguments to compare or run from a project dir.') return name } @@ -133,7 +129,7 @@ class Diff extends BaseCommand { noPackageJson = true } - const missingPackageJson = this.usageError('Needs multiple arguments to compare or run from a project dir.\n') + const missingPackageJson = this.usageError('Needs multiple arguments to compare or run from a project dir.') // using a valid semver range, that means it should just diff // the cwd against a published version to the registry using the @@ -222,7 +218,7 @@ class Diff extends BaseCommand { `file:${this.prefix}`, ] } else - throw this.usageError(`Spec type ${spec.type} not supported.\n`) + throw this.usageError(`Spec type ${spec.type} not supported.`) } async convertVersionsToSpecs ([a, b]) { @@ -239,7 +235,7 @@ class Diff extends BaseCommand { } if (!pkgName) - throw this.usageError('Needs to be run from a project dir in order to diff two versions.\n') + throw this.usageError('Needs to be run from a project dir in order to diff two versions.') return [`${pkgName}@${a}`, `${pkgName}@${b}`] } diff --git a/lib/commands/edit.js b/lib/commands/edit.js index db9be4a26..4f0af6e83 100644 --- a/lib/commands/edit.js +++ b/lib/commands/edit.js @@ -35,7 +35,7 @@ class Edit extends BaseCommand { async exec (args) { if (args.length !== 1) - throw new Error(this.usage) + throw this.usageError() const path = splitPackageNames(args[0]) const dir = resolve(this.npm.dir, path) diff --git a/lib/commands/exec.js b/lib/commands/exec.js index 8f7f3c3e5..ffe72ccb4 100644 --- a/lib/commands/exec.js +++ b/lib/commands/exec.js @@ -80,7 +80,7 @@ class Exec extends BaseCommand { const yes = this.npm.config.get('yes') if (call && _args.length) - throw this.usage + throw this.usageError() return libexec({ ...flatOptions, diff --git a/lib/commands/explore.js b/lib/commands/explore.js index 8ff88ddf6..81a71f86a 100644 --- a/lib/commands/explore.js +++ b/lib/commands/explore.js @@ -34,14 +34,14 @@ class Explore extends BaseCommand { async exec (args) { if (args.length < 1 || !args[0]) - throw this.usage + throw this.usageError() const pkgname = args.shift() // detect and prevent any .. shenanigans const path = join(this.npm.dir, join('/', pkgname)) if (relative(path, this.npm.dir) === '') - throw this.usage + throw this.usageError() // run as if running a script named '_explore', which we set to either // the set of arguments, or the shell config, and let @npmcli/run-script diff --git a/lib/commands/help-search.js b/lib/commands/help-search.js index 4ab4a0896..a179939ab 100644 --- a/lib/commands/help-search.js +++ b/lib/commands/help-search.js @@ -28,7 +28,7 @@ class HelpSearch extends BaseCommand { async exec (args) { if (!args.length) - return this.npm.output(this.usage) + throw this.usageError() const docPath = path.resolve(__dirname, '..', '..', 'docs/content') const files = await glob(`${docPath}/*/*.md`) diff --git a/lib/commands/hook.js b/lib/commands/hook.js index 96b6d9626..7b2deff22 100644 --- a/lib/commands/hook.js +++ b/lib/commands/hook.js @@ -43,7 +43,7 @@ class Hook extends BaseCommand { case 'up': return this.update(args[1], args[2], args[3], opts) default: - throw this.usage + throw this.usageError() } }) } diff --git a/lib/commands/pkg.js b/lib/commands/pkg.js index c9b8f7d40..1fa2c3bc5 100644 --- a/lib/commands/pkg.js +++ b/lib/commands/pkg.js @@ -96,10 +96,7 @@ class Pkg extends BaseCommand { async set (args) { const setError = () => - Object.assign( - new TypeError('npm pkg set expects a key=value pair of args.'), - { code: 'EPKGSET' } - ) + this.usageError('npm pkg set expects a key=value pair of args.') if (!args.length) throw setError() @@ -123,10 +120,7 @@ class Pkg extends BaseCommand { async delete (args) { const setError = () => - Object.assign( - new TypeError('npm pkg delete expects key args.'), - { code: 'EPKGDELETE' } - ) + this.usageError('npm pkg delete expects key args.') if (!args.length) throw setError() diff --git a/lib/commands/profile.js b/lib/commands/profile.js index caab13d78..abfe5edd7 100644 --- a/lib/commands/profile.js +++ b/lib/commands/profile.js @@ -90,7 +90,7 @@ class Profile extends BaseCommand { async exec (args) { if (args.length === 0) - throw new Error(this.usage) + throw this.usageError() log.gauge.show('profile') diff --git a/lib/commands/star.js b/lib/commands/star.js index 3e5b0fc62..36003a020 100644 --- a/lib/commands/star.js +++ b/lib/commands/star.js @@ -30,7 +30,7 @@ class Star extends BaseCommand { async exec (args) { if (!args.length) - throw new Error(this.usage) + throw this.usageError() // if we're unstarring, then show an empty star image // otherwise, show the full star image diff --git a/lib/commands/team.js b/lib/commands/team.js index 11a7deb52..b337a7536 100644 --- a/lib/commands/team.js +++ b/lib/commands/team.js @@ -68,7 +68,7 @@ class Team extends BaseCommand { return this.listTeams(entity, opts) } default: - throw this.usage + throw this.usageError() } }) } |