1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const chalk = require('chalk');
|
6 | const reactTemplates = require('./reactTemplates');
|
7 | const fsUtil = require('./fsUtil');
|
8 | const convertRT = reactTemplates.convertRT;
|
9 | const convertJSRTToJS = reactTemplates.convertJSRTToJS;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function 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 |
|
49 | module.exports = {
|
50 | convertFile,
|
51 | context: require('./context'),
|
52 | _test: {}
|
53 | };
|