11#!/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
69
710const commandFlags = {
811 ai : {
@@ -42,6 +45,23 @@ const errorHandler =
4245 }
4346 }
4447
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+
4565if ( process . versions . node && process . versions . node . split ( '.' ) && process . versions . node . split ( '.' ) [ 0 ] < 12 ) {
4666 error ( 'NodeJS >= 12 is required to run.' )
4767 print ( )
@@ -53,22 +73,16 @@ if (process.versions.node && process.versions.node.split('.') && process.version
5373program . usage ( '<command> [options]' )
5474program . version ( Codecept . version ( ) )
5575
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' ) )
6077
6178program
6279 . command ( 'check' )
6380 . option ( commandFlags . config . flag , commandFlags . config . description )
6481 . description ( 'Checks configuration and environment before running tests' )
6582 . 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' ) )
6784
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' ) )
7286
7387program
7488 . command ( 'shell [path]' )
@@ -78,34 +92,30 @@ program
7892 . option ( commandFlags . profile . flag , commandFlags . profile . description )
7993 . option ( commandFlags . ai . flag , commandFlags . ai . description )
8094 . option ( commandFlags . config . flag , commandFlags . config . description )
81- . action ( errorHandler ( require ( '../lib/command/interactive' ) ) )
95+ . action ( commandHandler ( '../lib/command/interactive.js' ) )
8296
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' ) )
8898
8999program
90100 . command ( 'def [path]' )
91101 . description ( 'Generates TypeScript definitions for all I actions.' )
92102 . option ( commandFlags . config . flag , commandFlags . config . description )
93103 . option ( '-o, --output [folder]' , 'target folder to paste definitions' )
94- . action ( errorHandler ( require ( '../lib/command/definitions' ) ) )
104+ . action ( commandHandler ( '../lib/command/definitions.js' ) )
95105
96106program
97107 . command ( 'gherkin:init [path]' )
98108 . alias ( 'bdd:init' )
99109 . description ( 'Prepare CodeceptJS to run feature files.' )
100110 . option ( commandFlags . config . flag , commandFlags . config . description )
101- . action ( errorHandler ( require ( '../lib/command/gherkin/init' ) ) )
111+ . action ( commandHandler ( '../lib/command/gherkin/init.js' ) )
102112
103113program
104114 . command ( 'gherkin:steps [path]' )
105115 . alias ( 'bdd:steps' )
106116 . description ( 'Prints all defined gherkin steps.' )
107117 . option ( commandFlags . config . flag , commandFlags . config . description )
108- . action ( errorHandler ( require ( '../lib/command/gherkin/steps' ) ) )
118+ . action ( commandHandler ( '../lib/command/gherkin/steps.js' ) )
109119
110120program
111121 . command ( 'gherkin:snippets [path]' )
@@ -115,38 +125,22 @@ program
115125 . option ( commandFlags . config . flag , commandFlags . config . description )
116126 . option ( '--feature [file]' , 'feature files(s) to scan' )
117127 . 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' ) )
119129
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' ) )
125131
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' ) )
131133
132134program
133135 . command ( 'generate:object [path]' )
134136 . alias ( 'go' )
135137 . option ( '--type, -t [kind]' , 'type of object to be created' )
136138 . 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' ) )
138140
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' ) )
144142
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' ) )
150144
151145program
152146 . command ( 'run [test]' )
@@ -185,7 +179,7 @@ program
185179 . option ( '--recursive' , 'include sub directories' )
186180 . option ( '--trace' , 'trace function calls' )
187181 . option ( '--child <string>' , 'option for child processes' )
188- . action ( errorHandler ( require ( '../lib/command/run' ) ) )
182+ . action ( commandHandler ( '../lib/command/run.js' ) )
189183
190184program
191185 . command ( 'run-workers <workers> [selectedRuns...]' )
@@ -204,7 +198,7 @@ program
204198 . option ( '-p, --plugins <k=v,k2=v2,...>' , 'enable plugins, comma-separated' )
205199 . option ( '-O, --reporter-options <k=v,k2=v2,...>' , 'reporter-specific options' )
206200 . 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' ) )
208202
209203program
210204 . command ( 'run-multiple [suites...]' )
@@ -230,13 +224,9 @@ program
230224 // mocha options
231225 . option ( '--colors' , 'force enabling of colors' )
232226
233- . action ( errorHandler ( require ( '../lib/command/run-multiple' ) ) )
227+ . action ( commandHandler ( '../lib/command/run-multiple.js' ) )
234228
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' ) )
240230
241231program
242232 . command ( 'dry-run [test]' )
@@ -253,7 +243,7 @@ program
253243 . option ( commandFlags . steps . flag , commandFlags . steps . description )
254244 . option ( commandFlags . verbose . flag , commandFlags . verbose . description )
255245 . option ( commandFlags . debug . flag , commandFlags . debug . description )
256- . action ( errorHandler ( require ( '../lib/command/dryRun' ) ) )
246+ . action ( commandHandler ( '../lib/command/dryRun.js' ) )
257247
258248program
259249 . command ( 'run-rerun [test]' )
@@ -291,7 +281,10 @@ program
291281 . option ( '--trace' , 'trace function calls' )
292282 . option ( '--child <string>' , 'option for child processes' )
293283
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+ } )
295288
296289program . on ( 'command:*' , cmd => {
297290 console . log ( `\nUnknown command ${ cmd } \n` )
0 commit comments