UNPKG

3.2 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5var _ = require('lodash');
6var path = require('path');
7var api = require('./api');
8var context = require('./context');
9var shell = require('./shell');
10var pkg = require('../package.json');
11var options = require('./options');
12var reactDOMSupport = require('./reactDOMSupport');
13var reactTemplates = require('./reactTemplates');
14var rtStyle = require('./rtStyle');
15
16/**
17 * @param {Options} currentOptions
18 * @return {number}
19 */
20function 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
44function 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 * @param {Options} currentOptions
55 * @param {string} filename file name to process
56 */
57function 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 * Executes the CLI based on an array of arguments that is passed in.
81 * @param {string|Array|Object} args The arguments to process.
82 * @returns {int} The exit code for the operation.
83 */
84function 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
95module.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