mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
add force option
This commit is contained in:
parent
75323d07d7
commit
027787ce69
17
src/cli.js
Normal file → Executable file
17
src/cli.js
Normal file → Executable file
@ -8,7 +8,7 @@ var _ = require('lodash');
|
||||
var path = require('path');
|
||||
var reactTemplates = require('./reactTemplates');
|
||||
var pkg = require('../package.json');
|
||||
var options = {commonJS: false};
|
||||
var options = {commonJS: false, force: false};
|
||||
|
||||
if (process.argv.length > 2) {
|
||||
var files = [];
|
||||
@ -17,23 +17,30 @@ if (process.argv.length > 2) {
|
||||
console.log(pkg.version);
|
||||
} else if (param === '-h' || param === '--help') {
|
||||
printHelp();
|
||||
} else if (param === '--common') {
|
||||
} else if (param === '-c' || param === '--common') {
|
||||
options.commonJS = true;
|
||||
} else if (param === '-f' || param === '--force') {
|
||||
options.force = true;
|
||||
} else {
|
||||
files.push(param);
|
||||
}
|
||||
});
|
||||
_.forEach(files,handleSingleFile);
|
||||
|
||||
_.forEach(files, handleSingleFile);
|
||||
} else {
|
||||
printHelp();
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
console.log(pkg.name + ' ' + pkg.version);
|
||||
console.log(pkg.description);
|
||||
console.log('');
|
||||
console.log('Usage:');
|
||||
console.log(' $ node reactTemplates.js <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(' --common Use Common JS output. default: false');
|
||||
}
|
||||
|
||||
function handleSingleFile(filename) {
|
||||
|
@ -259,9 +259,10 @@ function extractDefinesFromJSXTag(html, defines) {
|
||||
|
||||
/**
|
||||
* @param {string} html
|
||||
* @param {{commonJS:boolean}} options
|
||||
* @return {string}
|
||||
*/
|
||||
function convertTemplateToReact(html,options) {
|
||||
function convertTemplateToReact(html, options) {
|
||||
// var x = cheerio.load(html);
|
||||
options = options || {};
|
||||
var defines = {react: 'React', lodash: '_'};
|
||||
@ -271,8 +272,8 @@ function convertTemplateToReact(html,options) {
|
||||
var body = convertHtmlToReact(rootNode.root()[0].children[0], context);
|
||||
var requirePaths = _(defines).keys().map(function (reqName) { return '"' + reqName + '"'; }).value().join(',');
|
||||
var requireVars = _(defines).values().value().join(',');
|
||||
var vars = _(defines).map(function (reqVar,reqPath) {return "var "+reqVar+" = require('"+reqPath+"');"}).join("\n");
|
||||
var data = {body: body, injectedFunctions: '', requireNames: requireVars, requirePaths: requirePaths, vars:vars};
|
||||
var vars = _(defines).map(function (reqVar, reqPath) { return 'var ' + reqVar + " = require('" + reqPath + "');"; }).join('\n');
|
||||
var data = {body: body, injectedFunctions: '', requireNames: requireVars, requirePaths: requirePaths, vars: vars};
|
||||
data.injectedFunctions = context.injectedFunctions.join('\n');
|
||||
var code = options.commonJS ? templateCommonJSTemplate(data) : templateAMDTemplate(data);
|
||||
try {
|
||||
@ -288,6 +289,7 @@ function convertTemplateToReact(html,options) {
|
||||
|
||||
/**
|
||||
* @param {string} source
|
||||
* @param {{commonJS:boolean}} options
|
||||
* @param {string} target
|
||||
*/
|
||||
function convertFile(source, target, options) {
|
||||
@ -297,7 +299,7 @@ function convertFile(source, target, options) {
|
||||
// }
|
||||
var util = require('./util');
|
||||
|
||||
if (!util.isStale(source, target)) {
|
||||
if (!options.force && !util.isStale(source, target)) {
|
||||
console.log('target file ' + chalk.cyan(target) + ' is up to date, skipping');
|
||||
// return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user