Skip to content

Commit c1e3f2f

Browse files
authored
Merge pull request #82 from Mogztter/issue-81-export-read-from-stdin
Export a static function to read from stdin
2 parents 6c14e87 + b73dbb4 commit c1e3f2f

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

lib/invoker.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Invoker {
1111
this.options = options
1212
}
1313

14-
invoke () {
14+
async invoke () {
1515
const processArgs = this.options.argv.slice(2)
1616
const { args } = this.options
1717
const { verbose, version, files } = args
@@ -21,10 +21,13 @@ class Invoker {
2121
}
2222
Invoker.prepareProcessor(args, asciidoctor)
2323
const options = this.options.options
24+
const failureLevel = options['failure_level']
2425
if (this.options.stdin) {
25-
Invoker.convertFromStdin(options, args)
26+
await Invoker.convertFromStdin(options, args)
27+
Invoker.exit(failureLevel)
2628
} else if (files && files.length > 0) {
2729
Invoker.processFiles(files, verbose, args['timings'], options)
30+
Invoker.exit(failureLevel)
2831
} else {
2932
this.showHelp()
3033
process.exit(0)
@@ -57,17 +60,20 @@ CLI version ${pkg.version}`
5760
console.log(new Invoker().version())
5861
}
5962

60-
static convertFromStdin (options, args) {
61-
stdin.read((data) => {
62-
if (args['timings']) {
63-
const timings = asciidoctor.Timings.create()
64-
const instanceOptions = Object.assign({}, options, { timings })
65-
Invoker.convert(asciidoctor.convert, data, instanceOptions)
66-
timings.printReport(process.stderr, '-')
67-
} else {
68-
Invoker.convert(asciidoctor.convert, data, options)
69-
}
70-
})
63+
static async readFromStdin () {
64+
return stdin.read()
65+
}
66+
67+
static async convertFromStdin (options, args) {
68+
const data = await Invoker.readFromStdin()
69+
if (args['timings']) {
70+
const timings = asciidoctor.Timings.create()
71+
const instanceOptions = Object.assign({}, options, { timings })
72+
Invoker.convert(asciidoctor.convert, data, instanceOptions)
73+
timings.printReport(process.stderr, '-')
74+
} else {
75+
Invoker.convert(asciidoctor.convert, data, options)
76+
}
7177
}
7278

7379
static convert (processorFn, input, options) {
@@ -101,12 +107,6 @@ CLI version ${pkg.version}`
101107
Invoker.convertFile(file, options)
102108
}
103109
})
104-
let code = 0
105-
const logger = asciidoctor.LoggerManager.getLogger()
106-
if (logger && typeof logger.getMaxSeverity === 'function' && logger.getMaxSeverity() && logger.getMaxSeverity() >= options['failure_level']) {
107-
code = 1
108-
}
109-
process.exit(code)
110110
}
111111

112112
static requireLibrary (requirePath, cwd = process.cwd()) {
@@ -136,6 +136,15 @@ CLI version ${pkg.version}`
136136
})
137137
}
138138
}
139+
140+
static exit (failureLevel) {
141+
let code = 0
142+
const logger = asciidoctor.LoggerManager.getLogger()
143+
if (logger && typeof logger.getMaxSeverity === 'function' && logger.getMaxSeverity() && logger.getMaxSeverity() >= failureLevel) {
144+
code = 1
145+
}
146+
process.exit(code)
147+
}
139148
}
140149

141150
module.exports = Invoker

lib/stdin.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const readFromStdin = (callback) => {
1+
const readFromStdin = async () => new Promise((resolve, reject) => {
22
const encoding = 'utf-8'
33
let data
44
data = ''
@@ -9,12 +9,15 @@ const readFromStdin = (callback) => {
99
data += chunk
1010
}
1111
})
12+
process.stdin.on('error', (error) => {
13+
reject(error)
14+
})
1215
process.stdin.on('end', function () {
1316
// There will be a trailing \n from the user hitting enter. Get rid of it.
1417
data = data.replace(/\n$/, '')
15-
callback(data)
18+
resolve(data)
1619
})
17-
}
20+
})
1821

1922
module.exports = {
2023
read: readFromStdin

test/test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,12 @@ describe('Options converter', () => {
114114
})
115115

116116
describe('Read from stdin', () => {
117-
it('should read from stdin', () => {
118-
sinon.stub(stdin, 'read').yields('An *AsciiDoc* input')
117+
it('should read from stdin', async () => {
118+
sinon.stub(stdin, 'read').resolves('An *AsciiDoc* input')
119119
sinon.stub(processor, 'convert')
120+
sinon.stub(process, 'exit')
120121
try {
121-
new Invoker(defaultOptions.parse(['/path/to/node', '/path/to/asciidoctor', '-'])).invoke()
122+
await new Invoker(defaultOptions.parse(['/path/to/node', '/path/to/asciidoctor', '-'])).invoke()
122123
expect(stdin.read.called).to.be.true()
123124
expect(processor.convert.called).to.be.true()
124125
const firstArgument = processor.convert.getCall(0).args[0]
@@ -128,6 +129,7 @@ describe('Read from stdin', () => {
128129
} finally {
129130
stdin.read.restore()
130131
processor.convert.restore()
132+
process.exit.restore()
131133
}
132134
})
133135
})
@@ -199,6 +201,7 @@ describe('Version', () => {
199201
return `Asciidoctor reveal.js 3.0.1 using ${super.version()}`
200202
}
201203
}
204+
202205
new CustomInvoker(defaultOptions.parse(['/path/to/node', '/path/to/asciidoctor', '-v'])).invoke()
203206
expect(process.exit.called).to.be.true()
204207
expect(process.exit.calledWith(0)).to.be.true()
@@ -224,7 +227,8 @@ describe('Process files', () => {
224227
it('should exit with code 1 when failure level is lower than the maximum logging level', () => {
225228
sinon.stub(process, 'exit')
226229
try {
227-
Invoker.processFiles([bookFilePath], false, false, { failure_level: 3 }) // ERROR: 3
230+
Invoker.processFiles([bookFilePath], false, false)
231+
Invoker.exit(3) // ERROR: 3
228232
expect(process.exit.called).to.be.true()
229233
expect(process.exit.calledWith(1)).to.be.true()
230234
} finally {
@@ -235,7 +239,8 @@ describe('Process files', () => {
235239
it('should exit with code 0 when failure level is lower than the maximum logging level', () => {
236240
sinon.stub(process, 'exit')
237241
try {
238-
Invoker.processFiles([bookFilePath], false, false, { failure_level: 4 }) // FATAL: 4
242+
Invoker.processFiles([bookFilePath], false, false)
243+
Invoker.exit(4) // FATAL: 4
239244
expect(process.exit.called).to.be.true()
240245
expect(process.exit.calledWith(0)).to.be.true()
241246
} finally {

0 commit comments

Comments
 (0)