1
1
'use strict' ;
2
2
3
3
const _ = require ( 'lodash' ) ;
4
- const program = require ( 'commander' ) ;
5
- const Promise = require ( 'bluebird' ) ;
4
+ const { Command} = require ( 'commander' ) ;
6
5
7
- const Config = require ( '../config' ) ;
8
6
const Events = require ( '../constants/events' ) ;
9
7
const Gemini = require ( '../gemini' ) ;
10
8
const 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' ) ;
14
11
15
12
let exitCode ;
16
13
17
14
exports . run = ( ) => {
15
+ const program = new Command ( ) ;
16
+
18
17
program
19
18
. 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
20
25
. option ( '-b, --browser <browser>' , 'run test only in specified browser' , collect )
21
- . option ( '-c, --config <file>' , 'config file' )
22
26
. option ( '--grep <pattern>' , 'run only suites matching the pattern' , RegExp ) ;
23
27
24
28
program . command ( 'update [paths...]' )
25
29
. allowUnknownOption ( true )
26
30
. option ( '--diff' , 'update only screenshots with diff' )
27
31
. option ( '--new' , 'save only new screenshots' )
28
32
. 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 ( ) ) ;
30
34
31
35
program . command ( 'test [paths...]' )
32
36
. allowUnknownOption ( true )
@@ -39,7 +43,11 @@ exports.run = () => {
39
43
console . log ( ' vflat verbose console reporter' ) ;
40
44
console . log ( '' ) ;
41
45
} )
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 ) ) ;
43
51
44
52
program . on ( '--help' , ( ) => {
45
53
console . log ( '' ) ;
@@ -66,58 +74,59 @@ exports.run = () => {
66
74
console . log ( '' ) ;
67
75
} ) ;
68
76
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 ) ;
71
78
72
79
program . parse ( process . argv ) ;
73
80
} ;
74
81
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' ) ;
77
87
78
- console . log ( config [ option ] || `Cannot list option ${ option } . Available options are: sets, browsers` ) ;
88
+ configFileParser . parse ( process . argv ) ;
89
+ return configFileParser [ option ] ;
79
90
}
80
91
81
- function parseConfig ( configPath ) {
82
- const config = new Config ( configPath ) ;
92
+ function mkGemini ( configPath ) {
93
+ checkForDeprecations ( ) ;
83
94
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 ) ;
89
97
90
- function collect ( newValue , array ) {
91
- array = array || [ ] ;
92
- return array . concat ( newValue ) ;
98
+ return gemini ;
93
99
}
94
100
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
117
112
} )
118
113
. then ( ( stats ) => stats . failed > 0 ? 2 : 0 )
119
114
. catch ( handleErrors )
120
115
. 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 ) ;
121
130
}
122
131
123
132
function parseReporterOptions ( options ) {
0 commit comments