1
0
mirror of https://github.com/bobwen-dev/react-templates synced 2025-04-12 00:56:39 +02:00

add optionator

This commit is contained in:
ido 2014-12-04 18:04:33 +02:00
parent 13fffaa55d
commit e97275b760
4 changed files with 120 additions and 85 deletions

View File

@ -28,6 +28,7 @@
"escodegen": "^1.4.1", "escodegen": "^1.4.1",
"esprima": "^1.2.2", "esprima": "^1.2.2",
"lodash": "^2.4.1", "lodash": "^2.4.1",
"optionator": "^0.4.0",
"text-table": "^0.2.0" "text-table": "^0.2.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -10,84 +10,35 @@ var api = require('./api');
var context = require('./context'); var context = require('./context');
var shell = require('./shell'); var shell = require('./shell');
var pkg = require('../package.json'); var pkg = require('../package.json');
var options = {commonJS: false, force: false, json: false}; //var defaultOptions = {commonJS: false, force: false, json: false};
var options = require('./options');
//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;
}
function executeOptions(currentOptions) { function executeOptions(currentOptions) {
var ret = 0; var ret = 0;
if (currentOptions.help) { var files = currentOptions._;
printHelp(); if (currentOptions.version) {
} else if (currentOptions.version) { console.log('v' + pkg.version);
console.log(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 { } else {
_.forEach(currentOptions._, function (f) { context.options.format = currentOptions.format;
handleSingleFile(f, currentOptions); _.forEach(files, handleSingleFile.bind(this, currentOptions));
});
ret = shell.printResults(context); ret = shell.printResults(context);
} }
return ret; return ret;
} }
function printHelp() { /**
console.log(pkg.name + ' ' + pkg.version); * @param {*} currentOptions
console.log(pkg.description); * @param {string} filename file name to process
console.log(''); */
console.log('Usage:'); function handleSingleFile(currentOptions, filename) {
console.log(' $ rt <filename>[,<filename>] [<args>]');
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) {
if (path.extname(filename) !== '.rt') { if (path.extname(filename) !== '.rt') {
context.error('invalid file, only handle rt files', filename); context.error('invalid file, only handle rt files', filename);
return;// only handle html files return;// only handle html files
@ -103,13 +54,13 @@ function handleSingleFile(filename, currentOptions) {
api.convertFile(filename, filename + '.js', currentOptions, context); api.convertFile(filename, filename + '.js', currentOptions, context);
} catch (e) { } catch (e) {
context.error(e.message, filename, e.line || -1, -1, e.index || -1); 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); // context.error(e.message, filename, e.line || -1, -1, e.index || -1);
// console.log(JSON.stringify(context.getMessages(), undefined, 2)); // console.log(JSON.stringify(context.getMessages(), undefined, 2));
// } else { // } else {
// console.log('Error processing file: ' + filename + ', ' + e.message + ' line: ' + e.line || -1); // console.log('Error processing file: ' + filename + ', ' + e.message + ' line: ' + e.line || -1);
// } // }
// if (options.stack) // if (defaultOptions.stack)
// console.log(e.stack); // console.log(e.stack);
} }
} }
@ -120,12 +71,15 @@ function handleSingleFile(filename, currentOptions) {
* @returns {int} The exit code for the operation. * @returns {int} The exit code for the operation.
*/ */
function execute(args) { function execute(args) {
if (args.length > 2) { var currentOptions;
var opts = parseOptions(args.slice(2)); try {
return executeOptions(opts); currentOptions = options.parse(args);
} catch (error) {
console.error(error.message);
return 1;
} }
printHelp(); console.log(currentOptions);
return 0; return executeOptions(currentOptions);
} }
module.exports = {execute: execute, executeOptions: executeOptions}; module.exports = {execute: execute, executeOptions: executeOptions};

View File

@ -38,12 +38,12 @@ module.exports = function (warnings, config) {
warnings.map(function (message) { warnings.map(function (message) {
return [ return [
'', '',
message.file, message.file || '',
lineText(message.line || 0), lineText(message.line || 0),
lineText(message.column || 0), lineText(message.column || 0),
getMessageType(message), getMessageType(message),
// message.message.replace(/\.$/, ""), // message.message.replace(/\.$/, ""),
message.msg message.msg || ''
// chalk.gray(message.ruleId) // chalk.gray(message.ruleId)
]; ];
}), }),
@ -58,18 +58,18 @@ module.exports = function (warnings, config) {
var buf = []; var buf = [];
buf.push(output + '\n'); buf.push(output);
var grouped = _.groupBy(warnings, 'level'); var grouped = _.groupBy(warnings, 'level');
var errCount = grouped.ERROR ? grouped.ERROR.length : 0; var errCount = grouped.ERROR ? grouped.ERROR.length : 0;
var warnCount = grouped.WARN ? grouped.WARN.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'); // buf.push(errCount + ' ' + warnCount + ' ' + infoCount + '\n');
if (errCount === 0 && warnCount === 0) { if (errCount === 0 && warnCount === 0) {
buf.push('React templates done\n'); buf.push(chalk.green('React templates done'));
} else { } else {
var msg = []; var msg = [];
if (errCount > 0) { if (errCount > 0) {
@ -77,11 +77,11 @@ module.exports = function (warnings, config) {
} else { } else {
msg.push(warnCount + ' ' + pluralize(warnCount, 'warning', 'warnings')); 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) { if (errCount > 0) {
buf.push('React templates failed due to errors\n'); buf.push(chalk.red('React templates failed due to errors'));
} else { } 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(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + warnings.length + ' ' + pluralize(warnings.length, 'problem', 'problems')) + '\n');
// buf.push('React templates done with warnings\n'); // buf.push('React templates done with warnings\n');
// } // }
return buf.join(''); return buf.join('\n');
}; };

80
src/options.js Normal file
View File

@ -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 <filename>[,<filename>] [<args>]'
].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.'
}]
});