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