@@ -29,8 +29,8 @@ class WebpackCLI {
29
29
this . utils = { toKebabCase, getPkg, promptInstallation } ;
30
30
}
31
31
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 , {
34
34
noHelp : commandOptions . noHelp ,
35
35
hidden : commandOptions . hidden ,
36
36
isDefault : commandOptions . isDefault ,
@@ -56,8 +56,50 @@ class WebpackCLI {
56
56
command . pkg = 'webpack-cli' ;
57
57
}
58
58
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 ) => {
61
103
this . makeOption ( command , optionForCommand ) ;
62
104
} ) ;
63
105
}
@@ -271,29 +313,11 @@ class WebpackCLI {
271
313
await this . bundleCommand ( options ) ;
272
314
} ) ;
273
315
} 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 , [ ] , ( ) => { } ) ;
285
318
} 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 , [ ] , ( ) => { } ) ;
297
321
} else {
298
322
const builtInExternalCommandInfo = externalBuiltInCommandsInfo . find (
299
323
( externalBuiltInCommandInfo ) =>
@@ -310,11 +334,7 @@ class WebpackCLI {
310
334
311
335
if ( pkg !== 'webpack-cli' && ! getPkg ( pkg ) ) {
312
336
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 ;
318
338
}
319
339
320
340
try {
@@ -464,6 +484,12 @@ class WebpackCLI {
464
484
( command ) => command . name ( ) === possibleCommandName || command . alias ( ) === possibleCommandName ,
465
485
) ;
466
486
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
+
467
493
try {
468
494
const { name, version } = require ( `${ foundCommand . pkg } /package.json` ) ;
469
495
@@ -481,7 +507,7 @@ class WebpackCLI {
481
507
logger . raw ( `webpack-cli ${ pkgJSON . version } ` ) ;
482
508
483
509
if ( getPkg ( 'webpack-dev-server' ) ) {
484
- // eslint-disable-next-line node/no-extraneous-require
510
+ // eslint-disable-next-line
485
511
const { version } = require ( 'webpack-dev-server/package.json' ) ;
486
512
487
513
logger . raw ( `webpack-dev-server ${ version } ` ) ;
@@ -547,6 +573,12 @@ class WebpackCLI {
547
573
} else {
548
574
const [ name , ...optionsWithoutCommandName ] = options ;
549
575
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
+
550
582
optionsWithoutCommandName . forEach ( ( option ) => {
551
583
logger . error ( `Unknown option '${ option } '` ) ;
552
584
logger . error ( "Run 'webpack --help' to see available commands and options" ) ;
@@ -636,6 +668,8 @@ class WebpackCLI {
636
668
}
637
669
}
638
670
671
+ this . program . forHelp = true ;
672
+
639
673
const optionsForHelp = [ ] . concat ( opts . help && ! isDefault ? [ commandName ] : [ ] ) . concat ( options ) ;
640
674
641
675
await outputHelp ( optionsForHelp , isVerbose , program ) ;
0 commit comments