11#! /usr/bin/env node
22
3- var todo = process . argv [ 2 ] ,
4- path = require ( 'path' ) ,
5- config ;
3+ var yargs = require ( 'yargs' )
4+ . command ( 'init [preset] [path]' , 'initialise browserstack.json with preset and test runner path' , function ( yargs ) {
5+ return yargs . option ( 'preset' , {
6+ type : 'string' ,
7+ default : 'default' ,
8+ description : 'name of preset json file(without extension)(present in node_modules/browserstack-runner/presets to be used while initiating'
9+ } )
10+ . option ( 'path' , {
11+ type : 'string' ,
12+ default : '/path/to/test/runner' ,
13+ description : 'path to test runner to be inserted in browserstack.json'
14+ } )
15+ } )
16+ . option ( 'browsers' , {
17+ alias : 'b' ,
18+ type : 'array' ,
19+ description : 'list of space separatedbrowsers keys as described in json file'
20+ } )
21+ . option ( 'path' , {
22+ type : 'string' ,
23+ description : 'path to test file'
24+ } )
25+ . option ( 'version' , {
26+ alias : 'V' ,
27+ description : 'browserstack-runner version'
28+ } )
29+ . option ( 'pid' , {
30+ type : 'string' ,
31+ description : 'path to pid file'
32+ } )
33+ . option ( 'verbose' , {
34+ alias : 'v' ,
35+ description : 'verbose logging'
36+ } ) . argv ;
637
7- if ( process . argv . indexOf ( '-- verbose') !== - 1 ) {
38+ if ( yargs [ ' verbose'] ) {
839 global . logLevel = process . env . LOG_LEVEL || 'debug' ;
940} else {
1041 global . logLevel = 'info' ;
1142}
43+ var path = require ( 'path' ) ,
44+ config ;
1245
13- if ( todo === 'init' ) {
46+ if ( yargs [ '_' ] . indexOf ( 'init' ) !== - 1 ) {
47+ module . exports . preset = yargs [ 'preset' ] ;
48+ module . exports . path = yargs [ 'path' ] ;
1449 require ( './init.js' ) ;
1550 return ;
16- } else if ( todo === '--version' ) {
17- require ( './version.js' ) ;
18- return ;
1951}
2052
2153var config_path = process . env . BROWSERSTACK_JSON || 'browserstack.json' ;
@@ -37,17 +69,28 @@ try {
3769}
3870
3971// extract a path to file to store tunnel pid
40- var pid = process . argv . find ( function ( param ) { return param . indexOf ( '--pid' ) !== - 1 ; } ) ;
41-
42- if ( pid ) {
43- var extracted_path = / - - p i d = ( [ \w \/ \. - ] + ) / g. exec ( pid ) ;
44- if ( extracted_path ) {
45- config . tunnel_pid_file = extracted_path [ 1 ] ;
72+ if ( yargs [ 'pid' ] ) {
73+ if ( yargs [ 'pid' ] . length > 0 ) {
74+ config . tunnel_pid_file = yargs [ 'pid' ] ;
4675 } else {
4776 console . error ( 'Error while parsing flag --pid. Usage: --pid=/path/to/file' ) ;
4877 }
4978}
5079
80+ // filter browsers according to from command line arguments
81+ if ( yargs [ 'browsers' ] ) {
82+ if ( yargs [ 'browsers' ] . length > 0 ) {
83+ config . browsers = config . browsers . filter ( function ( browser ) {
84+ return yargs [ 'browsers' ] . indexOf ( browser [ 'cli_key' ] ) !== - 1 ;
85+ } ) ;
86+ } else {
87+ console . error ( 'No browser keys specified. Usage --browsers <key1> <key2> ...' ) ;
88+ }
89+ }
90+
91+ // test file path from cli arguments
92+ config . test_path = yargs [ 'path' ] || config . test_path ;
93+
5194var runner = require ( './cli.js' ) ;
5295runner . run ( config , function ( err ) {
5396 if ( err ) {
0 commit comments