17
17
const debug = require ( 'debug' ) ( 'headless' )
18
18
debug ( 'starting' )
19
19
20
+ // by default, we'll exit with an exit code of 0 when success is
21
+ // called; this bit is necessary, as process.exit doesn't seem to
22
+ // really exit the first time
23
+ let exitCode = 0
24
+
20
25
// electron pops up a window by default, for uncaught exceptions
21
26
process . on ( 'uncaughtException' , err => {
22
27
console . error ( err . red )
@@ -63,6 +68,8 @@ let noAuth = false
63
68
function mimicDom ( app , { createWindow } , localStorage ) {
64
69
debug ( 'mimicDom' )
65
70
71
+ const { quit } = app
72
+
66
73
try {
67
74
global . localStorage = Store ( app )
68
75
} catch ( err ) {
@@ -152,7 +159,7 @@ function mimicDom(app, { createWindow }, localStorage) {
152
159
showEntity : entity => print ( entity ) ,
153
160
maybeHideEntity : ( ) => false ,
154
161
ok : ( ) => dom0 ( ) ,
155
- oops : ( ) => failure ,
162
+ oops : ( ) => failure ( quit ) ,
156
163
oopsMessage : err => {
157
164
return ( err && err . error && err . error . response && err . error . response . result && err . error . response . result . error && err . error . response . result . error . error ) // feed creation error. nice
158
165
|| ( err && err . error && err . error . response && err . error . response . result && err . error . response . result . error )
@@ -230,11 +237,11 @@ function mimicDom(app, { createWindow }, localStorage) {
230
237
. then ( resolve )
231
238
. catch ( reject ) ;
232
239
} catch ( err ) {
233
- failure ( err ) ; reject ( err )
240
+ failure ( quit ) ( err ) ; reject ( err )
234
241
}
235
242
} )
236
243
} catch ( err ) {
237
- failure ( err ) ; reject ( err )
244
+ failure ( quit ) ( err ) ; reject ( err )
238
245
}
239
246
} )
240
247
@@ -424,12 +431,20 @@ const print = (msg, logger=log, stream=process.stdout, color='reset', ok='ok') =
424
431
425
432
if ( msg . _isFakeDom ) {
426
433
// msg is a DOM facade
434
+
435
+ if ( msg . className . indexOf ( 'usage-error-wrapper' ) >= 0 ) {
436
+ // print usage errors to stdout
437
+ stream = process . stdout
438
+ }
439
+
427
440
prettyDom ( msg , logger , stream , color )
428
441
logger ( )
429
442
430
443
} else if ( msg . then ) {
431
444
// msg is a promise; resolve it and try again
432
- msg . then ( msg => print ( msg , logger , stream , color , ok ) )
445
+ return msg . then ( msg => {
446
+ return print ( msg , logger , stream , color , ok )
447
+ } )
433
448
434
449
} else if ( msg . message && msg . message . _isFakeDom ) {
435
450
// msg.message is a DOM facade
@@ -505,14 +520,17 @@ const success = quit => out => {
505
520
506
521
if ( ! graphicalShellIsOpen ) {
507
522
quit ( )
508
- process . exit ( 0 )
523
+ process . exit ( exitCode )
509
524
} else {
510
525
//log('The graphical shell should now be open. This process will stay alive till you close the window.'.red)
511
526
//log('You may background this process, but do not kill it, unless you also want to kill the graphical shell.'.red)
512
527
}
513
528
}
514
- const failure = err => {
529
+ const failure = quit => err => {
515
530
debug ( 'failure' , err )
531
+
532
+ let completion = Promise . resolve ( )
533
+
516
534
if ( ! noAuth ) {
517
535
// we're not in a corner case of having no openwhisk auth, so
518
536
// print the error
@@ -521,18 +539,22 @@ const failure = err => {
521
539
if ( typeof msg === 'string' ) {
522
540
error ( msg . red )
523
541
} else {
524
- print ( msg , error , process . stderr , 'red' , 'error' )
542
+ completion = print ( msg , error , process . stderr , 'red' , 'error' ) || Promise . resolve ( )
525
543
}
526
544
} else {
527
545
error ( `No wskprops file was found. Consider trying again with "fsh help" command.` )
528
546
}
529
547
530
- if ( ! graphicalShellIsOpen ) {
531
- // if the graphical shell isn't open, then we're done here
532
- process . exit ( 1 )
533
- }
548
+ return completion . then ( ( ) => {
549
+ if ( ! graphicalShellIsOpen ) {
550
+ // if the graphical shell isn't open, then we're done here
551
+ exitCode = 1
552
+ process . exit ( 1 )
553
+ if ( quit ) quit ( )
554
+ }
534
555
535
- return false
556
+ return false
557
+ } )
536
558
}
537
559
538
560
/**
@@ -545,13 +567,7 @@ const onlyOpts = argv => !argv.find(_ => _.charAt(0) !== '-')
545
567
* Insufficient arguments provided?
546
568
*
547
569
*/
548
- const insufficientArgs = ( ) => argv . length === 0 || onlyOpts ( argv ) || argv . find ( _ => _ === '--help' || _ === '-h' )
549
-
550
- /**
551
- * Print usage information, if the command line arguments are insufficient
552
- *
553
- */
554
- const usage = ( ) => repl . qexec ( 'help' )
570
+ const insufficientArgs = ( ) => argv . length === 0
555
571
556
572
/**
557
573
* Initialize headless mode
@@ -610,9 +626,9 @@ const main = (app, mainFunctions) => {
610
626
debug ( 'delayed namespace loading' )
611
627
return namespace . init ( )
612
628
. then ( ( ) => eval ( cmd ) )
613
- . catch ( failure )
629
+ . catch ( failure ( quit ) )
614
630
} else {
615
- return failure ( err )
631
+ return failure ( quit ) ( err )
616
632
}
617
633
}
618
634
@@ -628,7 +644,7 @@ const main = (app, mainFunctions) => {
628
644
debug ( 'exiting, no command' )
629
645
process . exit ( 0 )
630
646
}
631
- } ) . catch ( failure )
647
+ } ) . then ( success ( quit ) ) . catch ( failure ( quit ) )
632
648
}
633
649
634
650
exports . main = main
0 commit comments