Skip to content

Commit

Permalink
Support input from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
danyill committed Jan 23, 2020
1 parent 3da382e commit cafcc16
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
32 changes: 31 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const fs = require('fs')
const pkg = require('../package.json')
const { Options, Invoker, processor } = require('@asciidoctor/cli')
const chokidar = require('chokidar')
Expand All @@ -9,6 +10,7 @@ class ProcessorEmitter extends EventEmitter {}
const processorEmitter = new ProcessorEmitter()

const converter = require('./converter.js')
const stdin = require('./stdin')

async function convertFiles (files, argv, options, verbose, preview) {
for (const file of files) {
Expand All @@ -19,6 +21,18 @@ async function convertFiles (files, argv, options, verbose, preview) {
}
}

function getTemporaryAdocFile (workingDir) {
let tempFile
// random file name for stdin data
const name = 'asciidoctor-pdf-' + Math.random().toString(36).substring(2, 15)
if (path.isAbsolute(workingDir)) {
tempFile = path.join(workingDir, `${name}.adoc`)
} else {
tempFile = path.normalize(path.join(process.cwd(), workingDir, `${name}.adoc`))
}
return tempFile
}

class PdfOptions {
constructor () {
this.options = new Options()
Expand Down Expand Up @@ -87,7 +101,23 @@ class PdfInvoker extends Invoker {
converter.registerTemplateConverter(processor, templates)
Invoker.prepareProcessor(args, processor)
const options = this.options.options
if (files && files.length > 0) {
if (this.options.stdin) {
const dir = this.options.base_dir || this.options.doc_dir || process.cwd()
const adocFile = getTemporaryAdocFile(dir)
const adocFilePath = path.parse(adocFile)
const htmlFilePath = path.join(adocFilePath.dir, adocFilePath.name + '.html')
stdin.read((data) => {
fs.writeFile(adocFile, data, { flag: 'wx' }, function (err) {
if (err) throw err
convertFiles([adocFile], args, options, verbose, preview).then(() => {
// this should be handled async but skills inadequate :-(
fs.unlinkSync(adocFile)
fs.unlinkSync(htmlFilePath)
})
})
})
return { exit: false }
} else if (files && files.length > 0) {
await convertFiles(files, args, options, verbose, preview)
if (watch) {
const watchFiles = files.map((file) => {
Expand Down
21 changes: 21 additions & 0 deletions lib/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const readFromStdin = (callback) => {
const encoding = 'utf-8'
let data
data = ''
process.stdin.setEncoding(encoding)
process.stdin.on('readable', function () {
const chunk = process.stdin.read()
if (chunk !== null) {
data += chunk
}
})
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)
})
}

module.exports = {
read: readFromStdin
}

0 comments on commit cafcc16

Please sign in to comment.