Code coverage report for istanbul/lib/cli.js

Statements: 100% (33 / 33)      Branches: 100% (10 / 10)      Functions: 100% (4 / 4)      Lines: 100% (32 / 32)      Ignored: 2 statements, 1 branch     

All files » istanbul/lib/ » 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                88       88   88 88   88 88 86       2     88 36 14 6     8 8 8       88 88         88 2     86 86 86   86 86   1   85     88 88       88 1 1     88        
#!/usr/bin/env node
 
/*
 Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
 */
 
 
var Command = require('./command'),
    inputError = require('./util/input-error'),
    exit = process.exit; //hold a reference to original process.exit so that we are not affected even when a test changes it
 
require('./register-plugins');
 
function findCommandPosition(args) {
    var i;
 
    for (i = 0; i < args.length; i += 1) {
        if (args[i].charAt(0) !== '-') {
            return i;
        }
    }
 
    return -1;
}
 
function errHandler (ex) {
    if (!ex) { return; }
    if (!ex.inputError) {
        throw ex; // turn it into an uncaught exception
    } else {
        //don't print nasty traces but still exit(1)
        console.error(ex.message);
        console.error('Try "istanbul help" for usage');
        exit(1);
    }
}
 
function runCommand(args, callback) {
    var pos = findCommandPosition(args),
        command,
        commandArgs,
        commandObject;
 
    if (pos < 0) {
        return callback(inputError.create('Need a command to run'));
    }
 
    commandArgs = args.slice(0, pos);
    command = args[pos];
    commandArgs.push.apply(commandArgs, args.slice(pos + 1));
 
    try {
        commandObject = Command.create(command);
    } catch (ex) {
        errHandler(inputError.create(ex.message));
    }
    commandObject.run(commandArgs, errHandler);
}
 
function runToCompletion(args) {
    runCommand(args, errHandler);
}
 
/* istanbul ignore if: untestable */
Iif (require.main === module) {
    var args = Array.prototype.slice.call(process.argv, 2);
    runToCompletion(args);
}
 
module.exports = {
    runToCompletion: runToCompletion
};