11'use strict' ;
22
33const _ = require ( 'lodash' ) ;
4- const program = require ( 'commander' ) ;
5- const Promise = require ( 'bluebird' ) ;
4+ const { Command} = require ( 'commander' ) ;
65
7- const Config = require ( '../config' ) ;
86const Events = require ( '../constants/events' ) ;
97const Gemini = require ( '../gemini' ) ;
108const pkg = require ( '../../package.json' ) ;
11- const handleErrors = require ( './errors' ) . handleErrors ;
12- const checkForDeprecations = require ( './deprecations' ) . checkForDeprecations ;
13- const handleUncaughtExceptions = require ( './errors' ) . handleUncaughtExceptions ;
9+ const { checkForDeprecations} = require ( './deprecations' ) ;
10+ const { handleErrors, handleUncaughtExceptions} = require ( './errors' ) ;
1411
1512let exitCode ;
1613
1714exports . run = ( ) => {
15+ const program = new Command ( ) ;
16+
1817 program
1918 . version ( pkg . version )
19+ . option ( '-c, --config <file>' , 'config file' ) ;
20+
21+ const configPath = preparseOption ( program , 'config' ) ;
22+ const gemini = mkGemini ( configPath ) ;
23+
24+ program
2025 . option ( '-b, --browser <browser>' , 'run test only in specified browser' , collect )
21- . option ( '-c, --config <file>' , 'config file' )
2226 . option ( '--grep <pattern>' , 'run only suites matching the pattern' , RegExp ) ;
2327
2428 program . command ( 'update [paths...]' )
2529 . allowUnknownOption ( true )
2630 . option ( '--diff' , 'update only screenshots with diff' )
2731 . option ( '--new' , 'save only new screenshots' )
2832 . description ( 'update the changed screenshots or gather if they doesn\'t exist' )
29- . action ( ( paths , options ) => runGemini ( 'update' , paths , options ) . done ( ) ) ;
33+ . action ( ( paths , options ) => mkRunFn ( gemini , 'update' , program ) ( paths , options ) . done ( ) ) ;
3034
3135 program . command ( 'test [paths...]' )
3236 . allowUnknownOption ( true )
@@ -39,7 +43,11 @@ exports.run = () => {
3943 console . log ( ' vflat verbose console reporter' ) ;
4044 console . log ( '' ) ;
4145 } )
42- . action ( ( paths , options ) => runGemini ( 'test' , paths , options ) . done ( ) ) ;
46+ . action ( ( paths , options ) => mkRunFn ( gemini , 'test' , program ) ( paths , options ) . done ( ) ) ;
47+
48+ program . command ( 'list <key>' )
49+ . description ( `Use 'list browsers' or 'list sets' to get all available browsers or sets.` )
50+ . action ( ( key ) => logOptionFromConfig ( key , gemini ) ) ;
4351
4452 program . on ( '--help' , ( ) => {
4553 console . log ( '' ) ;
@@ -66,58 +74,59 @@ exports.run = () => {
6674 console . log ( '' ) ;
6775 } ) ;
6876
69- program . option ( 'list' , 'Use \'list browsers\' or \'list sets\' to get all available browsers or sets.' )
70- . action ( ( option ) => logOptionFromConfig ( option ) ) ;
77+ gemini . extendCli ( program ) ;
7178
7279 program . parse ( process . argv ) ;
7380} ;
7481
75- function logOptionFromConfig ( option ) {
76- const config = parseConfig ( program . config ) ;
82+ function preparseOption ( program , option ) {
83+ // do not display any help, do not exit
84+ const configFileParser = Object . create ( program ) ;
85+ configFileParser . options = [ ] . concat ( program . options ) ;
86+ configFileParser . option ( '-h, --help' ) ;
7787
78- console . log ( config [ option ] || `Cannot list option ${ option } . Available options are: sets, browsers` ) ;
88+ configFileParser . parse ( process . argv ) ;
89+ return configFileParser [ option ] ;
7990}
8091
81- function parseConfig ( configPath ) {
82- const config = new Config ( configPath ) ;
92+ function mkGemini ( configPath ) {
93+ checkForDeprecations ( ) ;
8394
84- return {
85- sets : _ . keys ( config . sets ) . join ( ', ' ) ,
86- browsers : config . getBrowserIds ( ) . join ( ', ' )
87- } ;
88- }
95+ const gemini = new Gemini ( configPath , { cli : true , env : true } ) ;
96+ gemini . on ( Events . INTERRUPT , ( data ) => exitCode = data . exitCode ) ;
8997
90- function collect ( newValue , array ) {
91- array = array || [ ] ;
92- return array . concat ( newValue ) ;
98+ return gemini ;
9399}
94100
95- function runGemini ( method , paths , options ) {
96- options = options || { } ;
97-
98- handleUncaughtExceptions ( ) ;
99-
100- return Promise . try ( ( ) => {
101- checkForDeprecations ( ) ;
102-
103- const gemini = new Gemini ( program . config , { cli : true , env : true } ) ;
104- gemini . on ( Events . INTERRUPT , ( data ) => exitCode = data . exitCode ) ;
105-
106- return gemini ;
107- } )
108- . then ( ( gemini ) => {
109- return gemini [ method ] ( paths , {
110- sets : options . set ,
111- reporters : parseReporterOptions ( options ) ,
112- grep : program . grep ,
113- browsers : program . browser ,
114- diff : options . diff ,
115- new : options . new
116- } ) ;
101+ function mkRunFn ( gemini , method , globalOpts ) {
102+ return ( paths , opts = { } ) => {
103+ handleUncaughtExceptions ( ) ;
104+
105+ return gemini [ method ] ( paths , {
106+ sets : opts . set ,
107+ reporters : parseReporterOptions ( opts ) ,
108+ grep : globalOpts . grep ,
109+ browsers : globalOpts . browser ,
110+ diff : opts . diff ,
111+ new : opts . new
117112 } )
118113 . then ( ( stats ) => stats . failed > 0 ? 2 : 0 )
119114 . catch ( handleErrors )
120115 . then ( exit ) ;
116+ } ;
117+ }
118+
119+ function logOptionFromConfig ( key , { config} ) {
120+ const data = {
121+ sets : _ . keys ( config . sets ) . join ( ', ' ) ,
122+ browsers : config . getBrowserIds ( ) . join ( ', ' )
123+ } ;
124+
125+ console . log ( data [ key ] || `Cannot list option ${ key } . Available options are: sets, browsers` ) ;
126+ }
127+
128+ function collect ( newValue , array = [ ] ) {
129+ return array . concat ( newValue ) ;
121130}
122131
123132function parseReporterOptions ( options ) {
0 commit comments