1717const debug = require ( 'debug' ) ( 'headless' )
1818debug ( 'starting' )
1919
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+
2025// electron pops up a window by default, for uncaught exceptions
2126process . on ( 'uncaughtException' , err => {
2227 console . error ( err . red )
@@ -63,6 +68,8 @@ let noAuth = false
6368function mimicDom ( app , { createWindow } , localStorage ) {
6469 debug ( 'mimicDom' )
6570
71+ const { quit } = app
72+
6673 try {
6774 global . localStorage = Store ( app )
6875 } catch ( err ) {
@@ -152,7 +159,7 @@ function mimicDom(app, { createWindow }, localStorage) {
152159 showEntity : entity => print ( entity ) ,
153160 maybeHideEntity : ( ) => false ,
154161 ok : ( ) => dom0 ( ) ,
155- oops : ( ) => failure ,
162+ oops : ( ) => failure ( quit ) ,
156163 oopsMessage : err => {
157164 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
158165 || ( err && err . error && err . error . response && err . error . response . result && err . error . response . result . error )
@@ -230,11 +237,11 @@ function mimicDom(app, { createWindow }, localStorage) {
230237 . then ( resolve )
231238 . catch ( reject ) ;
232239 } catch ( err ) {
233- failure ( err ) ; reject ( err )
240+ failure ( quit ) ( err ) ; reject ( err )
234241 }
235242 } )
236243 } catch ( err ) {
237- failure ( err ) ; reject ( err )
244+ failure ( quit ) ( err ) ; reject ( err )
238245 }
239246 } )
240247
@@ -424,12 +431,20 @@ const print = (msg, logger=log, stream=process.stdout, color='reset', ok='ok') =
424431
425432 if ( msg . _isFakeDom ) {
426433 // 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+
427440 prettyDom ( msg , logger , stream , color )
428441 logger ( )
429442
430443 } else if ( msg . then ) {
431444 // 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+ } )
433448
434449 } else if ( msg . message && msg . message . _isFakeDom ) {
435450 // msg.message is a DOM facade
@@ -505,14 +520,17 @@ const success = quit => out => {
505520
506521 if ( ! graphicalShellIsOpen ) {
507522 quit ( )
508- process . exit ( 0 )
523+ process . exit ( exitCode )
509524 } else {
510525 //log('The graphical shell should now be open. This process will stay alive till you close the window.'.red)
511526 //log('You may background this process, but do not kill it, unless you also want to kill the graphical shell.'.red)
512527 }
513528}
514- const failure = err => {
529+ const failure = quit => err => {
515530 debug ( 'failure' , err )
531+
532+ let completion = Promise . resolve ( )
533+
516534 if ( ! noAuth ) {
517535 // we're not in a corner case of having no openwhisk auth, so
518536 // print the error
@@ -521,18 +539,22 @@ const failure = err => {
521539 if ( typeof msg === 'string' ) {
522540 error ( msg . red )
523541 } else {
524- print ( msg , error , process . stderr , 'red' , 'error' )
542+ completion = print ( msg , error , process . stderr , 'red' , 'error' ) || Promise . resolve ( )
525543 }
526544 } else {
527545 error ( `No wskprops file was found. Consider trying again with "fsh help" command.` )
528546 }
529547
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+ }
534555
535- return false
556+ return false
557+ } )
536558}
537559
538560/**
@@ -545,13 +567,7 @@ const onlyOpts = argv => !argv.find(_ => _.charAt(0) !== '-')
545567 * Insufficient arguments provided?
546568 *
547569 */
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
555571
556572/**
557573 * Initialize headless mode
@@ -610,9 +626,9 @@ const main = (app, mainFunctions) => {
610626 debug ( 'delayed namespace loading' )
611627 return namespace . init ( )
612628 . then ( ( ) => eval ( cmd ) )
613- . catch ( failure )
629+ . catch ( failure ( quit ) )
614630 } else {
615- return failure ( err )
631+ return failure ( quit ) ( err )
616632 }
617633 }
618634
@@ -628,7 +644,7 @@ const main = (app, mainFunctions) => {
628644 debug ( 'exiting, no command' )
629645 process . exit ( 0 )
630646 }
631- } ) . catch ( failure )
647+ } ) . then ( success ( quit ) ) . catch ( failure ( quit ) )
632648}
633649
634650exports . main = main
0 commit comments