File tree Expand file tree Collapse file tree 4 files changed +18
-24
lines changed Expand file tree Collapse file tree 4 files changed +18
-24
lines changed Original file line number Diff line number Diff line change @@ -53,14 +53,10 @@ class BaseCommand {
53
53
return results
54
54
}
55
55
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 } ` ) , {
64
60
code : 'EUSAGE' ,
65
61
} )
66
62
}
Original file line number Diff line number Diff line change @@ -116,7 +116,7 @@ class Cache extends BaseCommand {
116
116
case 'ls' :
117
117
return await this . ls ( args )
118
118
default :
119
- throw Object . assign ( new Error ( this . usage ) , { code : 'EUSAGE' } )
119
+ throw this . usageError ( )
120
120
}
121
121
}
122
122
@@ -161,14 +161,9 @@ class Cache extends BaseCommand {
161
161
// npm cache add <tarball>...
162
162
// npm cache add <folder>...
163
163
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'
169
164
log . silly ( 'cache add' , 'args' , args )
170
165
if ( args . length === 0 )
171
- throw Object . assign ( new Error ( usage ) , { code : 'EUSAGE' } )
166
+ throw this . usageError ( 'First argument to `add` is required' )
172
167
173
168
return Promise . all ( args . map ( spec => {
174
169
log . silly ( 'cache add' , 'spec' , spec )
Original file line number Diff line number Diff line change @@ -101,7 +101,7 @@ class Diff extends BaseCommand {
101
101
}
102
102
103
103
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.' )
105
105
106
106
return name
107
107
}
@@ -133,7 +133,7 @@ class Diff extends BaseCommand {
133
133
noPackageJson = true
134
134
}
135
135
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.' )
137
137
138
138
// using a valid semver range, that means it should just diff
139
139
// the cwd against a published version to the registry using the
@@ -222,7 +222,7 @@ class Diff extends BaseCommand {
222
222
`file:${ this . prefix } ` ,
223
223
]
224
224
} else
225
- throw this . usageError ( `Spec type ${ spec . type } not supported.\n ` )
225
+ throw this . usageError ( `Spec type ${ spec . type } not supported.` )
226
226
}
227
227
228
228
async convertVersionsToSpecs ( [ a , b ] ) {
@@ -239,7 +239,7 @@ class Diff extends BaseCommand {
239
239
}
240
240
241
241
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.' )
243
243
244
244
return [ `${ pkgName } @${ a } ` , `${ pkgName } @${ b } ` ]
245
245
}
Original file line number Diff line number Diff line change @@ -3,8 +3,6 @@ const { fake: mockNpm } = require('../../fixtures/mock-npm.js')
3
3
const path = require ( 'path' )
4
4
const npa = require ( 'npm-package-arg' )
5
5
6
- const usageUtil = ( ) => 'usage instructions'
7
-
8
6
let outputOutput = [ ]
9
7
10
8
let rimrafPath = ''
@@ -140,7 +138,6 @@ const Cache = t.mock('../../../lib/commands/cache.js', {
140
138
npmlog,
141
139
pacote,
142
140
rimraf,
143
- '../../../lib/utils/usage.js' : usageUtil ,
144
141
} )
145
142
146
143
const npm = mockNpm ( {
@@ -161,7 +158,10 @@ const cache = new Cache(npm)
161
158
t . test ( 'cache no args' , async t => {
162
159
await t . rejects (
163
160
cache . exec ( [ ] ) ,
164
- 'usage instructions' ,
161
+ {
162
+ code : 'EUSAGE' ,
163
+ message : / ^ U s a g e : n p m c a c h e $ / m,
164
+ } ,
165
165
'should throw usage instructions'
166
166
)
167
167
} )
@@ -194,7 +194,10 @@ t.test('cache add no arg', async t => {
194
194
195
195
await t . rejects (
196
196
cache . exec ( [ 'add' ] ) ,
197
- { code : 'EUSAGE' } ,
197
+ {
198
+ code : 'EUSAGE' ,
199
+ message : / ^ U s a g e : F i r s t a r g u m e n t t o ` a d d ` i s r e q u i r e d $ / m,
200
+ } ,
198
201
'throws usage error'
199
202
)
200
203
t . strictSame ( logOutput , [
You can’t perform that action at this time.
0 commit comments