refactor(CLI): refactoring of CLI code

This commit is contained in:
Estevao Soares dos Santos 2017-01-09 02:08:23 +00:00
parent 2d6cd1e908
commit e3521bd8fa
6 changed files with 172 additions and 90 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

View File

@ -1,6 +0,0 @@
module.exports = exports = function errorExit(e) {
'use strict';
console.error('ERROR: ' + e.message);
console.error('Run \'showdown <command> -h\' for help');
process.exit(1);
};

View File

@ -1,18 +1,20 @@
var yargs = require('yargs'),
fs = require('fs'),
errorExit = require('./errorexit.js'),
showdown = require('../../dist/showdown');
Messenger = require('./messenger.js'),
showdown = require('../../dist/showdown'),
showdownOptions = showdown.getDefaultOptions(false);
yargs.reset()
.usage('Usage: showdown makehtml [options]')
.example('showdown makehtml -i', 'Reads from stdin and outputs to stdout')
.example('showdown makehtml -i foo.md -o bar.html', 'Reads \'foo.md\' and writes to \'bar.html\'')
.example('showdown makehtml -i --flavor="github"', 'Parses stdin using GFM style')
.version()
.alias('v', 'version')
.config('c')
.alias('c', 'config')
.help('h')
.alias('h', 'help')
.version()
.alias('v', 'version')
.option('i', {
alias : 'input',
describe: 'Input source. Usually a md file. If omitted or empty, reads from stdin',
@ -32,7 +34,8 @@ yargs.reset()
.option('a', {
alias : 'append',
describe: 'Append data to output instead of overwriting',
type: 'string'
type: 'string',
default: false
})
.option('e', {
alias : 'extensions',
@ -45,87 +48,135 @@ yargs.reset()
type: 'string'
});
yargs.options(showdown.getDefaultOptions(false));
argv = yargs.argv;
function run() {
'use strict';
var input = '',
enc = 'utf8',
output;
if (argv.encoding) {
enc = argv.encoding;
}
// to avoid passing extensions to converter
delete argv.extensions;
var converter = new showdown.Converter(argv);
// Load extensions
if (argv.e) {
for (var i = 0; i < argv.e.length; ++i) {
loadExtension(argv.e[i], converter);
}
}
if (!argv.i || argv.i === '') {
// 'i' is undefined or empty, read from stdin
try {
var size = fs.fstatSync(process.stdin.fd).size;
input = size > 0 ? fs.readSync(process.stdin.fd, size)[0] : '';
} catch (e) {
var err = new Error('Could not read from stdin, reason: ' + e.message);
errorExit(err);
}
// load showdown default options
for (var opt in showdownOptions) {
if (showdownOptions.hasOwnProperty(opt)) {
if (showdownOptions[opt].defaultValue === false) {
showdownOptions[opt].default = null;
} else {
// 'i' has a value, read from file
try {
input = fs.readFileSync(argv.i, enc);
} catch (err) {
errorExit(err);
showdownOptions[opt].default = showdownOptions[opt].defaultValue;
}
}
// Load flavor
if (argv.p) {
converter.setFlavor(argv.p);
}
// parse and convert file
output = converter.makeHtml(input);
// Write output
if (!argv.o || argv.o === '') {
// o is undefined or empty, write to stdout
process.stdout.write(output);
// we won't print anything since it would conspurcate stdout and,
// consequently, the outputted file
} else {
// o is has a value, presumably a file, write to it.
// If a flag is passed, it means we should append instead of overwriting.
// Only works with files, obviously
var write = (argv.a) ? fs.appendFileSync : fs.writeFileSync;
try {
write(argv.o, output);
} catch (err) {
errorExit(err);
}
console.error('DONE!');
yargs.option(opt, showdownOptions[opt]);
}
}
function loadExtension(path, converter) {
function run() {
'use strict';
var ext;
var argv = yargs.argv,
readMode = (!argv.i || argv.i === '') ? 'stdin' : 'file',
writeMode = (!argv.o || argv.o === '') ? 'stdout' : 'file',
msgMode = (writeMode === 'file') ? 'stdout' : 'stderr',
/**
* MSG object
* @type {Messenger}
*/
messenger = new Messenger(msgMode),
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;
// Load extensions
if (argv.e) {
messenger.printMsg('Loading extensions');
for (var i = 0; i < argv.e.length; ++i) {
try {
ext = require(path);
converter.addExtension(ext, path);
var ext = require(argv.e[i]);
converter.addExtension(ext, argv.e[i]);
} catch (e) {
console.error('Could not load extension ' + path + '. Reason:');
console.error(e.message);
messenger.printError('Could not load extension ' + argv.e[i] + '. Reason:');
messenger.errorExit(e);
}
}
}
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);
// write the output
messenger.printMsg('Writing data to ' + writeMode + '...');
write(html, append);
messenger.printMsg('\n');
messenger.okExit();
function parseOptions(flavor) {
var options = {},
flavorOpts = showdown.getFlavorOptions(flavor) || {};
// if flavor is not undefined, let's tell the user we're loading that preset
if (flavor) {
messenger.printMsg('Loading ' + flavor + ' flavor.');
}
for (var opt in argv) {
if (argv.hasOwnProperty(opt)) {
// first we load the default options
if (showdownOptions.hasOwnProperty(opt) && showdownOptions[opt].default !== null) {
options[opt] = showdownOptions[opt].default;
}
// we now override defaults with flavor, if a flavor was indeed passed
if (flavorOpts.hasOwnProperty(opt)) {
options[opt] = flavorOpts[opt];
}
// lastly we override with explicit passed options
// being careful not to pass CLI specific options, such as -v, -h or --extensions
if (showdownOptions.hasOwnProperty(opt)) {
if (argv[opt] === true) {
messenger.printMsg('Enabling option ' + opt);
options[opt] = argv[opt];
} else if (argv[opt] === false) {
options[opt] = argv[opt];
}
}
}
}
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 writeToStdOut(html) {
return process.stdout.write(html);
}
function writeToFile(html, append) {
// If a flag is passed, it means we should append instead of overwriting.
// Only works with files, obviously
var write = (append) ? fs.appendFileSync : fs.writeFileSync;
try {
write(argv.o, html);
} catch (err) {
messenger.errorExit(err);
}
}
}

39
src/cli/messenger.js Normal file
View File

@ -0,0 +1,39 @@
function Messenger(writeMode, supress, mute) {
'use strict';
writeMode = writeMode || 'stderr';
supress = !!supress;
mute = (!!supress || !!mute);
this._print = (writeMode === 'stdout') ? console.log : console.error;
this.errorExit = function (e) {
if (!mute) {
console.error('ERROR: ' + e.message);
console.error('Run \'showdown <command> -h\' for help');
}
process.exit(1);
};
this.okExit = function () {
if (!mute) {
this._print('DONE!');
}
process.exit(0);
};
this.printMsg = function (msg) {
if (supress || mute || !msg) {
return;
}
this._print(msg);
};
this.printError = function (msg) {
if (mute) {
return;
}
console.error(msg);
};
}
module.exports = Messenger;

View File

@ -1,4 +1,3 @@
/*
var semver = require('semver'),
cmd = 'node bin/showdown.js';
@ -15,4 +14,3 @@ describe('showdown cli', function () {
});
}
});
*/