Skip to content

Commit

Permalink
fix: make prefixed usage errors more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Nov 4, 2021
1 parent 8ffeb71 commit df8c5ab
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
12 changes: 4 additions & 8 deletions lib/base-command.js
Expand Up @@ -53,14 +53,10 @@ class BaseCommand {
return results
}

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}`), {
usageError (prefix = '') {
if (prefix)
prefix += '\n\n'
return Object.assign(new Error(`\nUsage: ${prefix}${this.usage}`), {
code: 'EUSAGE',
})
}
Expand Down
9 changes: 2 additions & 7 deletions lib/commands/cache.js
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions lib/commands/diff.js
Expand Up @@ -101,7 +101,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
}
Expand Down Expand Up @@ -133,7 +133,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
Expand Down Expand Up @@ -222,7 +222,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]) {
Expand All @@ -239,7 +239,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}`]
}
Expand Down
13 changes: 8 additions & 5 deletions test/lib/commands/cache.js
Expand Up @@ -3,8 +3,6 @@ const { fake: mockNpm } = require('../../fixtures/mock-npm.js')
const path = require('path')
const npa = require('npm-package-arg')

const usageUtil = () => 'usage instructions'

let outputOutput = []

let rimrafPath = ''
Expand Down Expand Up @@ -140,7 +138,6 @@ const Cache = t.mock('../../../lib/commands/cache.js', {
npmlog,
pacote,
rimraf,
'../../../lib/utils/usage.js': usageUtil,
})

const npm = mockNpm({
Expand All @@ -161,7 +158,10 @@ const cache = new Cache(npm)
t.test('cache no args', async t => {
await t.rejects(
cache.exec([]),
'usage instructions',
{
code: 'EUSAGE',
message: /^Usage: npm cache$/m,
},
'should throw usage instructions'
)
})
Expand Down Expand Up @@ -194,7 +194,10 @@ t.test('cache add no arg', async t => {

await t.rejects(
cache.exec(['add']),
{ code: 'EUSAGE' },
{
code: 'EUSAGE',
message: /^Usage: First argument to `add` is required$/m,
},
'throws usage error'
)
t.strictSame(logOutput, [
Expand Down

0 comments on commit df8c5ab

Please sign in to comment.