Code coverage report for src/cli.js

Statements: 69.57% (32 / 46)      Branches: 40% (8 / 20)      Functions: 75% (3 / 4)      Lines: 69.57% (32 / 46)      Ignored: none     

All files » src/ » cli.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104            1 1 1 1 1 1   1 1 1   1 1 1 1   1   1           1   1     1 1   1     1                         1 6                     6 6   5                                 1 1 1 1           1     1          
#!/usr/bin/env node
/**
 * Created by idok on 11/10/14.
 */
'use strict';
//var fs = require('fs');
var _ = require('lodash');
var path = require('path');
var api = require('./api');
var context = require('./context');
var shell = require('./shell');
var pkg = require('../package.json');
//var defaultOptions = {commonJS: false, force: false, json: false};
var options = require('./options');
var reactDOMSupport = require('./reactDOMSupport');
var reactTemplates = require('./reactTemplates');
 
function executeOptions(currentOptions) {
    var ret = 0;
    var files = currentOptions._;
    context.options.format = currentOptions.format || 'stylish';
 
    Iif (currentOptions.version) {
        console.log('v' + pkg.version);
    } else Iif (currentOptions.help) {
        if  (files.length) {
            console.log(options.generateHelpForOption(files[0]));
        } else {
            console.log(options.generateHelp());
        }
    } else Iif (currentOptions.listTargetVersion) {
        printVersions(currentOptions);
    } else Iif (!files.length) {
        console.log(options.generateHelp());
    } else {
        _.forEach(files, handleSingleFile.bind(this, currentOptions));
        ret = shell.printResults(context);
    }
    return ret;
}
 
function printVersions(currentOptions) {
    var ret = Object.keys(reactDOMSupport);
    if (currentOptions.format === 'json') {
        console.log(JSON.stringify(ret, undefined, 2));
    } else {
        console.log(ret.join(', '));
    }
}
 
/**
 * @param {*} currentOptions
 * @param {string} filename file name to process
 */
function handleSingleFile(currentOptions, filename) {
    Iif (path.extname(filename) !== '.rt') {
        context.error('invalid file, only handle rt files', filename);
        return;// only handle html files
    }
//    var html = fs.readFileSync(filename).toString();
//    if (!html.match(/\<\!doctype jsx/)) {
//        console.log('invalid file, missing header');
//        return;
//    }
//    var js = reactTemplates.convertTemplateToReact(html);
//    fs.writeFileSync(filename + '.js', js);
    try {
        api.convertFile(filename, filename + '.js', currentOptions, context);
    } catch (e) {
        context.error(e.message, filename, e.line || -1, -1, e.index || -1);
//        if (defaultOptions.json) {
//            context.error(e.message, filename, e.line || -1, -1, e.index || -1);
//            console.log(JSON.stringify(context.getMessages(), undefined, 2));
//        } else {
//            console.log('Error processing file: ' + filename + ', ' + e.message + ' line: ' + e.line || -1);
//        }
        // if (defaultOptions.stack)
        // console.log(e.stack);
    }
}
 
/**
 * Executes the CLI based on an array of arguments that is passed in.
 * @param {string|Array|Object} args The arguments to process.
 * @returns {int} The exit code for the operation.
 */
function execute(args) {
    var currentOptions;
    try {
        currentOptions = options.parse(args);
    } catch (error) {
        console.error(error.message);
        return 1;
    }
    //console.log(currentOptions);
    return executeOptions(currentOptions);
}
 
module.exports = {
    execute: execute,
    executeOptions: executeOptions,
    handleSingleFile: handleSingleFile,
    convertTemplateToReact: reactTemplates.convertTemplateToReact
};