Skip to content

Commit 6115734

Browse files
committed
resolves #86 exit process consistency
1 parent c1e3f2f commit 6115734

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed

lib/invoker.js

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,28 @@ class Invoker {
1919
this.showVersion()
2020
process.exit(0)
2121
}
22+
if (files.length === 0 && !this.options.stdin) {
23+
// nothing to do...
24+
this.showHelp()
25+
process.exit(0)
26+
}
2227
Invoker.prepareProcessor(args, asciidoctor)
2328
const options = this.options.options
2429
const failureLevel = options['failure_level']
25-
if (this.options.stdin) {
26-
await Invoker.convertFromStdin(options, args)
27-
Invoker.exit(failureLevel)
28-
} else if (files && files.length > 0) {
29-
Invoker.processFiles(files, verbose, args['timings'], options)
30+
try {
31+
if (this.options.stdin) {
32+
await Invoker.convertFromStdin(options, args)
33+
} else if (files && files.length > 0) {
34+
Invoker.convertFiles(files, verbose, args['timings'], options)
35+
}
3036
Invoker.exit(failureLevel)
31-
} else {
32-
this.showHelp()
33-
process.exit(0)
37+
} catch (e) {
38+
if (e && e.name === 'NotImplementedError' && e.message === `asciidoctor: FAILED: missing converter for backend '${options.backend}'. Processing aborted.`) {
39+
console.error(`> Error: missing converter for backend '${options.backend}'. Processing aborted.`)
40+
console.error(`> You might want to require a Node.js package with --require option to support this backend.`)
41+
process.exit(1)
42+
}
43+
throw e
3444
}
3545
}
3646

@@ -69,42 +79,25 @@ CLI version ${pkg.version}`
6979
if (args['timings']) {
7080
const timings = asciidoctor.Timings.create()
7181
const instanceOptions = Object.assign({}, options, { timings })
72-
Invoker.convert(asciidoctor.convert, data, instanceOptions)
82+
asciidoctor.convert(data, instanceOptions)
7383
timings.printReport(process.stderr, '-')
7484
} else {
75-
Invoker.convert(asciidoctor.convert, data, options)
76-
}
77-
}
78-
79-
static convert (processorFn, input, options) {
80-
try {
81-
processorFn.apply(asciidoctor, [input, options])
82-
} catch (e) {
83-
if (e && e.name === 'NotImplementedError' && e.message === `asciidoctor: FAILED: missing converter for backend '${options.backend}'. Processing aborted.`) {
84-
console.error(`> Error: missing converter for backend '${options.backend}'. Processing aborted.`)
85-
console.error(`> You might want to require a Node.js package with --require option to support this backend.`)
86-
process.exit(1)
87-
}
88-
throw e
85+
asciidoctor.convert(data, options)
8986
}
9087
}
9188

92-
static convertFile (file, options) {
93-
Invoker.convert(asciidoctor.convertFile, file, options)
94-
}
95-
96-
static processFiles (files, verbose, timings, options) {
89+
static convertFiles (files, verbose, timings, options) {
9790
files.forEach((file) => {
9891
if (verbose) {
9992
console.log(`converting file ${file}`)
10093
}
10194
if (timings) {
10295
const timings = asciidoctor.Timings.create()
10396
const instanceOptions = Object.assign({}, options, { timings })
104-
Invoker.convertFile(file, instanceOptions)
97+
asciidoctor.convertFile(file, instanceOptions)
10598
timings.printReport(process.stderr, file)
10699
} else {
107-
Invoker.convertFile(file, options)
100+
asciidoctor.convertFile(file, options)
108101
}
109102
})
110103
}

test/test.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ describe('Help', () => {
156156
describe('Print timings report', () => {
157157
it('should print timings report', () => {
158158
sinon.stub(process.stderr, 'write')
159+
sinon.stub(process.stdout, 'write')
159160
sinon.stub(process, 'exit')
160161
try {
161162
new Invoker(new Options().parse(['node', 'asciidoctor', `${__dirname}/fixtures/sample.adoc`, '--timings', '-o', '-'])).invoke()
@@ -166,6 +167,43 @@ describe('Print timings report', () => {
166167
expect(process.stderr.write.getCall(3).args[0]).to.include('Total time (read, parse and convert):')
167168
} finally {
168169
process.stderr.write.restore()
170+
process.stdout.write.restore()
171+
process.exit.restore()
172+
}
173+
})
174+
})
175+
176+
describe('Write to stdout', () => {
177+
it('should write to stdout', () => {
178+
sinon.stub(process.stdout, 'write')
179+
sinon.stub(process, 'exit')
180+
try {
181+
new Invoker(new Options().parse(['node', 'asciidoctor', `${__dirname}/fixtures/sample.adoc`, '-a', 'nofooter', '-o', '-'])).invoke()
182+
expect(process.stdout.write.called).to.be.true()
183+
expect(process.stdout.write.getCall(0).args[0]).to.include(`<body class="article">
184+
<div id="header">
185+
<h1>Title</h1>
186+
</div>
187+
<div id="content">
188+
<div id="preamble">
189+
<div class="sectionbody">
190+
<div class="paragraph">
191+
<p>This is a sample document</p>
192+
</div>
193+
</div>
194+
</div>
195+
<div class="sect1">
196+
<h2 id="_first_section">First section</h2>
197+
<div class="sectionbody">
198+
<div class="paragraph">
199+
<p>This is a paragraph.</p>
200+
</div>
201+
</div>
202+
</div>
203+
</div>
204+
</body>`)
205+
} finally {
206+
process.stdout.write.restore()
169207
process.exit.restore()
170208
}
171209
})
@@ -227,8 +265,9 @@ describe('Process files', () => {
227265
it('should exit with code 1 when failure level is lower than the maximum logging level', () => {
228266
sinon.stub(process, 'exit')
229267
try {
230-
Invoker.processFiles([bookFilePath], false, false)
268+
Invoker.convertFiles([bookFilePath], false, false)
231269
Invoker.exit(3) // ERROR: 3
270+
Invoker.exit(3)
232271
expect(process.exit.called).to.be.true()
233272
expect(process.exit.calledWith(1)).to.be.true()
234273
} finally {
@@ -239,8 +278,9 @@ describe('Process files', () => {
239278
it('should exit with code 0 when failure level is lower than the maximum logging level', () => {
240279
sinon.stub(process, 'exit')
241280
try {
242-
Invoker.processFiles([bookFilePath], false, false)
281+
Invoker.convertFiles([bookFilePath], false, false)
243282
Invoker.exit(4) // FATAL: 4
283+
Invoker.exit(4)
244284
expect(process.exit.called).to.be.true()
245285
expect(process.exit.calledWith(0)).to.be.true()
246286
} finally {

0 commit comments

Comments
 (0)