UNPKG

1.61 kBJavaScriptView Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5const chalk = require('chalk');
6const reactTemplates = require('./reactTemplates');
7const fsUtil = require('./fsUtil');
8const convertRT = reactTemplates.convertRT;
9const convertJSRTToJS = reactTemplates.convertJSRTToJS;
10
11/**
12 * @param {string} source
13 * @param {string} target
14 * @param {Options} options
15 * @param {CONTEXT} context
16 */
17function convertFile(source, target, options, context) {
18 options = options || {};
19 options.fileName = source;
20
21 if (!options.force && !fsUtil.isStale(source, target)) {
22 context.verbose(`target file ${chalk.cyan(target)} is up to date, skipping`);
23 return;
24 }
25
26 const html = fs.readFileSync(source).toString();
27 if (path.extname(source) === '.rts') {
28 const rtStyle = require('./rtStyle');
29 const out = rtStyle.convert(html);
30 if (!options.dryRun) {
31 fs.writeFileSync(target, out);
32 }
33 return;
34 }
35 const shouldAddName = options.modules === 'none' && !options.name;
36 if (shouldAddName) {
37 options.name = reactTemplates.normalizeName(path.basename(source, path.extname(source))) + 'RT';
38 }
39 options.readFileSync = fsUtil.createRelativeReadFileSync(source);
40 const js = options.modules === 'jsrt' ? convertJSRTToJS(html, context, options) : convertRT(html, context, options);
41 if (!options.dryRun) {
42 fs.writeFileSync(target, js);
43 }
44 if (shouldAddName) {
45 delete options.name;
46 }
47}
48
49module.exports = {
50 convertFile,
51 context: require('./context'),
52 _test: {}
53};