Skip to content

Commit

Permalink
cli: read input data suing a stream to fix showdownjs#353
Browse files Browse the repository at this point in the history
  • Loading branch information
mh-cbon committed Mar 13, 2017
1 parent 8e01650 commit 65dbb0d
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/cli/makehtml.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ 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,
Expand All @@ -109,16 +108,17 @@ 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) {
// 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();
})

function parseOptions (flavor) {
var options = {},
Expand Down Expand Up @@ -156,22 +156,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 65dbb0d

Please sign in to comment.