From 1967652acfff5c676052826329d82b782bbd35db Mon Sep 17 00:00:00 2001 From: mh-cbon Date: Thu, 24 Feb 2022 01:06:08 +0100 Subject: [PATCH] fix(cli): read input data using stream (#358) * cli: prevent brutal exit when the process is doing async work * cli: read input data suing a stream to fix #353 * cli: add error return support * cli: add error return support * lint Closes #358 --- src/cli/cli.js | 9 +++++-- src/cli/makehtml.cmd.js | 57 ++++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/cli/cli.js b/src/cli/cli.js index 3080c77b4..c051b555f 100644 --- a/src/cli/cli.js +++ b/src/cli/cli.js @@ -34,12 +34,17 @@ var argv = yargs.argv, command = argv._[0]; if (command === 'makehtml') { - require('./makehtml.cmd.js').run(); + require('./makehtml.cmd.js').run(function (err) { + if (err) { + console.error(err); + } + }); } else { yargs.showHelp(); + process.exit(0); } if (argv.help) { yargs.showHelp(); + process.exit(0); } -process.exit(0); diff --git a/src/cli/makehtml.cmd.js b/src/cli/makehtml.cmd.js index eabc7169a..66ec4f332 100644 --- a/src/cli/makehtml.cmd.js +++ b/src/cli/makehtml.cmd.js @@ -72,7 +72,7 @@ for (var opt in showdownOptions) { } } -function run () { +function run (cb) { 'use strict'; var argv = yargs.argv, readMode = (!argv.i || argv.i === '') ? 'stdin' : 'file', @@ -83,14 +83,13 @@ function run () { * @type {Messenger} */ messenger = new Messenger(msgMode, argv.q, argv.m), - read = (readMode === 'stdin') ? readFromStdIn : readFromFile, write = (writeMode === 'stdout') ? writeToStdOut : writeToFile, enc = argv.encoding || 'utf8', flavor = argv.p, append = argv.a || false, options = parseOptions(flavor), converter = new showdown.Converter(options), - md, html; + html; // Load extensions if (argv.e) { @@ -109,16 +108,22 @@ function run () { messenger.printMsg('...'); // read the input messenger.printMsg('Reading data from ' + readMode + '...'); - md = read(enc); - // process the input - messenger.printMsg('Parsing markdown...'); - html = converter.makeHtml(md); + readFrom(argv.i, enc, function (err, md) { + if (err) { + return cb(err); + } + + // process the input + messenger.printMsg('Parsing markdown...'); + html = converter.makeHtml(md); - // write the output - messenger.printMsg('Writing data to ' + writeMode + '...'); - write(html, append); - messenger.okExit(); + // write the output + messenger.printMsg('Writing data to ' + writeMode + '...'); + write(html, append); + messenger.okExit(); + cb(); + }); function parseOptions (flavor) { var options = {}, @@ -156,22 +161,22 @@ function run () { return options; } - function readFromStdIn () { - try { - var size = fs.fstatSync(process.stdin.fd).size; - return size > 0 ? fs.readSync(process.stdin.fd, size)[0] : ''; - } catch (e) { - var err = new Error('Could not read from stdin, reason: ' + e.message); - messenger.errorExit(err); - } - } - - function readFromFile (encoding) { - try { - return fs.readFileSync(argv.i, encoding); - } catch (err) { - messenger.errorExit(err); + function readFrom (src, enc, cb) { + var stream = process.stdin; + if (src && src.length) { + stream = fs.createReadStream(src, {encoding: enc}); + } else { + process.stdin.setEncoding(enc); + process.stdin.resume(); } + var data = ''; + stream.on('data', function (chunk) { + data += chunk.toString(); + }); + stream.on('end',function () { + cb(null, data); + }); + stream.on('error', cb); } function writeToStdOut (html) {