4 Copyright (c) 2012, Yahoo! Inc. All rights reserved.
5 Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
9 var async = require('async'),
10 Command = require('./command'),
11 inputError = require('./util/input-error'),
12 exitProcess = process.exit; //hold a reference to original process.exit so that we are not affected even when a test changes it
14 require('./register-plugins');
16 function findCommandPosition(args) {
19 for (i = 0; i < args.length; i += 1) {
20 if (args[i].charAt(0) !== '-') {
28 function exit(ex, code) {
29 // flush output for Node.js Windows pipe bug
30 // https://github.com/joyent/node/issues/6247 is just one bug example
31 // https://github.com/visionmedia/mocha/issues/333 has a good discussion
32 var streams = [process.stdout, process.stderr];
33 async.forEach(streams, function (stream, done) {
34 // submit a write request and wait until it's written
35 stream.write('', done);
38 throw ex; // turn it into an uncaught exception
45 function errHandler (ex) {
48 // exit with exception stack trace
51 //don't print nasty traces but still exit(1)
52 console.error(ex.message);
53 console.error('Try "istanbul help" for usage');
58 function runCommand(args, callback) {
59 var pos = findCommandPosition(args),
65 return callback(inputError.create('Need a command to run'));
68 commandArgs = args.slice(0, pos);
70 commandArgs.push.apply(commandArgs, args.slice(pos + 1));
73 commandObject = Command.create(command);
75 errHandler(inputError.create(ex.message));
78 commandObject.run(commandArgs, errHandler);
81 function runToCompletion(args) {
82 runCommand(args, errHandler);
85 /* istanbul ignore if: untestable */
86 if (require.main === module) {
87 var args = Array.prototype.slice.call(process.argv, 2);
88 runToCompletion(args);
92 runToCompletion: runToCompletion