From 8e01650950aecbfa5844fe1199939f472b04aac6 Mon Sep 17 00:00:00 2001 From: mh-cbon Date: Mon, 13 Mar 2017 23:10:51 +0100 Subject: [PATCH 1/5] cli: prevent brutal exit when the process is doing async work --- src/cli/cli.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli/cli.js b/src/cli/cli.js index 3080c77b..e06dfc55 100644 --- a/src/cli/cli.js +++ b/src/cli/cli.js @@ -37,9 +37,10 @@ if (command === 'makehtml') { require('./makehtml.cmd.js').run(); } else { yargs.showHelp(); + process.exit(0); } if (argv.help) { yargs.showHelp(); + process.exit(0); } -process.exit(0); From 65dbb0de2d92822538b9a9e8f02bed0ab606849d Mon Sep 17 00:00:00 2001 From: mh-cbon Date: Mon, 13 Mar 2017 23:11:39 +0100 Subject: [PATCH 2/5] cli: read input data suing a stream to fix #353 --- src/cli/makehtml.cmd.js | 48 ++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/cli/makehtml.cmd.js b/src/cli/makehtml.cmd.js index eabc7169..0d3e62d9 100644 --- a/src/cli/makehtml.cmd.js +++ b/src/cli/makehtml.cmd.js @@ -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, @@ -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 = {}, @@ -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) { From 790397b696684c32a2cc4187f4580f00af9c3d89 Mon Sep 17 00:00:00 2001 From: mh-cbon Date: Mon, 13 Mar 2017 23:16:46 +0100 Subject: [PATCH 3/5] cli: add error return support --- src/cli/cli.js | 4 +++- src/cli/makehtml.cmd.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/cli/cli.js b/src/cli/cli.js index e06dfc55..2523881a 100644 --- a/src/cli/cli.js +++ b/src/cli/cli.js @@ -34,7 +34,9 @@ var argv = yargs.argv, command = argv._[0]; if (command === 'makehtml') { - require('./makehtml.cmd.js').run(); + require('./makehtml.cmd.js').run(function(err) { + console.error(err) + }); } else { yargs.showHelp(); process.exit(0); diff --git a/src/cli/makehtml.cmd.js b/src/cli/makehtml.cmd.js index 0d3e62d9..7c0c7e4e 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', @@ -110,6 +110,7 @@ function run () { messenger.printMsg('Reading data from ' + readMode + '...'); readFrom(argv.i, enc, function(err, md) { + if (err) return cb(err) // process the input messenger.printMsg('Parsing markdown...'); html = converter.makeHtml(md); @@ -118,6 +119,7 @@ function run () { messenger.printMsg('Writing data to ' + writeMode + '...'); write(html, append); messenger.okExit(); + cb() }) function parseOptions (flavor) { From 6c9ec84fabb040387621f6a27c58c1db206f288f Mon Sep 17 00:00:00 2001 From: mh-cbon Date: Mon, 13 Mar 2017 23:17:14 +0100 Subject: [PATCH 4/5] cli: add error return support --- src/cli/cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/cli.js b/src/cli/cli.js index 2523881a..ad6bc935 100644 --- a/src/cli/cli.js +++ b/src/cli/cli.js @@ -35,7 +35,7 @@ var argv = yargs.argv, if (command === 'makehtml') { require('./makehtml.cmd.js').run(function(err) { - console.error(err) + err && console.error(err) }); } else { yargs.showHelp(); From 37b58237d0df57b309222d8992ab76b2fffad1dc Mon Sep 17 00:00:00 2001 From: mh-cbon Date: Mon, 13 Mar 2017 23:27:37 +0100 Subject: [PATCH 5/5] lint --- src/cli/cli.js | 6 ++++-- src/cli/makehtml.cmd.js | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/cli/cli.js b/src/cli/cli.js index ad6bc935..c051b555 100644 --- a/src/cli/cli.js +++ b/src/cli/cli.js @@ -34,8 +34,10 @@ var argv = yargs.argv, command = argv._[0]; if (command === 'makehtml') { - require('./makehtml.cmd.js').run(function(err) { - err && console.error(err) + require('./makehtml.cmd.js').run(function (err) { + if (err) { + console.error(err); + } }); } else { yargs.showHelp(); diff --git a/src/cli/makehtml.cmd.js b/src/cli/makehtml.cmd.js index 7c0c7e4e..66ec4f33 100644 --- a/src/cli/makehtml.cmd.js +++ b/src/cli/makehtml.cmd.js @@ -89,7 +89,7 @@ function run (cb) { append = argv.a || false, options = parseOptions(flavor), converter = new showdown.Converter(options), - md, html; + html; // Load extensions if (argv.e) { @@ -109,8 +109,11 @@ function run (cb) { // read the input messenger.printMsg('Reading data from ' + readMode + '...'); - readFrom(argv.i, enc, function(err, md) { - if (err) return cb(err) + readFrom(argv.i, enc, function (err, md) { + if (err) { + return cb(err); + } + // process the input messenger.printMsg('Parsing markdown...'); html = converter.makeHtml(md); @@ -119,8 +122,8 @@ function run (cb) { messenger.printMsg('Writing data to ' + writeMode + '...'); write(html, append); messenger.okExit(); - cb() - }) + cb(); + }); function parseOptions (flavor) { var options = {}, @@ -158,20 +161,20 @@ function run (cb) { return options; } - function readFrom(src, enc, cb) { + function readFrom (src, enc, cb) { var stream = process.stdin; - if(src && src.length) { + 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) { + stream.on('data', function (chunk) { data += chunk.toString(); }); - stream.on('end',function() { - cb(null, data) + stream.on('end',function () { + cb(null, data); }); stream.on('error', cb); }