Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
#!/usr/bin/env node
2
3
/*
4
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
5
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
6
*/
7
8
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
13
14
require('./register-plugins');
15
16
function findCommandPosition(args) {
17
var i;
18
19
for (i = 0; i < args.length; i += 1) {
20
if (args[i].charAt(0) !== '-') {
21
return i;
22
}
23
}
24
25
return -1;
26
}
27
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);
36
}, function () {
37
if (ex) {
38
throw ex; // turn it into an uncaught exception
39
} else {
40
exitProcess(code);
41
}
42
});
43
}
44
45
function errHandler (ex) {
46
if (!ex) { return; }
47
if (!ex.inputError) {
48
// exit with exception stack trace
49
exit(ex);
50
} else {
51
//don't print nasty traces but still exit(1)
52
console.error(ex.message);
53
console.error('Try "istanbul help" for usage');
54
exit(null, 1);
55
}
56
}
57
58
function runCommand(args, callback) {
59
var pos = findCommandPosition(args),
60
command,
61
commandArgs,
62
commandObject;
63
64
if (pos < 0) {
65
return callback(inputError.create('Need a command to run'));
66
}
67
68
commandArgs = args.slice(0, pos);
69
command = args[pos];
70
commandArgs.push.apply(commandArgs, args.slice(pos + 1));
71
72
try {
73
commandObject = Command.create(command);
74
} catch (ex) {
75
errHandler(inputError.create(ex.message));
76
return;
77
}
78
commandObject.run(commandArgs, errHandler);
79
}
80
81
function runToCompletion(args) {
82
runCommand(args, errHandler);
83
}
84
85
/* istanbul ignore if: untestable */
86
if (require.main === module) {
87
var args = Array.prototype.slice.call(process.argv, 2);
88
runToCompletion(args);
89
}
90
91
module.exports = {
92
runToCompletion: runToCompletion
93
};
94
95
96