Skip to content

Commit

Permalink
Merge pull request #82 from Mogztter/issue-81-export-read-from-stdin
Browse files Browse the repository at this point in the history
Export a static function to read from stdin
  • Loading branch information
ggrossetie authored Feb 1, 2020
2 parents 6c14e87 + b73dbb4 commit c1e3f2f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
47 changes: 28 additions & 19 deletions lib/invoker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Invoker {
this.options = options
}

invoke () {
async invoke () {
const processArgs = this.options.argv.slice(2)
const { args } = this.options
const { verbose, version, files } = args
Expand All @@ -21,10 +21,13 @@ class Invoker {
}
Invoker.prepareProcessor(args, asciidoctor)
const options = this.options.options
const failureLevel = options['failure_level']
if (this.options.stdin) {
Invoker.convertFromStdin(options, args)
await Invoker.convertFromStdin(options, args)
Invoker.exit(failureLevel)
} else if (files && files.length > 0) {
Invoker.processFiles(files, verbose, args['timings'], options)
Invoker.exit(failureLevel)
} else {
this.showHelp()
process.exit(0)
Expand Down Expand Up @@ -57,17 +60,20 @@ CLI version ${pkg.version}`
console.log(new Invoker().version())
}

static convertFromStdin (options, args) {
stdin.read((data) => {
if (args['timings']) {
const timings = asciidoctor.Timings.create()
const instanceOptions = Object.assign({}, options, { timings })
Invoker.convert(asciidoctor.convert, data, instanceOptions)
timings.printReport(process.stderr, '-')
} else {
Invoker.convert(asciidoctor.convert, data, options)
}
})
static async readFromStdin () {
return stdin.read()
}

static async convertFromStdin (options, args) {
const data = await Invoker.readFromStdin()
if (args['timings']) {
const timings = asciidoctor.Timings.create()
const instanceOptions = Object.assign({}, options, { timings })
Invoker.convert(asciidoctor.convert, data, instanceOptions)
timings.printReport(process.stderr, '-')
} else {
Invoker.convert(asciidoctor.convert, data, options)
}
}

static convert (processorFn, input, options) {
Expand Down Expand Up @@ -101,12 +107,6 @@ CLI version ${pkg.version}`
Invoker.convertFile(file, options)
}
})
let code = 0
const logger = asciidoctor.LoggerManager.getLogger()
if (logger && typeof logger.getMaxSeverity === 'function' && logger.getMaxSeverity() && logger.getMaxSeverity() >= options['failure_level']) {
code = 1
}
process.exit(code)
}

static requireLibrary (requirePath, cwd = process.cwd()) {
Expand Down Expand Up @@ -136,6 +136,15 @@ CLI version ${pkg.version}`
})
}
}

static exit (failureLevel) {
let code = 0
const logger = asciidoctor.LoggerManager.getLogger()
if (logger && typeof logger.getMaxSeverity === 'function' && logger.getMaxSeverity() && logger.getMaxSeverity() >= failureLevel) {
code = 1
}
process.exit(code)
}
}

module.exports = Invoker
Expand Down
9 changes: 6 additions & 3 deletions lib/stdin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const readFromStdin = (callback) => {
const readFromStdin = async () => new Promise((resolve, reject) => {
const encoding = 'utf-8'
let data
data = ''
Expand All @@ -9,12 +9,15 @@ const readFromStdin = (callback) => {
data += chunk
}
})
process.stdin.on('error', (error) => {
reject(error)
})
process.stdin.on('end', function () {
// There will be a trailing \n from the user hitting enter. Get rid of it.
data = data.replace(/\n$/, '')
callback(data)
resolve(data)
})
}
})

module.exports = {
read: readFromStdin
Expand Down
15 changes: 10 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ describe('Options converter', () => {
})

describe('Read from stdin', () => {
it('should read from stdin', () => {
sinon.stub(stdin, 'read').yields('An *AsciiDoc* input')
it('should read from stdin', async () => {
sinon.stub(stdin, 'read').resolves('An *AsciiDoc* input')
sinon.stub(processor, 'convert')
sinon.stub(process, 'exit')
try {
new Invoker(defaultOptions.parse(['/path/to/node', '/path/to/asciidoctor', '-'])).invoke()
await new Invoker(defaultOptions.parse(['/path/to/node', '/path/to/asciidoctor', '-'])).invoke()
expect(stdin.read.called).to.be.true()
expect(processor.convert.called).to.be.true()
const firstArgument = processor.convert.getCall(0).args[0]
Expand All @@ -128,6 +129,7 @@ describe('Read from stdin', () => {
} finally {
stdin.read.restore()
processor.convert.restore()
process.exit.restore()
}
})
})
Expand Down Expand Up @@ -199,6 +201,7 @@ describe('Version', () => {
return `Asciidoctor reveal.js 3.0.1 using ${super.version()}`
}
}

new CustomInvoker(defaultOptions.parse(['/path/to/node', '/path/to/asciidoctor', '-v'])).invoke()
expect(process.exit.called).to.be.true()
expect(process.exit.calledWith(0)).to.be.true()
Expand All @@ -224,7 +227,8 @@ describe('Process files', () => {
it('should exit with code 1 when failure level is lower than the maximum logging level', () => {
sinon.stub(process, 'exit')
try {
Invoker.processFiles([bookFilePath], false, false, { failure_level: 3 }) // ERROR: 3
Invoker.processFiles([bookFilePath], false, false)
Invoker.exit(3) // ERROR: 3
expect(process.exit.called).to.be.true()
expect(process.exit.calledWith(1)).to.be.true()
} finally {
Expand All @@ -235,7 +239,8 @@ describe('Process files', () => {
it('should exit with code 0 when failure level is lower than the maximum logging level', () => {
sinon.stub(process, 'exit')
try {
Invoker.processFiles([bookFilePath], false, false, { failure_level: 4 }) // FATAL: 4
Invoker.processFiles([bookFilePath], false, false)
Invoker.exit(4) // FATAL: 4
expect(process.exit.called).to.be.true()
expect(process.exit.calledWith(0)).to.be.true()
} finally {
Expand Down

0 comments on commit c1e3f2f

Please sign in to comment.