react-templates/src/api.js

48 lines
1.4 KiB
JavaScript

'use strict';
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var reactTemplates = require('./reactTemplates');
var convertTemplateToReact = reactTemplates.convertTemplateToReact;
/**
* @param {string} source
* @param {{modules:string, dryRun:boolean}?} options
* @param {string} target
* @param {CONTEXT} context
*/
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 || {};
var fsUtil = require('./fsUtil');
if (!options.force && !fsUtil.isStale(source, target)) {
context.info('target file ' + chalk.cyan(target) + ' is up to date, skipping');
return;
}
var html = fs.readFileSync(source).toString();
var shouldAddName = options.modules === 'none' && !options.name
if (shouldAddName) {
options.name = reactTemplates.normalizeName(path.basename(source, path.extname(source))) + 'RT';
}
var js = convertTemplateToReact(html, options);
if (!options.dryRun) {
fs.writeFileSync(target, js);
}
if (shouldAddName) {
delete options.name
}
}
module.exports = {
// convertTemplateToReact: convertTemplateToReact,
convertFile: convertFile,
context: require('./context'),
_test: {}
};