react-templates/src/api.js

55 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict';
var fs = require('fs');
2014-12-28 10:47:34 +01:00
var path = require('path');
var chalk = require('chalk');
var reactTemplates = require('./reactTemplates');
var convertTemplateToReact = reactTemplates.convertTemplateToReact;
2015-04-02 15:00:38 +02:00
var convertJSRTToJS = reactTemplates.convertJSRTToJS;
/**
* @param {string} source
* @param {string} target
* @param {{modules:string, dryRun:boolean}?} options
2014-12-02 13:30:06 +01:00
* @param {CONTEXT} context
*/
2014-12-02 13:30:06 +01:00
function convertFile(source, target, options, context) {
// if (path.extname(filename) !== ".html") {
// console.log('invalid file, only handle html files');
// return;// only handle html files
// }
options = options || {};
options.fileName = source;
var fsUtil = require('./fsUtil');
if (!options.force && !fsUtil.isStale(source, target)) {
2015-04-21 16:42:49 +02:00
context.info('target file ' + chalk.cyan(target) + ' is up to date, skipping', source);
return;
}
var html = fs.readFileSync(source).toString();
2015-02-06 15:52:08 +01:00
var shouldAddName = options.modules === 'none' && !options.name;
if (shouldAddName) {
2014-12-30 09:42:31 +01:00
options.name = reactTemplates.normalizeName(path.basename(source, path.extname(source))) + 'RT';
2014-12-28 10:47:34 +01:00
}
2015-04-02 15:00:38 +02:00
var js;
if (options.modules === 'jsrt') {
2015-04-21 16:21:12 +02:00
js = convertJSRTToJS(html, context, options);
2015-04-02 15:00:38 +02:00
} else {
js = convertTemplateToReact(html, context, options);
2015-04-02 15:00:38 +02:00
}
2014-12-10 13:13:16 +01:00
if (!options.dryRun) {
fs.writeFileSync(target, js);
}
if (shouldAddName) {
2015-02-12 09:08:05 +01:00
delete options.name;
}
}
module.exports = {
// convertTemplateToReact: convertTemplateToReact,
convertFile: convertFile,
2014-12-02 13:30:06 +01:00
context: require('./context'),
_test: {}
};