Skip to content

Commit

Permalink
fix(cli): read input data using stream (#358)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mh-cbon authored Feb 24, 2022
1 parent 3fd1ea6 commit 1967652
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
9 changes: 7 additions & 2 deletions src/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
57 changes: 31 additions & 26 deletions src/cli/makehtml.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) {
Expand All @@ -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 = {},
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1967652

Please sign in to comment.