UNPKG

3.15 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3const _ = require('lodash');
4const path = require('path');
5const api = require('./api');
6const context = require('./context');
7const shell = require('./shell');
8const pkg = require('../package.json');
9const options = require('./options');
10const reactDOMSupport = require('./reactDOMSupport');
11const reactTemplates = require('./reactTemplates');
12const rtStyle = require('./rtStyle');
13
14/**
15 * @param {Options} currentOptions
16 * @return {number}
17 */
18function 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
42function 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 * @param {Options} currentOptions
53 * @param {string} filename file name to process
54 */
55function 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 * Executes the CLI based on an array of arguments that is passed in.
79 * @param {string|Array|Object} args The arguments to process.
80 * @returns {int} The exit code for the operation.
81 */
82function 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
93module.exports = {
94 context,
95 execute,
96 executeOptions,
97 handleSingleFile,
98 convertTemplateToReact: reactTemplates.convertTemplateToReact,
99 convertStyle: rtStyle.convert
100};