react-templates/src/api.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
var fs = require('fs');
var chalk = require('chalk');
var reactTemplates = require('./reactTemplates');
var convertTemplateToReact = reactTemplates.convertTemplateToReact;
/**
* @param {string} source
2014-12-10 13:13:16 +01:00
* @param {{commonJS:boolean, dryRun:boolean}?} options
* @param {string} target
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 || {};
var fsUtil = require('./fsUtil');
if (!options.force && !fsUtil.isStale(source, target)) {
2014-12-02 13:30:06 +01:00
context.info('target file ' + chalk.cyan(target) + ' is up to date, skipping');
return;
}
var html = fs.readFileSync(source).toString();
if (!html.match(/\<\!doctype rt/i)) {
throw new Error('invalid file, missing header');
}
var js = convertTemplateToReact(html, options);
2014-12-10 13:13:16 +01:00
if (!options.dryRun) {
fs.writeFileSync(target, js);
}
}
module.exports = {
// convertTemplateToReact: convertTemplateToReact,
convertFile: convertFile,
2014-12-02 13:30:06 +01:00
context: require('./context'),
_test: {}
};