Skip to content

Commit df8c5ab

Browse files
committed
fix: make prefixed usage errors more consistent
1 parent 8ffeb71 commit df8c5ab

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

lib/base-command.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ class BaseCommand {
5353
return results
5454
}
5555

56-
usageError (msg) {
57-
if (!msg) {
58-
return Object.assign(new Error(`\nUsage: ${this.usage}`), {
59-
code: 'EUSAGE',
60-
})
61-
}
62-
63-
return Object.assign(new Error(`\nUsage: ${msg}\n\n${this.usage}`), {
56+
usageError (prefix = '') {
57+
if (prefix)
58+
prefix += '\n\n'
59+
return Object.assign(new Error(`\nUsage: ${prefix}${this.usage}`), {
6460
code: 'EUSAGE',
6561
})
6662
}

lib/commands/cache.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Cache extends BaseCommand {
116116
case 'ls':
117117
return await this.ls(args)
118118
default:
119-
throw Object.assign(new Error(this.usage), { code: 'EUSAGE' })
119+
throw this.usageError()
120120
}
121121
}
122122

@@ -161,14 +161,9 @@ class Cache extends BaseCommand {
161161
// npm cache add <tarball>...
162162
// npm cache add <folder>...
163163
async add (args) {
164-
const usage = 'Usage:\n' +
165-
' npm cache add <tarball-url>...\n' +
166-
' npm cache add <pkg>@<ver>...\n' +
167-
' npm cache add <tarball>...\n' +
168-
' npm cache add <folder>...\n'
169164
log.silly('cache add', 'args', args)
170165
if (args.length === 0)
171-
throw Object.assign(new Error(usage), { code: 'EUSAGE' })
166+
throw this.usageError('First argument to `add` is required')
172167

173168
return Promise.all(args.map(spec => {
174169
log.silly('cache add', 'spec', spec)

lib/commands/diff.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Diff extends BaseCommand {
101101
}
102102

103103
if (!name)
104-
throw this.usageError('Needs multiple arguments to compare or run from a project dir.\n')
104+
throw this.usageError('Needs multiple arguments to compare or run from a project dir.')
105105

106106
return name
107107
}
@@ -133,7 +133,7 @@ class Diff extends BaseCommand {
133133
noPackageJson = true
134134
}
135135

136-
const missingPackageJson = this.usageError('Needs multiple arguments to compare or run from a project dir.\n')
136+
const missingPackageJson = this.usageError('Needs multiple arguments to compare or run from a project dir.')
137137

138138
// using a valid semver range, that means it should just diff
139139
// the cwd against a published version to the registry using the
@@ -222,7 +222,7 @@ class Diff extends BaseCommand {
222222
`file:${this.prefix}`,
223223
]
224224
} else
225-
throw this.usageError(`Spec type ${spec.type} not supported.\n`)
225+
throw this.usageError(`Spec type ${spec.type} not supported.`)
226226
}
227227

228228
async convertVersionsToSpecs ([a, b]) {
@@ -239,7 +239,7 @@ class Diff extends BaseCommand {
239239
}
240240

241241
if (!pkgName)
242-
throw this.usageError('Needs to be run from a project dir in order to diff two versions.\n')
242+
throw this.usageError('Needs to be run from a project dir in order to diff two versions.')
243243

244244
return [`${pkgName}@${a}`, `${pkgName}@${b}`]
245245
}

test/lib/commands/cache.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ const { fake: mockNpm } = require('../../fixtures/mock-npm.js')
33
const path = require('path')
44
const npa = require('npm-package-arg')
55

6-
const usageUtil = () => 'usage instructions'
7-
86
let outputOutput = []
97

108
let rimrafPath = ''
@@ -140,7 +138,6 @@ const Cache = t.mock('../../../lib/commands/cache.js', {
140138
npmlog,
141139
pacote,
142140
rimraf,
143-
'../../../lib/utils/usage.js': usageUtil,
144141
})
145142

146143
const npm = mockNpm({
@@ -161,7 +158,10 @@ const cache = new Cache(npm)
161158
t.test('cache no args', async t => {
162159
await t.rejects(
163160
cache.exec([]),
164-
'usage instructions',
161+
{
162+
code: 'EUSAGE',
163+
message: /^Usage: npm cache$/m,
164+
},
165165
'should throw usage instructions'
166166
)
167167
})
@@ -194,7 +194,10 @@ t.test('cache add no arg', async t => {
194194

195195
await t.rejects(
196196
cache.exec(['add']),
197-
{ code: 'EUSAGE' },
197+
{
198+
code: 'EUSAGE',
199+
message: /^Usage: First argument to `add` is required$/m,
200+
},
198201
'throws usage error'
199202
)
200203
t.strictSame(logOutput, [

0 commit comments

Comments
 (0)