2014-11-27 17:21:46 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var fs = require('fs');
|
2014-12-28 10:47:34 +01:00
|
|
|
var path = require('path');
|
2014-11-27 17:21:46 +01:00
|
|
|
var chalk = require('chalk');
|
|
|
|
var reactTemplates = require('./reactTemplates');
|
|
|
|
var convertTemplateToReact = reactTemplates.convertTemplateToReact;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} source
|
2014-12-28 10:47:34 +01:00
|
|
|
* @param {{modules:string, 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();
|
2015-02-06 15:52:08 +01:00
|
|
|
var shouldAddName = options.modules === 'none' && !options.name;
|
2015-02-06 15:16:01 +01:00
|
|
|
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
|
|
|
}
|
2014-11-27 17:21:46 +01:00
|
|
|
var js = convertTemplateToReact(html, options);
|
2014-12-10 13:13:16 +01:00
|
|
|
if (!options.dryRun) {
|
|
|
|
fs.writeFileSync(target, js);
|
|
|
|
}
|
2015-02-06 15:16:01 +01:00
|
|
|
if (shouldAddName) {
|
2015-02-06 15:52:08 +01:00
|
|
|
delete options.name;
|
2015-02-06 15:16:01 +01:00
|
|
|
}
|
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: {}
|
2014-12-11 08:30:27 +01:00
|
|
|
};
|