1
1
#!/usr/bin/env node
2
- const program = require ( 'commander' )
3
- const Codecept = require ( '../lib/codecept' )
4
- const { print, error } = require ( '../lib/output' )
5
- const { printError } = require ( '../lib/command/utils' )
2
+ import { Command } from 'commander'
3
+ const program = new Command ( )
4
+ import Codecept from '../lib/codecept.js'
5
+ import output from '../lib/output.js'
6
+ const { print, error } = output
7
+ import commandUtils from '../lib/command/utils.js'
8
+ const { printError } = commandUtils
6
9
7
10
const commandFlags = {
8
11
ai : {
@@ -42,6 +45,23 @@ const errorHandler =
42
45
}
43
46
}
44
47
48
+ const dynamicImport = async modulePath => {
49
+ const module = await import ( modulePath )
50
+ return module . default || module
51
+ }
52
+
53
+ const commandHandler = modulePath =>
54
+ errorHandler ( async ( ...args ) => {
55
+ const handler = await dynamicImport ( modulePath )
56
+ return handler ( ...args )
57
+ } )
58
+
59
+ const commandHandlerWithProperty = ( modulePath , property ) =>
60
+ errorHandler ( async ( ...args ) => {
61
+ const module = await dynamicImport ( modulePath )
62
+ return module [ property ] ( ...args )
63
+ } )
64
+
45
65
if ( process . versions . node && process . versions . node . split ( '.' ) && process . versions . node . split ( '.' ) [ 0 ] < 12 ) {
46
66
error ( 'NodeJS >= 12 is required to run.' )
47
67
print ( )
@@ -53,22 +73,16 @@ if (process.versions.node && process.versions.node.split('.') && process.version
53
73
program . usage ( '<command> [options]' )
54
74
program . version ( Codecept . version ( ) )
55
75
56
- program
57
- . command ( 'init [path]' )
58
- . description ( 'Creates dummy config in current dir or [path]' )
59
- . action ( errorHandler ( require ( '../lib/command/init' ) ) )
76
+ program . command ( 'init [path]' ) . description ( 'Creates dummy config in current dir or [path]' ) . action ( commandHandler ( '../lib/command/init.js' ) )
60
77
61
78
program
62
79
. command ( 'check' )
63
80
. option ( commandFlags . config . flag , commandFlags . config . description )
64
81
. description ( 'Checks configuration and environment before running tests' )
65
82
. option ( '-t, --timeout [ms]' , 'timeout for checks in ms, 50000 by default' )
66
- . action ( errorHandler ( require ( '../lib/command/check' ) ) )
83
+ . action ( commandHandler ( '../lib/command/check.js' ) )
67
84
68
- program
69
- . command ( 'migrate [path]' )
70
- . description ( 'Migrate json config to js config in current dir or [path]' )
71
- . action ( errorHandler ( require ( '../lib/command/configMigrate' ) ) )
85
+ program . command ( 'migrate [path]' ) . description ( 'Migrate json config to js config in current dir or [path]' ) . action ( commandHandler ( '../lib/command/configMigrate.js' ) )
72
86
73
87
program
74
88
. command ( 'shell [path]' )
@@ -78,34 +92,30 @@ program
78
92
. option ( commandFlags . profile . flag , commandFlags . profile . description )
79
93
. option ( commandFlags . ai . flag , commandFlags . ai . description )
80
94
. option ( commandFlags . config . flag , commandFlags . config . description )
81
- . action ( errorHandler ( require ( '../lib/command/interactive' ) ) )
95
+ . action ( commandHandler ( '../lib/command/interactive.js' ) )
82
96
83
- program
84
- . command ( 'list [path]' )
85
- . alias ( 'l' )
86
- . description ( 'List all actions for I.' )
87
- . action ( errorHandler ( require ( '../lib/command/list' ) ) )
97
+ program . command ( 'list [path]' ) . alias ( 'l' ) . description ( 'List all actions for I.' ) . action ( commandHandler ( '../lib/command/list.js' ) )
88
98
89
99
program
90
100
. command ( 'def [path]' )
91
101
. description ( 'Generates TypeScript definitions for all I actions.' )
92
102
. option ( commandFlags . config . flag , commandFlags . config . description )
93
103
. option ( '-o, --output [folder]' , 'target folder to paste definitions' )
94
- . action ( errorHandler ( require ( '../lib/command/definitions' ) ) )
104
+ . action ( commandHandler ( '../lib/command/definitions.js' ) )
95
105
96
106
program
97
107
. command ( 'gherkin:init [path]' )
98
108
. alias ( 'bdd:init' )
99
109
. description ( 'Prepare CodeceptJS to run feature files.' )
100
110
. option ( commandFlags . config . flag , commandFlags . config . description )
101
- . action ( errorHandler ( require ( '../lib/command/gherkin/init' ) ) )
111
+ . action ( commandHandler ( '../lib/command/gherkin/init.js' ) )
102
112
103
113
program
104
114
. command ( 'gherkin:steps [path]' )
105
115
. alias ( 'bdd:steps' )
106
116
. description ( 'Prints all defined gherkin steps.' )
107
117
. option ( commandFlags . config . flag , commandFlags . config . description )
108
- . action ( errorHandler ( require ( '../lib/command/gherkin/steps' ) ) )
118
+ . action ( commandHandler ( '../lib/command/gherkin/steps.js' ) )
109
119
110
120
program
111
121
. command ( 'gherkin:snippets [path]' )
@@ -115,38 +125,22 @@ program
115
125
. option ( commandFlags . config . flag , commandFlags . config . description )
116
126
. option ( '--feature [file]' , 'feature files(s) to scan' )
117
127
. option ( '--path [file]' , 'file in which to place the new snippets' )
118
- . action ( errorHandler ( require ( '../lib/command/gherkin/snippets' ) ) )
128
+ . action ( commandHandler ( '../lib/command/gherkin/snippets.js' ) )
119
129
120
- program
121
- . command ( 'generate:test [path]' )
122
- . alias ( 'gt' )
123
- . description ( 'Generates an empty test' )
124
- . action ( errorHandler ( require ( '../lib/command/generate' ) . test ) )
130
+ program . command ( 'generate:test [path]' ) . alias ( 'gt' ) . description ( 'Generates an empty test' ) . action ( commandHandlerWithProperty ( '../lib/command/generate.js' , 'test' ) )
125
131
126
- program
127
- . command ( 'generate:pageobject [path]' )
128
- . alias ( 'gpo' )
129
- . description ( 'Generates an empty page object' )
130
- . action ( errorHandler ( require ( '../lib/command/generate' ) . pageObject ) )
132
+ program . command ( 'generate:pageobject [path]' ) . alias ( 'gpo' ) . description ( 'Generates an empty page object' ) . action ( commandHandlerWithProperty ( '../lib/command/generate.js' , 'pageObject' ) )
131
133
132
134
program
133
135
. command ( 'generate:object [path]' )
134
136
. alias ( 'go' )
135
137
. option ( '--type, -t [kind]' , 'type of object to be created' )
136
138
. description ( 'Generates an empty support object (page/step/fragment)' )
137
- . action ( errorHandler ( require ( '../lib/command/generate' ) . pageObject ) )
139
+ . action ( commandHandlerWithProperty ( '../lib/command/generate.js' , ' pageObject' ) )
138
140
139
- program
140
- . command ( 'generate:helper [path]' )
141
- . alias ( 'gh' )
142
- . description ( 'Generates a new helper' )
143
- . action ( errorHandler ( require ( '../lib/command/generate' ) . helper ) )
141
+ program . command ( 'generate:helper [path]' ) . alias ( 'gh' ) . description ( 'Generates a new helper' ) . action ( commandHandlerWithProperty ( '../lib/command/generate.js' , 'helper' ) )
144
142
145
- program
146
- . command ( 'generate:heal [path]' )
147
- . alias ( 'gr' )
148
- . description ( 'Generates basic heal recipes' )
149
- . action ( errorHandler ( require ( '../lib/command/generate' ) . heal ) )
143
+ program . command ( 'generate:heal [path]' ) . alias ( 'gr' ) . description ( 'Generates basic heal recipes' ) . action ( commandHandlerWithProperty ( '../lib/command/generate.js' , 'heal' ) )
150
144
151
145
program
152
146
. command ( 'run [test]' )
@@ -185,7 +179,7 @@ program
185
179
. option ( '--recursive' , 'include sub directories' )
186
180
. option ( '--trace' , 'trace function calls' )
187
181
. option ( '--child <string>' , 'option for child processes' )
188
- . action ( errorHandler ( require ( '../lib/command/run' ) ) )
182
+ . action ( commandHandler ( '../lib/command/run.js' ) )
189
183
190
184
program
191
185
. command ( 'run-workers <workers> [selectedRuns...]' )
@@ -204,7 +198,7 @@ program
204
198
. option ( '-p, --plugins <k=v,k2=v2,...>' , 'enable plugins, comma-separated' )
205
199
. option ( '-O, --reporter-options <k=v,k2=v2,...>' , 'reporter-specific options' )
206
200
. option ( '-R, --reporter <name>' , 'specify the reporter to use' )
207
- . action ( errorHandler ( require ( '../lib/command/run-workers' ) ) )
201
+ . action ( commandHandler ( '../lib/command/run-workers.js' ) )
208
202
209
203
program
210
204
. command ( 'run-multiple [suites...]' )
@@ -230,13 +224,9 @@ program
230
224
// mocha options
231
225
. option ( '--colors' , 'force enabling of colors' )
232
226
233
- . action ( errorHandler ( require ( '../lib/command/run-multiple' ) ) )
227
+ . action ( commandHandler ( '../lib/command/run-multiple.js' ) )
234
228
235
- program
236
- . command ( 'info [path]' )
237
- . description ( 'Print debugging information concerning the local environment' )
238
- . option ( '-c, --config' , 'your config file path' )
239
- . action ( errorHandler ( require ( '../lib/command/info' ) ) )
229
+ program . command ( 'info [path]' ) . description ( 'Print debugging information concerning the local environment' ) . option ( '-c, --config' , 'your config file path' ) . action ( commandHandler ( '../lib/command/info.js' ) )
240
230
241
231
program
242
232
. command ( 'dry-run [test]' )
@@ -253,7 +243,7 @@ program
253
243
. option ( commandFlags . steps . flag , commandFlags . steps . description )
254
244
. option ( commandFlags . verbose . flag , commandFlags . verbose . description )
255
245
. option ( commandFlags . debug . flag , commandFlags . debug . description )
256
- . action ( errorHandler ( require ( '../lib/command/dryRun' ) ) )
246
+ . action ( commandHandler ( '../lib/command/dryRun.js' ) )
257
247
258
248
program
259
249
. command ( 'run-rerun [test]' )
@@ -291,7 +281,10 @@ program
291
281
. option ( '--trace' , 'trace function calls' )
292
282
. option ( '--child <string>' , 'option for child processes' )
293
283
294
- . action ( require ( '../lib/command/run-rerun' ) )
284
+ . action ( async ( ...args ) => {
285
+ const runRerun = await dynamicImport ( '../lib/command/run-rerun.js' )
286
+ return runRerun ( ...args )
287
+ } )
295
288
296
289
program . on ( 'command:*' , cmd => {
297
290
console . log ( `\nUnknown command ${ cmd } \n` )
0 commit comments