1+ /* global Opal */
12const fs = require ( 'fs' )
23const ospath = require ( 'path' )
4+ const { once } = require ( 'events' )
35const asciidoctor = require ( '@asciidoctor/core' ) ( )
46const pkg = require ( '../package.json' )
57const stdin = require ( './stdin' )
@@ -24,10 +26,10 @@ class Invoker {
2426 const failureLevel = options . failure_level
2527 if ( this . options . stdin ) {
2628 await Invoker . convertFromStdin ( options , args )
27- Invoker . exit ( failureLevel )
29+ await Invoker . exit ( failureLevel , options )
2830 } else if ( files && files . length > 0 ) {
2931 Invoker . processFiles ( files , verbose , args . timings , options )
30- Invoker . exit ( failureLevel )
32+ await Invoker . exit ( failureLevel , options )
3133 } else {
3234 this . showHelp ( )
3335 process . exit ( 0 )
@@ -99,7 +101,7 @@ CLI version ${pkg.version}`
99101 }
100102
101103 static processFiles ( files , verbose , timings , options ) {
102- files . forEach ( ( file ) => {
104+ for ( const file of files ) {
103105 if ( verbose ) {
104106 console . log ( `converting file ${ file } ` )
105107 }
@@ -111,7 +113,7 @@ CLI version ${pkg.version}`
111113 } else {
112114 Invoker . convertFile ( file , options )
113115 }
114- } )
116+ }
115117 }
116118
117119 static requireLibrary ( requirePath , cwd = process . cwd ( ) ) {
@@ -142,12 +144,15 @@ CLI version ${pkg.version}`
142144 }
143145 }
144146
145- static exit ( failureLevel ) {
147+ static async exit ( failureLevel , options = { } ) {
146148 let code = 0
147149 const logger = asciidoctor . LoggerManager . getLogger ( )
148150 if ( logger && typeof logger . getMaxSeverity === 'function' && logger . getMaxSeverity ( ) && logger . getMaxSeverity ( ) >= failureLevel ) {
149151 code = 1
150152 }
153+ if ( options . to_file === Opal . gvars . stdout ) {
154+ await once ( process . stdout . end ( ) , 'close' )
155+ }
151156 process . exit ( code )
152157 }
153158}
0 commit comments