From e97275b76029f9a15d691cf43fc466790300f5c7 Mon Sep 17 00:00:00 2001 From: ido Date: Thu, 4 Dec 2014 18:04:33 +0200 Subject: [PATCH] add optionator --- package.json | 1 + src/cli.js | 106 +++++++++++--------------------------- src/formatters/stylish.js | 18 +++---- src/options.js | 80 ++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 85 deletions(-) create mode 100644 src/options.js diff --git a/package.json b/package.json index 125d007..25b6080 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "escodegen": "^1.4.1", "esprima": "^1.2.2", "lodash": "^2.4.1", + "optionator": "^0.4.0", "text-table": "^0.2.0" }, "devDependencies": { diff --git a/src/cli.js b/src/cli.js index a546a75..0d6d83f 100755 --- a/src/cli.js +++ b/src/cli.js @@ -10,84 +10,35 @@ var api = require('./api'); var context = require('./context'); var shell = require('./shell'); var pkg = require('../package.json'); -var options = {commonJS: false, force: false, json: false}; - -//if (process.argv.length > 2) { -// var files = []; -// _.forEach(process.argv.slice(2),function (param) { -// if (param === '-v' || param === '--version') { -// console.log(pkg.version); -// } else if (param === '-h' || param === '--help') { -// printHelp(); -// } else if (param === '-c' || param === '--common') { -// options.commonJS = true; -// } else if (param === '-f' || param === '--force') { -// options.force = true; -// } else if (param === '-j' || param === '--json') { // TODO use optionator -// context.options.format = 'json'; -// } else if (param !== '--no-color') { -// files.push(param); -// } -// }); -// _.forEach(files, handleSingleFile); -// shell.printResults(context); -//} else { -// printHelp(); -//} - -function parseOptions(args) { - var parsedOptions = {_: [], version: false, commonJS: false, force: false, format: 'stylish'}; - _.forEach(args, function (param) { - if (param === '-v' || param === '--version') { - parsedOptions.version = true; - } else if (param === '-h' || param === '--help') { - printHelp(); - } else if (param === '-c' || param === '--common') { - parsedOptions.commonJS = true; - } else if (param === '-f' || param === '--force') { - parsedOptions.force = true; - } else if (param === '-j' || param === '--json') { // TODO use optionator - parsedOptions.format = 'json'; - context.options.format = 'json'; - } else if (param !== '--no-color') { - parsedOptions._.push(param); - } - }); - return parsedOptions; -} +//var defaultOptions = {commonJS: false, force: false, json: false}; +var options = require('./options'); function executeOptions(currentOptions) { var ret = 0; - if (currentOptions.help) { - printHelp(); - } else if (currentOptions.version) { - console.log(pkg.version); + var files = currentOptions._; + if (currentOptions.version) { + console.log('v' + pkg.version); + } else if (currentOptions.help) { + if (files.length) { + console.log(options.generateHelpForOption(files[0])); + } else { + console.log(options.generateHelp()); + } + } else if (!files.length) { + console.log(options.generateHelp()); } else { - _.forEach(currentOptions._, function (f) { - handleSingleFile(f, currentOptions); - }); + context.options.format = currentOptions.format; + _.forEach(files, handleSingleFile.bind(this, currentOptions)); ret = shell.printResults(context); } return ret; } -function printHelp() { - console.log(pkg.name + ' ' + pkg.version); - console.log(pkg.description); - console.log(''); - console.log('Usage:'); - console.log(' $ rt [,] []'); - console.log(''); - console.log('Options:'); - console.log(' -v, --version Outputs the version number.'); - console.log(' -h, --help Show help.'); - console.log(' -j, --json Report output format. [stylish,json]'); -// console.log(' -ft, --format Report output format. [stylish,json]'); - console.log(' --common Use Common JS output. default: false'); - console.log(' -f --force Force creation of output. default: false'); -} - -function handleSingleFile(filename, currentOptions) { +/** + * @param {*} currentOptions + * @param {string} filename file name to process + */ +function handleSingleFile(currentOptions, filename) { if (path.extname(filename) !== '.rt') { context.error('invalid file, only handle rt files', filename); return;// only handle html files @@ -103,13 +54,13 @@ function handleSingleFile(filename, currentOptions) { api.convertFile(filename, filename + '.js', currentOptions, context); } catch (e) { context.error(e.message, filename, e.line || -1, -1, e.index || -1); -// if (options.json) { +// if (defaultOptions.json) { // context.error(e.message, filename, e.line || -1, -1, e.index || -1); // console.log(JSON.stringify(context.getMessages(), undefined, 2)); // } else { // console.log('Error processing file: ' + filename + ', ' + e.message + ' line: ' + e.line || -1); // } - // if (options.stack) + // if (defaultOptions.stack) // console.log(e.stack); } } @@ -120,12 +71,15 @@ function handleSingleFile(filename, currentOptions) { * @returns {int} The exit code for the operation. */ function execute(args) { - if (args.length > 2) { - var opts = parseOptions(args.slice(2)); - return executeOptions(opts); + var currentOptions; + try { + currentOptions = options.parse(args); + } catch (error) { + console.error(error.message); + return 1; } - printHelp(); - return 0; + console.log(currentOptions); + return executeOptions(currentOptions); } module.exports = {execute: execute, executeOptions: executeOptions}; \ No newline at end of file diff --git a/src/formatters/stylish.js b/src/formatters/stylish.js index 1336bee..48cd7e4 100644 --- a/src/formatters/stylish.js +++ b/src/formatters/stylish.js @@ -38,12 +38,12 @@ module.exports = function (warnings, config) { warnings.map(function (message) { return [ '', - message.file, + message.file || '', lineText(message.line || 0), lineText(message.column || 0), getMessageType(message), // message.message.replace(/\.$/, ""), - message.msg + message.msg || '' // chalk.gray(message.ruleId) ]; }), @@ -58,18 +58,18 @@ module.exports = function (warnings, config) { var buf = []; - buf.push(output + '\n'); + buf.push(output); var grouped = _.groupBy(warnings, 'level'); var errCount = grouped.ERROR ? grouped.ERROR.length : 0; var warnCount = grouped.WARN ? grouped.WARN.length : 0; - var infoCount = grouped.INFO ? grouped.INFO.length : 0; + //var infoCount = grouped.INFO ? grouped.INFO.length : 0; // buf.push(errCount + ' ' + warnCount + ' ' + infoCount + '\n'); if (errCount === 0 && warnCount === 0) { - buf.push('React templates done\n'); + buf.push(chalk.green('React templates done')); } else { var msg = []; if (errCount > 0) { @@ -77,11 +77,11 @@ module.exports = function (warnings, config) { } else { msg.push(warnCount + ' ' + pluralize(warnCount, 'warning', 'warnings')); } - buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + msg.join(', ')) + '\n'); + buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + msg.join(', '))); if (errCount > 0) { - buf.push('React templates failed due to errors\n'); + buf.push(chalk.red('React templates failed due to errors')); } else { - buf.push('React templates done with warnings\n'); + buf.push(chalk.yellow('React templates done with warnings')); } } @@ -92,5 +92,5 @@ module.exports = function (warnings, config) { // buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + warnings.length + ' ' + pluralize(warnings.length, 'problem', 'problems')) + '\n'); // buf.push('React templates done with warnings\n'); // } - return buf.join(''); + return buf.join('\n'); }; \ No newline at end of file diff --git a/src/options.js b/src/options.js new file mode 100644 index 0000000..53d7ae0 --- /dev/null +++ b/src/options.js @@ -0,0 +1,80 @@ +/** + * @fileoverview Options configuration for optionator. + * @author idok + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +var optionator = require('optionator'); +var pkg = require('../package.json'); +var reactDOMSupport = require('./reactDOMSupport'); + +//------------------------------------------------------------------------------ +// Initialization and Public Interface +//------------------------------------------------------------------------------ + +// exports 'parse(args)', 'generateHelp()', and 'generateHelpForOption(optionName)' +module.exports = optionator({ + prepend: [ + pkg.name + ' v' + pkg.version, + pkg.description, + '', + 'Usage:', + '$ rt [,] []' + ].join('\n'), + concatRepeatedArrays: true, + mergeRepeatedObjects: true, + options: [{ + heading: 'Options' + }, { + option: 'help', + alias: 'h', + type: 'Boolean', + description: 'Show help.' + }, { + option: 'color', + alias: 'c', + default: 'true', + type: 'Boolean', + description: 'Use colors in output.' + }, { + option: 'common', + alias: 'm', + default: 'false', + type: 'Boolean', + description: 'Use Common JS output.' + }, { + option: 'force', + alias: 'r', + default: 'false', + type: 'Boolean', + description: 'Force creation of output. skip file check.' + }, { + option: 'format', + alias: 'f', + type: 'String', + default: 'stylish', + //enum: ['stylish', 'json'], + description: 'Use a specific output format. (stylish|json)' + }, { + option: 'target-version', + alias: 't', + type: 'String', + default: '0.12.1', + //enum: ['stylish', 'json'], + description: 'React version to generate code for (' + Object.keys(reactDOMSupport).join(', ') + ')' + }, { + option: 'version', + alias: 'v', + type: 'Boolean', + description: 'Outputs the version number.' + }, { + option: 'stack', + alias: 'k', + type: 'Boolean', + description: 'Show stack trace on errors.' + }] +});