1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 | const _ = require('lodash');
|
4 | const path = require('path');
|
5 | const api = require('./api');
|
6 | const context = require('./context');
|
7 | const shell = require('./shell');
|
8 | const pkg = require('../package.json');
|
9 | const options = require('./options');
|
10 | const reactDOMSupport = require('./reactDOMSupport');
|
11 | const reactTemplates = require('./reactTemplates');
|
12 | const rtStyle = require('./rtStyle');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function executeOptions(currentOptions) {
|
19 | let ret = 0;
|
20 | const files = currentOptions._;
|
21 | context.options.format = currentOptions.format || 'stylish';
|
22 |
|
23 | if (currentOptions.version) {
|
24 | console.log(`v${pkg.version}`);
|
25 | } else if (currentOptions.help) {
|
26 | if (files.length) {
|
27 | console.log(options.generateHelpForOption(files[0]));
|
28 | } else {
|
29 | console.log(options.generateHelp());
|
30 | }
|
31 | } else if (currentOptions.listTargetVersion) {
|
32 | printVersions(currentOptions);
|
33 | } else if (files.length) {
|
34 | _.forEach(files, handleSingleFile.bind(this, currentOptions));
|
35 | ret = shell.printResults(context);
|
36 | } else {
|
37 | console.log(options.generateHelp());
|
38 | }
|
39 | return ret;
|
40 | }
|
41 |
|
42 | function printVersions(currentOptions) {
|
43 | const ret = Object.keys(reactDOMSupport);
|
44 | if (currentOptions.format === 'json') {
|
45 | console.log(JSON.stringify(ret, undefined, 2));
|
46 | } else {
|
47 | console.log(ret.join(', '));
|
48 | }
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function handleSingleFile(currentOptions, filename) {
|
56 | try {
|
57 | const sourceExt = path.extname(filename);
|
58 | let outputFilename;
|
59 | if (sourceExt === '.rt') {
|
60 | outputFilename = filename + (currentOptions.modules === 'typescript' ? '.ts' : '.js');
|
61 | } else if (sourceExt === '.jsrt') {
|
62 | outputFilename = filename.replace(/\.jsrt$/, '.js');
|
63 | currentOptions = _.assign({}, currentOptions, {modules: 'jsrt'});
|
64 | } else if (sourceExt === '.rts') {
|
65 | outputFilename = filename + '.js';
|
66 | currentOptions = _.assign({}, currentOptions, {modules: 'rts'});
|
67 | } else {
|
68 | context.error('invalid file, only handle rt/jsrt files', filename);
|
69 | return;
|
70 | }
|
71 | api.convertFile(filename, outputFilename, currentOptions, context);
|
72 | } catch (e) {
|
73 | context.error(e.message, filename, e.line, e.column, e.startOffset, e.endOffset);
|
74 | }
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function execute(args) {
|
83 | let currentOptions;
|
84 | try {
|
85 | currentOptions = options.parse(args);
|
86 | } catch (error) {
|
87 | console.error(error.message);
|
88 | return 1;
|
89 | }
|
90 | return executeOptions(currentOptions);
|
91 | }
|
92 |
|
93 | module.exports = {
|
94 | context,
|
95 | execute,
|
96 | executeOptions,
|
97 | handleSingleFile,
|
98 | convertTemplateToReact: reactTemplates.convertTemplateToReact,
|
99 | convertStyle: rtStyle.convert
|
100 | };
|