forked from jamesshore/lets_code_javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
151 lines (122 loc) · 4.62 KB
/
cli.js
File metadata and controls
151 lines (122 loc) · 4.62 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var optimist = require('optimist');
var util = require('./util');
var constant = require('./constants');
var path = require('path');
var processArgs = function(argv, options) {
if (argv.help) {
console.log(optimist.help());
process.exit(0);
}
if (argv.version) {
console.log('Testacular version: ' + constant.VERSION);
process.exit(0);
}
// TODO(vojta): warn/throw when unknown argument (probably mispelled)
Object.getOwnPropertyNames(argv).forEach(function(name) {
if (name !== '_' && name !== '$0') {
options[util.dashToCamel(name)] = argv[name];
}
});
if (util.isString(options.autoWatch)) {
options.autoWatch = options.autoWatch === 'true';
}
if (util.isString(options.colors)) {
options.colors = options.colors === 'true';
}
if (util.isString(options.logLevel)) {
options.logLevel = constant['LOG_' + options.logLevel.toUpperCase()] || constant.LOG_DISABLE;
}
if (util.isString(options.singleRun)) {
options.singleRun = options.singleRun === 'true';
}
if (util.isString(options.browsers)) {
options.browsers = options.browsers.split(',');
}
options.configFile = path.resolve(argv._.shift() || 'testacular.conf.js');
return options;
};
var describeShared = function() {
optimist
.usage('Testacular - Spectacular Test Runner for JavaScript.\n\n' +
'Usage:\n' +
' $0 <command>\n\n' +
'Commands:\n' +
' start [<configFile>] [<options>] Start the server / do single run.\n' +
' init [<configFile>] Initialize a config file.\n' +
' run [<options>] Trigger a test run.\n\n' +
'Run --help with particular command to see its description and available options.')
.describe('help', 'Print usage and options.')
.describe('version', 'Print current version.');
};
var describeInit = function() {
optimist
.usage('Testacular - Spectacular Test Runner for JavaScript.\n\n' +
'INIT - Initialize a config file.\n\n' +
'Usage:\n' +
' $0 init [<configFile>]')
.describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
.describe('colors', 'Use colors when reporting and printing logs.')
.describe('no-colors', 'Do not use colors when reporting or printing logs.')
.describe('help', 'Print usage and options.')
.describe('version', 'Print current version.');
};
var describeStart = function() {
optimist
.usage('Testacular - Spectacular Test Runner for JavaScript.\n\n' +
'START - Start the server / do a single run.\n\n' +
'Usage:\n' +
' $0 start [<configFile>] [<options>]')
.describe('port', '<integer> Port where the web server is running.')
.describe('port-runner', '<integer> Port where the server is listening for runner.')
.describe('auto-watch', 'Auto watch source files and run on change.')
.describe('no-auto-watch', 'Do not watch source files.')
.describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
.describe('colors', 'Use colors when reporting and printing logs.')
.describe('no-colors', 'Do not use colors when reporting or printing logs.')
.describe('reporter', '<progress | dots> How the results are reported.')
.describe('browsers', 'List of browsers to start (eg. --browsers Chrome,ChromeCanary,Firefox).')
.describe('single-run', 'Run the test when browsers captured and exit.')
.describe('no-single-run', 'Disable single-run.')
.describe('help', 'Print usage and options.')
.describe('version', 'Print current version.');
};
var describeRun = function() {
optimist
.usage('Testacular - Spectacular Test Runner for JavaScript.\n\n' +
'RUN - Run the tests (requires running server).\n\n' +
'Usage:\n' +
' $0 run [<options>]')
.describe('port-runner', '<integer> Port where the server is listening for runner.')
.describe('help', 'Print usage.')
.describe('version', 'Print current version.');
};
exports.process = function() {
var argv = optimist.argv;
var options = {
cmd: argv._.shift()
};
switch (options.cmd) {
case 'start':
describeStart();
break;
case 'run':
describeRun();
break;
case 'init':
describeInit();
break;
default:
describeShared();
if (!options.cmd) {
processArgs(argv, options);
console.error('Command not specified.');
} else {
console.error('Unknown command "' + options.cmd + '".');
}
optimist.showHelp();
process.exit(1);
}
return processArgs(argv, options);
};
// just for testing
exports.processArgs = processArgs;