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 {
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 }
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change @@ -3,8 +3,6 @@ const { fake: mockNpm } = require('../../fixtures/mock-npm.js')
33const path = require ( 'path' )
44const npa = require ( 'npm-package-arg' )
55
6- const usageUtil = ( ) => 'usage instructions'
7-
86let outputOutput = [ ]
97
108let 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
146143const npm = mockNpm ( {
@@ -161,7 +158,10 @@ const cache = new Cache(npm)
161158t . test ( 'cache no args' , async t => {
162159 await t . rejects (
163160 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+ } ,
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 : / ^ 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+ } ,
198201 'throws usage error'
199202 )
200203 t . strictSame ( logOutput , [
You can’t perform that action at this time.
0 commit comments