2014-11-27 17:21:46 +01:00
|
|
|
'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
|
2014-11-27 17:21:46 +01:00
|
|
|
* @param {string} target
|
2014-12-02 13:30:06 +01:00
|
|
|
* @param {CONTEXT} context
|
2014-11-27 17:21:46 +01:00
|
|
|
*/
|
2014-12-02 13:30:06 +01:00
|
|
|
function convertFile(source, target, options, context) {
|
2014-11-27 17:21:46 +01:00
|
|
|
// 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');
|
2014-11-27 17:21:46 +01:00
|
|
|
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);
|
|
|
|
}
|
2014-11-27 17:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
// convertTemplateToReact: convertTemplateToReact,
|
|
|
|
convertFile: convertFile,
|
2014-12-02 13:30:06 +01:00
|
|
|
context: require('./context'),
|
2014-11-27 17:21:46 +01:00
|
|
|
_test: {}
|
|
|
|
};
|