@@ -29,8 +29,8 @@ class WebpackCLI {
2929 this . utils = { toKebabCase, getPkg, promptInstallation } ;
3030 }
3131
32- makeCommand ( commandOptions , optionsForCommand = [ ] , action ) {
33- const command = program . command ( commandOptions . name , {
32+ async makeCommand ( commandOptions , options , action ) {
33+ const command = this . program . command ( commandOptions . name , {
3434 noHelp : commandOptions . noHelp ,
3535 hidden : commandOptions . hidden ,
3636 isDefault : commandOptions . isDefault ,
@@ -56,8 +56,50 @@ class WebpackCLI {
5656 command . pkg = 'webpack-cli' ;
5757 }
5858
59- if ( optionsForCommand . length > 0 ) {
60- optionsForCommand . forEach ( ( optionForCommand ) => {
59+ const { forHelp } = this . program ;
60+
61+ let allDependenciesInstalled = true ;
62+
63+ if ( commandOptions . dependencies && commandOptions . dependencies . length > 0 ) {
64+ for ( const dependency of commandOptions . dependencies ) {
65+ const isPkgExist = getPkg ( dependency ) ;
66+
67+ if ( isPkgExist ) {
68+ continue ;
69+ } else if ( ! isPkgExist && forHelp ) {
70+ allDependenciesInstalled = false ;
71+ continue ;
72+ }
73+
74+ try {
75+ await promptInstallation ( dependency , ( ) => {
76+ logger . error (
77+ `For using '${ green ( commandOptions . name ) } ' command you need to install: '${ green ( dependency ) } ' package` ,
78+ ) ;
79+ } ) ;
80+ } catch ( error ) {
81+ logger . error ( "Action Interrupted, use 'webpack-cli help' to see possible commands." ) ;
82+ logger . error ( error ) ;
83+ process . exit ( 2 ) ;
84+ }
85+ }
86+ }
87+
88+ if ( options ) {
89+ if ( typeof options === 'function' ) {
90+ if ( forHelp && ! allDependenciesInstalled ) {
91+ command . description (
92+ `${ commandOptions . description } To see all available options you need to install ${ commandOptions . dependencies
93+ . map ( ( dependency ) => `'${ dependency } '` )
94+ . join ( ',' ) } .`,
95+ ) ;
96+ options = [ ] ;
97+ } else {
98+ options = options ( ) ;
99+ }
100+ }
101+
102+ options . forEach ( ( optionForCommand ) => {
61103 this . makeOption ( command , optionForCommand ) ;
62104 } ) ;
63105 }
@@ -271,29 +313,11 @@ class WebpackCLI {
271313 await this . bundleCommand ( options ) ;
272314 } ) ;
273315 } else if ( commandName === helpCommandOptions . name || commandName === helpCommandOptions . alias ) {
274- this . makeCommand (
275- {
276- name : 'help [command]' ,
277- alias : 'h' ,
278- description : 'Display help for commands and options' ,
279- usage : '[command]' ,
280- } ,
281- [ ] ,
282- // Stub for the `help` command
283- ( ) => { } ,
284- ) ;
316+ // Stub for the `help` command
317+ this . makeCommand ( helpCommandOptions , [ ] , ( ) => { } ) ;
285318 } else if ( commandName === versionCommandOptions . name || commandName === helpCommandOptions . alias ) {
286- this . makeCommand (
287- {
288- name : 'version [commands...]' ,
289- alias : 'v' ,
290- description : "Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands" ,
291- usage : '[commands...]' ,
292- } ,
293- [ ] ,
294- // Stub for the `help` command
295- ( ) => { } ,
296- ) ;
319+ // Stub for the `help` command
320+ this . makeCommand ( versionCommandOptions , [ ] , ( ) => { } ) ;
297321 } else {
298322 const builtInExternalCommandInfo = externalBuiltInCommandsInfo . find (
299323 ( externalBuiltInCommandInfo ) =>
@@ -310,11 +334,7 @@ class WebpackCLI {
310334
311335 if ( pkg !== 'webpack-cli' && ! getPkg ( pkg ) ) {
312336 if ( ! allowToInstall ) {
313- const isOptions = commandName . startsWith ( '-' ) ;
314-
315- logger . error ( `Unknown ${ isOptions ? 'option' : 'command' } '${ commandName } '` ) ;
316- logger . error ( "Run 'webpack --help' to see available commands and options" ) ;
317- process . exit ( 2 ) ;
337+ return ;
318338 }
319339
320340 try {
@@ -464,6 +484,12 @@ class WebpackCLI {
464484 ( command ) => command . name ( ) === possibleCommandName || command . alias ( ) === possibleCommandName ,
465485 ) ;
466486
487+ if ( ! foundCommand ) {
488+ logger . error ( `Unknown command '${ possibleCommandName } '` ) ;
489+ logger . error ( "Run 'webpack --help' to see available commands and options" ) ;
490+ process . exit ( 2 ) ;
491+ }
492+
467493 try {
468494 const { name, version } = require ( `${ foundCommand . pkg } /package.json` ) ;
469495
@@ -481,7 +507,7 @@ class WebpackCLI {
481507 logger . raw ( `webpack-cli ${ pkgJSON . version } ` ) ;
482508
483509 if ( getPkg ( 'webpack-dev-server' ) ) {
484- // eslint-disable-next-line node/no-extraneous-require
510+ // eslint-disable-next-line
485511 const { version } = require ( 'webpack-dev-server/package.json' ) ;
486512
487513 logger . raw ( `webpack-dev-server ${ version } ` ) ;
@@ -547,6 +573,12 @@ class WebpackCLI {
547573 } else {
548574 const [ name , ...optionsWithoutCommandName ] = options ;
549575
576+ if ( name . startsWith ( '-' ) ) {
577+ logger . error ( `Unknown option '${ name } '` ) ;
578+ logger . error ( "Run 'webpack --help' to see available commands and options" ) ;
579+ process . exit ( 2 ) ;
580+ }
581+
550582 optionsWithoutCommandName . forEach ( ( option ) => {
551583 logger . error ( `Unknown option '${ option } '` ) ;
552584 logger . error ( "Run 'webpack --help' to see available commands and options" ) ;
@@ -636,6 +668,8 @@ class WebpackCLI {
636668 }
637669 }
638670
671+ this . program . forHelp = true ;
672+
639673 const optionsForHelp = [ ] . concat ( opts . help && ! isDefault ? [ commandName ] : [ ] ) . concat ( options ) ;
640674
641675 await outputHelp ( optionsForHelp , isVerbose , program ) ;
0 commit comments