@@ -11,25 +11,20 @@ module.exports = (config, root) => {
1111 [ path . join ( root , 'node_modules' ) ] :
1212 module . parent . parent . paths ) . slice ( 0 ) ; // copy
1313 let settings = getSettings ( config ) ;
14+ let help = '' ;
1415
1516 return Promise . all ( [
1617 parseArgs ( process . argv , config ) ,
1718 read ( ) ,
1819 findRoot ( paths ) . then ( res => root = res ) ,
1920 ] ) . then ( res => {
2021 let args = res [ 0 ] . args ;
21- let help = res [ 0 ] . help ;
22+ help = res [ 0 ] . help ;
2223 let body = res [ 1 ] ;
2324 if ( ! args . $_ && ! settings . commands . _ && ! body ) {
24- return loadCommand ( root , { $_ : ':::./help' } , body , settings ) . catch ( e => {
25- if ( e . code === 'NO_HELP_CONFIGURED' ) {
26- e . message = help . trim ( ) ;
27- }
28- throw e ;
29- } ) . then ( res => {
30- var e = new Error ( res ) ;
31- throw e ;
32- } ) ;
25+ var e = new Error ( 'BAD_ARGS' ) ;
26+ e . code = 'BAD_ARGS' ;
27+ return Promise . reject ( e ) ;
3328 }
3429
3530 return loadCommand ( root , args , body , settings ) ;
@@ -42,13 +37,39 @@ module.exports = (config, root) => {
4237
4338 return res ;
4439 } ) . catch ( e => {
45- /* istanbul ignore if */
46- if ( ! settings . return ) {
47- debug ( e . stack ) ;
48- console . error ( e . message ) ;
49- return process . exit ( 1 ) ;
40+ var promise = Promise . reject ( e ) ;
41+
42+ if ( e . code === 'BAD_ARGS' ) {
43+ // show either the configured help or the automatically generated help
44+ promise = loadCommand ( root , {
45+ $_ : ':::./help' , help : true ,
46+ } , null , settings ) . then ( res => {
47+ var error = new Error ( res ) ; // based on loaded help
48+ error . code = 'BAD_ARGS' ;
49+ return Promise . reject ( error ) ;
50+ } ) . catch ( error => {
51+ if ( error . code === 'NO_HELP_CONFIGURED' ) {
52+ error . code = 'BAD_ARGS' ;
53+ error . message = help . trim ( ) ;
54+ }
55+
56+ if ( error . code === 'BAD_ARGS' && e . message !== 'BAD_ARGS' ) {
57+ error . message = `${ e . message } \n\n${ error . message } ` ;
58+ }
59+
60+ return Promise . reject ( error ) ;
61+ } ) ;
5062 }
51- throw e ;
63+
64+ return promise . catch ( e => {
65+ /* istanbul ignore if */
66+ if ( ! settings . return ) {
67+ debug ( e . stack ) ;
68+ console . error ( e . message ) ;
69+ return process . exit ( 1 ) ;
70+ }
71+ return Promise . reject ( e ) ;
72+ } ) ;
5273 } ) ;
5374} ;
5475
@@ -75,7 +96,7 @@ function loadCommand(root, args, body, settings) {
7596 resolve ( require ( filename ) ) ;
7697 } ) . catch ( e => {
7798 e . message = `Fatal: command failed to load "${ filename } "` ;
78- throw e ;
99+ return Promise . reject ( e ) ;
79100 } ) . then ( res => res ( args , settings , body ) ) . then ( res => {
80101 /* istanbul ignore if */
81102 if ( ! settings . return && internal ) {
0 commit comments