Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix #353: read input data using stream #358

Merged
merged 5 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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