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

chore(typescript): convert cli.js to cli.ts #3151

Merged
merged 1 commit into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 0 additions & 140 deletions lib/cli.js

This file was deleted.

149 changes: 149 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import * as path from 'path';
import * as fs from 'fs';
import * as optimist from 'optimist';

/**
* The command line interface for interacting with the Protractor runner.
* It takes care of parsing command line options.
*
* Values from command line options override values from the config.
*/

var args: Array<string> = [];

process.argv.slice(2).forEach(function(arg: string) {
var flag: string = arg.split('=')[0];

switch (flag) {
case 'debug':
args.push('--nodeDebug');
args.push('true');
break;
case '-d':
case '--debug':
case '--debug-brk':
args.push('--v8Debug');
args.push('true');
break;
default:
args.push(arg);
break;
}
});

optimist
.usage(
'Usage: protractor [configFile] [options]\n' +
'configFile defaults to protractor.conf.js\n' +
'The [options] object will override values from the config file.\n' +
'See the reference config for a full list of options.')
.describe('help', 'Print Protractor help menu')
.describe('version', 'Print Protractor version')
.describe('browser', 'Browsername, e.g. chrome or firefox')
.describe('seleniumAddress', 'A running selenium address to use')
.describe('seleniumSessionId', 'Attaching an existing session id')
.describe(
'seleniumServerJar', 'Location of the standalone selenium jar file')
.describe(
'seleniumPort', 'Optional port for the selenium standalone server')
.describe('baseUrl', 'URL to prepend to all relative paths')
.describe('rootElement', 'Element housing ng-app, if not html or body')
.describe('specs', 'Comma-separated list of files to test')
.describe('exclude', 'Comma-separated list of files to exclude')
.describe('verbose', 'Print full spec names')
.describe('stackTrace', 'Print stack trace on error')
.describe('params', 'Param object to be passed to the tests')
.describe('framework', 'Test framework to use: jasmine, mocha, or custom')
.describe('resultJsonOutputFile', 'Path to save JSON test result')
.describe('troubleshoot', 'Turn on troubleshooting output')
.describe('elementExplorer', 'Interactively test Protractor commands')
.describe(
'debuggerServerPort',
'Start a debugger server at specified port instead of repl')
.alias('browser', 'capabilities.browserName')
.alias('name', 'capabilities.name')
.alias('platform', 'capabilities.platform')
.alias('platform-version', 'capabilities.version')
.alias('tags', 'capabilities.tags')
.alias('build', 'capabilities.build')
.alias('grep', 'jasmineNodeOpts.grep')
.alias('invert-grep', 'jasmineNodeOpts.invertGrep')
.alias('explorer', 'elementExplorer')
.string('capabilities.tunnel-identifier')
.check(function(arg: any) {
if (arg._.length > 1) {
throw 'Error: more than one config file specified';
}
});

var argv: any = optimist.parse(args);

if (argv.help) {
optimist.showHelp();
process.exit(0);
}

if (argv.version) {
console.log(
'Version ' + require(path.join(__dirname, '../package.json')).version);
process.exit(0);
}

// WebDriver capabilities properties require dot notation, but optimist parses
// that into an object. Re-flatten it.
function flattenObject(obj: any, prefix?: string, out?: any): any {
prefix = prefix || '';
out = out || {};
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
typeof obj[prop] === 'object' ?
flattenObject(obj[prop], prefix + prop + '.', out) :
out[prefix + prop] = obj[prop];
}
}
return out;
};

if (argv.capabilities) {
argv.capabilities = flattenObject(argv.capabilities);
}

/**
* Helper to resolve comma separated lists of file pattern strings relative to
* the cwd.
*
* @private
* @param {Array} list
*/
function processFilePatterns_(list: string): Array<string> {
return list.split(',').map(function(spec) {
return path.resolve(process.cwd(), spec);
});
};

if (argv.specs) {
argv.specs = processFilePatterns_(<string>argv.specs);
}
if (argv.exclude) {
argv.exclude = processFilePatterns_(<string>argv.exclude);
}

// Use default configuration, if it exists.
var configFile: string = argv._[0];
if (!configFile) {
if (fs.existsSync('./protractor.conf.js')) {
configFile = './protractor.conf.js';
}
}

if (!configFile && !argv.elementExplorer && args.length < 3) {
console.log(
'**you must either specify a configuration file ' +
'or at least 3 options. See below for the options:\n');
optimist.showHelp();
process.exit(1);
}

// Run the launcher
import * as launcher from './launcher';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move the import to the top of the file.

launcher.init(configFile, argv);
1 change: 1 addition & 0 deletions typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"glob": "registry:dt/glob#5.0.10+20160317120654",
"minimatch": "registry:dt/minimatch#2.0.8+20160317120654",
"node": "registry:dt/node#4.0.0+20160412142033",
"optimist": "registry:dt/optimist#0.0.0+20160316171810",
"q": "registry:dt/q#0.0.0+20160417152954",
"request": "registry:dt/request#0.0.0+20160316155526"
}
Expand Down