@@ -57,9 +57,7 @@ program
5757 . option ( '-x, --proxy <proxyUrl>' , 'Use specified proxy when creating project' )
5858 . option ( '-b, --bare' , 'Scaffold project without beginner instructions' )
5959 . option ( '--skipGetStarted' , 'Skip displaying "Get started" instructions' )
60- . action ( ( name , cmd ) => {
61- const options = cleanArgs ( cmd )
62-
60+ . action ( ( name , options ) => {
6361 if ( minimist ( process . argv . slice ( 3 ) ) . _ . length > 1 ) {
6462 console . log ( chalk . yellow ( '\n Info: You provided more than one argument. The first one will be used as the app\'s name, the rest are ignored.' ) )
6563 }
@@ -97,8 +95,8 @@ program
9795 . option ( '--rules' , 'list all module rule names' )
9896 . option ( '--plugins' , 'list all plugin names' )
9997 . option ( '-v --verbose' , 'Show full function definitions in output' )
100- . action ( ( paths , cmd ) => {
101- require ( '../lib/inspect' ) ( paths , cleanArgs ( cmd ) )
98+ . action ( ( paths , options ) => {
99+ require ( '../lib/inspect' ) ( paths , options )
102100 } )
103101
104102program
@@ -124,9 +122,9 @@ program
124122 . option ( '-D, --dev' , 'Run in dev mode' )
125123 . option ( '--quiet' , `Don't output starting messages` )
126124 . option ( '--headless' , `Don't open browser on start and output port` )
127- . action ( ( cmd ) => {
125+ . action ( ( options ) => {
128126 checkNodeVersion ( '>=8.6' , 'vue ui' )
129- require ( '../lib/ui' ) ( cleanArgs ( cmd ) )
127+ require ( '../lib/ui' ) ( options )
130128 } )
131129
132130program
@@ -146,16 +144,16 @@ program
146144 . option ( '-d, --delete <path>' , 'delete option from config' )
147145 . option ( '-e, --edit' , 'open config with default editor' )
148146 . option ( '--json' , 'outputs JSON result only' )
149- . action ( ( value , cmd ) => {
150- require ( '../lib/config' ) ( value , cleanArgs ( cmd ) )
147+ . action ( ( value , options ) => {
148+ require ( '../lib/config' ) ( value , options )
151149 } )
152150
153151program
154152 . command ( 'outdated' )
155153 . description ( '(experimental) check for outdated vue cli service / plugins' )
156154 . option ( '--next' , 'Also check for alpha / beta / rc versions when upgrading' )
157- . action ( ( cmd ) => {
158- require ( '../lib/outdated' ) ( cleanArgs ( cmd ) )
155+ . action ( ( options ) => {
156+ require ( '../lib/outdated' ) ( options )
159157 } )
160158
161159program
@@ -166,17 +164,16 @@ program
166164 . option ( '-r, --registry <url>' , 'Use specified npm registry when installing dependencies' )
167165 . option ( '--all' , 'Upgrade all plugins' )
168166 . option ( '--next' , 'Also check for alpha / beta / rc versions when upgrading' )
169- . action ( ( packageName , cmd ) => {
170- require ( '../lib/upgrade' ) ( packageName , cleanArgs ( cmd ) )
167+ . action ( ( packageName , options ) => {
168+ require ( '../lib/upgrade' ) ( packageName , options )
171169 } )
172170
173171program
174172 . command ( 'migrate [plugin-name]' )
175173 . description ( '(experimental) run migrator for an already-installed cli plugin' )
176- // TODO: use `requiredOption` after upgrading to commander 4.x
177- . option ( '-f, --from <version>' , 'The base version for the migrator to migrate from' )
178- . action ( ( packageName , cmd ) => {
179- require ( '../lib/migrate' ) ( packageName , cleanArgs ( cmd ) )
174+ . requiredOption ( '-f, --from <version>' , 'The base version for the migrator to migrate from' )
175+ . action ( ( packageName , options ) => {
176+ require ( '../lib/migrate' ) ( packageName , options )
180177 } )
181178
182179program
@@ -201,15 +198,13 @@ program
201198 } )
202199
203200// output help information on unknown commands
204- program
205- . arguments ( '<command>' )
206- . action ( ( cmd ) => {
207- program . outputHelp ( )
208- console . log ( ` ` + chalk . red ( `Unknown command ${ chalk . yellow ( cmd ) } .` ) )
209- console . log ( )
210- suggestCommands ( cmd )
211- process . exitCode = 1
212- } )
201+ program . on ( 'command:*' , ( [ cmd ] ) => {
202+ program . outputHelp ( )
203+ console . log ( ` ` + chalk . red ( `Unknown command ${ chalk . yellow ( cmd ) } .` ) )
204+ console . log ( )
205+ suggestCommands ( cmd )
206+ process . exitCode = 1
207+ } )
213208
214209// add some useful info on help
215210program . on ( '--help' , ( ) => {
@@ -259,22 +254,3 @@ function suggestCommands (unknownCommand) {
259254 console . log ( ` ` + chalk . red ( `Did you mean ${ chalk . yellow ( suggestion ) } ?` ) )
260255 }
261256}
262-
263- function camelize ( str ) {
264- return str . replace ( / - ( \w ) / g, ( _ , c ) => c ? c . toUpperCase ( ) : '' )
265- }
266-
267- // commander passes the Command object itself as options,
268- // extract only actual options into a fresh object.
269- function cleanArgs ( cmd ) {
270- const args = { }
271- cmd . options . forEach ( o => {
272- const key = camelize ( o . long . replace ( / ^ - - / , '' ) )
273- // if an option is not present and Command has a method with the same name
274- // it should not be copied
275- if ( typeof cmd [ key ] !== 'function' && typeof cmd [ key ] !== 'undefined' ) {
276- args [ key ] = cmd [ key ]
277- }
278- } )
279- return args
280- }
0 commit comments