Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit cf2d0fb

Browse files
committed
chore(typescript): convert cli.js to cli.ts (#3151)
1 parent 47f54ad commit cf2d0fb

File tree

3 files changed

+150
-140
lines changed

3 files changed

+150
-140
lines changed

lib/cli.js

Lines changed: 0 additions & 140 deletions
This file was deleted.

lib/cli.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import * as path from 'path';
2+
import * as fs from 'fs';
3+
import * as optimist from 'optimist';
4+
5+
/**
6+
* The command line interface for interacting with the Protractor runner.
7+
* It takes care of parsing command line options.
8+
*
9+
* Values from command line options override values from the config.
10+
*/
11+
12+
var args: Array<string> = [];
13+
14+
process.argv.slice(2).forEach(function(arg: string) {
15+
var flag: string = arg.split('=')[0];
16+
17+
switch (flag) {
18+
case 'debug':
19+
args.push('--nodeDebug');
20+
args.push('true');
21+
break;
22+
case '-d':
23+
case '--debug':
24+
case '--debug-brk':
25+
args.push('--v8Debug');
26+
args.push('true');
27+
break;
28+
default:
29+
args.push(arg);
30+
break;
31+
}
32+
});
33+
34+
optimist
35+
.usage(
36+
'Usage: protractor [configFile] [options]\n' +
37+
'configFile defaults to protractor.conf.js\n' +
38+
'The [options] object will override values from the config file.\n' +
39+
'See the reference config for a full list of options.')
40+
.describe('help', 'Print Protractor help menu')
41+
.describe('version', 'Print Protractor version')
42+
.describe('browser', 'Browsername, e.g. chrome or firefox')
43+
.describe('seleniumAddress', 'A running selenium address to use')
44+
.describe('seleniumSessionId', 'Attaching an existing session id')
45+
.describe(
46+
'seleniumServerJar', 'Location of the standalone selenium jar file')
47+
.describe(
48+
'seleniumPort', 'Optional port for the selenium standalone server')
49+
.describe('baseUrl', 'URL to prepend to all relative paths')
50+
.describe('rootElement', 'Element housing ng-app, if not html or body')
51+
.describe('specs', 'Comma-separated list of files to test')
52+
.describe('exclude', 'Comma-separated list of files to exclude')
53+
.describe('verbose', 'Print full spec names')
54+
.describe('stackTrace', 'Print stack trace on error')
55+
.describe('params', 'Param object to be passed to the tests')
56+
.describe('framework', 'Test framework to use: jasmine, mocha, or custom')
57+
.describe('resultJsonOutputFile', 'Path to save JSON test result')
58+
.describe('troubleshoot', 'Turn on troubleshooting output')
59+
.describe('elementExplorer', 'Interactively test Protractor commands')
60+
.describe(
61+
'debuggerServerPort',
62+
'Start a debugger server at specified port instead of repl')
63+
.alias('browser', 'capabilities.browserName')
64+
.alias('name', 'capabilities.name')
65+
.alias('platform', 'capabilities.platform')
66+
.alias('platform-version', 'capabilities.version')
67+
.alias('tags', 'capabilities.tags')
68+
.alias('build', 'capabilities.build')
69+
.alias('grep', 'jasmineNodeOpts.grep')
70+
.alias('invert-grep', 'jasmineNodeOpts.invertGrep')
71+
.alias('explorer', 'elementExplorer')
72+
.string('capabilities.tunnel-identifier')
73+
.check(function(arg: any) {
74+
if (arg._.length > 1) {
75+
throw 'Error: more than one config file specified';
76+
}
77+
});
78+
79+
var argv: any = optimist.parse(args);
80+
81+
if (argv.help) {
82+
optimist.showHelp();
83+
process.exit(0);
84+
}
85+
86+
if (argv.version) {
87+
console.log(
88+
'Version ' + require(path.join(__dirname, '../package.json')).version);
89+
process.exit(0);
90+
}
91+
92+
// WebDriver capabilities properties require dot notation, but optimist parses
93+
// that into an object. Re-flatten it.
94+
function flattenObject(obj: any, prefix?: string, out?: any): any {
95+
prefix = prefix || '';
96+
out = out || {};
97+
for (var prop in obj) {
98+
if (obj.hasOwnProperty(prop)) {
99+
typeof obj[prop] === 'object' ?
100+
flattenObject(obj[prop], prefix + prop + '.', out) :
101+
out[prefix + prop] = obj[prop];
102+
}
103+
}
104+
return out;
105+
};
106+
107+
if (argv.capabilities) {
108+
argv.capabilities = flattenObject(argv.capabilities);
109+
}
110+
111+
/**
112+
* Helper to resolve comma separated lists of file pattern strings relative to
113+
* the cwd.
114+
*
115+
* @private
116+
* @param {Array} list
117+
*/
118+
function processFilePatterns_(list: string): Array<string> {
119+
return list.split(',').map(function(spec) {
120+
return path.resolve(process.cwd(), spec);
121+
});
122+
};
123+
124+
if (argv.specs) {
125+
argv.specs = processFilePatterns_(<string>argv.specs);
126+
}
127+
if (argv.exclude) {
128+
argv.exclude = processFilePatterns_(<string>argv.exclude);
129+
}
130+
131+
// Use default configuration, if it exists.
132+
var configFile: string = argv._[0];
133+
if (!configFile) {
134+
if (fs.existsSync('./protractor.conf.js')) {
135+
configFile = './protractor.conf.js';
136+
}
137+
}
138+
139+
if (!configFile && !argv.elementExplorer && args.length < 3) {
140+
console.log(
141+
'**you must either specify a configuration file ' +
142+
'or at least 3 options. See below for the options:\n');
143+
optimist.showHelp();
144+
process.exit(1);
145+
}
146+
147+
// Run the launcher
148+
import * as launcher from './launcher';
149+
launcher.init(configFile, argv);

typings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"glob": "registry:dt/glob#5.0.10+20160317120654",
66
"minimatch": "registry:dt/minimatch#2.0.8+20160317120654",
77
"node": "registry:dt/node#4.0.0+20160412142033",
8+
"optimist": "registry:dt/optimist#0.0.0+20160316171810",
89
"q": "registry:dt/q#0.0.0+20160417152954",
910
"request": "registry:dt/request#0.0.0+20160316155526"
1011
}

0 commit comments

Comments
 (0)