@@ -4,49 +4,63 @@ import inquirer from 'inquirer';
44import { exec } from 'child_process' ;
55import generate from './lib/generate.js' ;
66
7- const eslintConfig =
7+ let eslintConfig =
88`{
99 "env": {
1010 "browser": true,
1111 "es2021": true
1212 },
1313 "extends": "eslint:recommended",
1414 "parserOptions": {
15- "ecmaVersion": "latest",
16- "sourceType": "module"
15+ "ecmaVersion": "latest"
1716 },
1817 "rules": {
1918 "no-undef": 0,
2019 "no-prototype-builtins": 0
2120 }
2221}`
2322
23+ function projectModules ( ) {
24+ const packageJson = JSON . parse ( fs . readFileSync ( './package.json' ) )
25+ const packageType = packageJson . type || 'commonjs'
26+
27+ return packageType === 'commonjs'
28+ }
29+
30+ function validateInput ( input ) {
31+ return input . includes ( 'json' ) ? true : 'Please type correct answer, the file must be json format!'
32+ }
33+
34+ function generateCommand ( ) {
35+ inquirer
36+ . prompt ( [
37+ {
38+ type : 'input' ,
39+ name : 'jsonFileQ' ,
40+ message : 'Input your json file to be generate (example.json):' ,
41+ default : 'example.json' ,
42+ validate : validateInput
43+ }
44+ ] )
45+ . then ( ( answers ) => {
46+ //Print message indicating automation test generation has started..
47+ console . log ( `${ '\x1b[34m' } Generating automation test..${ '\x1b[0m' } ` )
48+
49+ //Call the generate function to generate automation tests.
50+ generate ( answers . jsonFileQ . includes ( '"' ) ? answers . jsonFileQ . replace ( / " / g, '' ) : answers . jsonFileQ , projectModules ( ) ? "CommonJS (require/exports)" : "Javascript modules (import/export)" )
51+ // write test script for run the regression test
52+ addScriptRunner ( )
53+ } )
54+ . catch ( ( err ) => {
55+ console . log ( err ) ;
56+ console . log ( 'Please type correct answer!' ) ;
57+ generateCommand ( )
58+ } )
59+ }
60+
2461const argRunner = process . argv [ process . argv . length - 1 ]
2562
2663if ( argRunner != 'undefined' && argRunner == 'generate' ) {
27- function generateCommand ( ) {
28- inquirer
29- . prompt ( [
30- {
31- type : 'input' ,
32- name : 'jsonFileQ' ,
33- message : 'Input your json file to be generate (example.json):' ,
34- validate : validateInput
35- }
36- ] )
37- . then ( ( answers ) => {
38- //Print message indicating automation test generation has started..
39- console . log ( `${ '\x1b[34m' } Generating automation test..${ '\x1b[0m' } ` )
40-
41- //Call the generate function to generate automation tests.
42- generate ( answers . jsonFileQ . includes ( '"' ) ? answers . jsonFileQ . replace ( / " / g, '' ) : answers . jsonFileQ )
43- } )
44- . catch ( ( err ) => {
45- console . log ( err ) ;
46- console . log ( 'Please type correct answer!' ) ;
47- generateCommand ( )
48- } )
49- }
5064 generateCommand ( )
5165} else {
5266 exec ( 'npm list --json' , ( error , stdout ) => {
@@ -74,26 +88,22 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
7488 }
7589 }
7690
77- function projectModules ( ) {
78- return true
79- }
80-
8191 function question ( ) {
8292 inquirer
8393 . prompt ( [
8494 {
8595 type : 'list' ,
8696 name : 'frameworkQ' ,
8797 message : 'What framework will be used?' ,
88- choices : [ "Mocha chai" , "WebDriverIO" ] ,
98+ choices : [ "Mocha chai" ] ,
8999 when : ( ) => mochaExist
90100 } ,
91101 {
92102 type : 'list' ,
93103 name : 'moduleQ' ,
94104 message : 'What type of modules does your project use?' ,
95105 choices : [ "Javascript modules (import/export)" , "CommonJS (require/exports)" ] ,
96- when : ( ) => projectModules
106+ when : ( ) => projectModules ( )
97107 } ,
98108 {
99109 type : 'list' ,
@@ -113,6 +123,7 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
113123 type : 'input' ,
114124 name : 'jsonFileQ' ,
115125 message : 'Type your json file to be generate (example.json):' ,
126+ default : 'example.json' ,
116127 validate : validateInput
117128 }
118129 ] )
@@ -122,6 +133,16 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
122133 npm += ' eslint'
123134
124135 // Write eslint configuration
136+ let moduleType = answers . moduleQ || "Javascript modules (import/export)"
137+ if ( moduleType == "Javascript modules (import/export)" ) {
138+ const jsonConfig = JSON . parse ( eslintConfig )
139+ jsonConfig . parserOptions = { ecmaVersion : 'latest' , sourceType : 'module' }
140+
141+ eslintConfig = JSON . stringify ( jsonConfig , null , 2 )
142+ }
143+
144+ console . log ( eslintConfig ) ;
145+
125146 fs . writeFile ( '.eslintrc.json' , eslintConfig , function ( err ) { if ( err ) throw err ; } ) ;
126147 }
127148 if ( answers . mochaweQ == 'Yes' ) {
@@ -131,21 +152,21 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
131152 if ( strPack != '' && npm != '' ) {
132153 // This line of code will print "Installing dependencies..." on the console.
133154 console . log ( "Installing dependencies..." ) ;
134- installPackage ( strPack , npm , answers . jsonFileQ )
155+ installPackage ( strPack , npm , answers . jsonFileQ , answers . moduleQ )
135156 } else if ( strPack != '' && npm == '' ) {
136157 // This line of code will print "Installing dependencies..." on the console.
137158 console . log ( "Installing dependencies..." ) ;
138- installPackage ( strPack , npm , answers . jsonFileQ )
159+ installPackage ( strPack , npm , answers . jsonFileQ , answers . moduleQ )
139160 } else if ( strPack == '' && npm != '' ) {
140161 // This line of code will print "Installing dependencies..." on the console.
141162 console . log ( "Installing dependencies..." ) ;
142- installDevPackge ( npm , answers . jsonFileQ )
163+ installDevPackge ( npm , answers . jsonFileQ , answers . moduleQ )
143164 } else {
144165 //Print message indicating automation test generation has started..
145166 console . log ( `${ '\x1b[34m' } Generating automation test..${ '\x1b[0m' } ` )
146167
147168 //Call the generate function to generate automation tests.
148- generate ( answers . jsonFileQ . includes ( '"' ) ? answers . jsonFileQ . replace ( / " / g, '' ) : answers . jsonFileQ )
169+ generate ( answers . jsonFileQ . includes ( '"' ) ? answers . jsonFileQ . replace ( / " / g, '' ) : answers . jsonFileQ , answers . moduleQ || "Javascript modules (import/export)" )
149170
150171 // write test script for run the regression test
151172 addScriptRunner ( )
@@ -161,10 +182,6 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
161182 } )
162183}
163184
164- function validateInput ( input ) {
165- return input . includes ( 'json' ) ? true : 'Please type correct answer, the file must be json format!'
166- }
167-
168185function addScriptRunner ( ) {
169186 const scriptName = 'test:dev' ; // Name of your new script
170187 const scriptCommand = 'cross-env NODE_ENV=dev mocha runner/regression.js --timeout 15000' ; // Command to execute your script
@@ -179,7 +196,7 @@ function addScriptRunner() {
179196 fs . writeFileSync ( './package.json' , JSON . stringify ( packageJson , null , 2 ) ) ;
180197}
181198
182- function installPackage ( strPack , npm , jsonfile ) {
199+ function installPackage ( strPack , npm , jsonfile , moduleQ ) {
183200 const installProcess = exec ( 'npm install ' + strPack ) ;
184201 //This code is registering a listener to the exit event of installProcess
185202 installProcess . on ( 'exit' , ( code ) => {
@@ -200,15 +217,15 @@ function installPackage(strPack, npm, jsonfile) {
200217 console . log ( `${ '\x1b[34m' } Generating automation test..${ '\x1b[0m' } ` )
201218
202219 //Call the generate function to generate automation tests.
203- generate ( jsonfile . includes ( '"' ) ? jsonfile . replace ( / " / g, '' ) : jsonfile )
220+ generate ( jsonfile . includes ( '"' ) ? jsonfile . replace ( / " / g, '' ) : jsonfile , moduleQ || "Javascript modules (import/export)" )
204221
205222 // write test script for run the regression test
206223 addScriptRunner ( )
207224 }
208225 } )
209226}
210227
211- function installDevPackge ( npm , jsonfile ) {
228+ function installDevPackge ( npm , jsonfile , moduleQ ) {
212229 const installOption = exec ( 'npm install' + npm + ' --save-dev' )
213230 installOption . on ( 'exit' , ( res ) => {
214231 //checking if npm install failed or succeeded by checking exit code
@@ -225,7 +242,7 @@ function installDevPackge(npm,jsonfile) {
225242 console . log ( `${ '\x1b[34m' } Generating automation test..${ '\x1b[0m' } ` )
226243
227244 //Call the generate function to generate automation tests.
228- generate ( jsonfile . includes ( '"' ) ? jsonfile . replace ( / " / g, '' ) : jsonfile )
245+ generate ( jsonfile . includes ( '"' ) ? jsonfile . replace ( / " / g, '' ) : jsonfile , moduleQ || "Javascript modules (import/export)" )
229246
230247 // write test script for run the regression test
231248 addScriptRunner ( )
0 commit comments