1
+ /* global Opal */
1
2
const fs = require ( 'fs' )
2
3
const ospath = require ( 'path' )
4
+ const { once } = require ( 'events' )
3
5
const asciidoctor = require ( '@asciidoctor/core' ) ( )
4
6
const pkg = require ( '../package.json' )
5
7
const stdin = require ( './stdin' )
@@ -24,10 +26,10 @@ class Invoker {
24
26
const failureLevel = options . failure_level
25
27
if ( this . options . stdin ) {
26
28
await Invoker . convertFromStdin ( options , args )
27
- Invoker . exit ( failureLevel )
29
+ await Invoker . exit ( failureLevel , options )
28
30
} else if ( files && files . length > 0 ) {
29
31
Invoker . processFiles ( files , verbose , args . timings , options )
30
- Invoker . exit ( failureLevel )
32
+ await Invoker . exit ( failureLevel , options )
31
33
} else {
32
34
this . showHelp ( )
33
35
process . exit ( 0 )
@@ -99,7 +101,7 @@ CLI version ${pkg.version}`
99
101
}
100
102
101
103
static processFiles ( files , verbose , timings , options ) {
102
- files . forEach ( ( file ) => {
104
+ for ( const file of files ) {
103
105
if ( verbose ) {
104
106
console . log ( `converting file ${ file } ` )
105
107
}
@@ -111,7 +113,7 @@ CLI version ${pkg.version}`
111
113
} else {
112
114
Invoker . convertFile ( file , options )
113
115
}
114
- } )
116
+ }
115
117
}
116
118
117
119
static requireLibrary ( requirePath , cwd = process . cwd ( ) ) {
@@ -142,12 +144,15 @@ CLI version ${pkg.version}`
142
144
}
143
145
}
144
146
145
- static exit ( failureLevel ) {
147
+ static async exit ( failureLevel , options = { } ) {
146
148
let code = 0
147
149
const logger = asciidoctor . LoggerManager . getLogger ( )
148
150
if ( logger && typeof logger . getMaxSeverity === 'function' && logger . getMaxSeverity ( ) && logger . getMaxSeverity ( ) >= failureLevel ) {
149
151
code = 1
150
152
}
153
+ if ( options . to_file === Opal . gvars . stdout ) {
154
+ await once ( process . stdout . end ( ) , 'close' )
155
+ }
151
156
process . exit ( code )
152
157
}
153
158
}
0 commit comments