Skip to content

Commit 05a0cec

Browse files
committed
fix npx runner
1 parent c6811e1 commit 05a0cec

File tree

1 file changed

+151
-94
lines changed

1 file changed

+151
-94
lines changed

index.js

Lines changed: 151 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@ import inquirer from 'inquirer';
44
import { exec } from 'child_process';
55
import generate from './lib/generate.js';
66

7-
function addScriptRunner() {
8-
const scriptName = 'test:regression'; // Name of your new script
9-
const scriptCommand = 'mocha runner/regression.js --timeout 15000 --dev'; // Command to execute your script
10-
11-
// Read the package.json answers.jsonFileQ
12-
const packageJson = JSON.parse(fs.readFileSync('./package.json'));
13-
14-
// Add the new script to the scripts object
15-
packageJson.scripts[scriptName] = scriptCommand;
16-
17-
// Write the updated package.json answers.jsonFileQ
18-
fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
19-
}
20-
217
const eslintConfig =
228
`{
239
"env": {
@@ -37,93 +23,164 @@ const eslintConfig =
3723
}
3824
}`
3925

40-
exec('npm list --json', (error, stdout, stderr) => {
41-
if (error) {
42-
console.error(`exec error: ${error}`);
43-
return;
26+
const argRunner = process.argv[process.argv.length - 1]
27+
28+
if (argRunner != 'undefined' && argRunner == 'generate') {
29+
function generateCommand() {
30+
inquirer
31+
.prompt([
32+
{
33+
type: 'input',
34+
name: 'jsonFileQ',
35+
message: 'Input your json file to be generate (example.json):',
36+
validate: validateInput
37+
}
38+
])
39+
.then((answers) => {
40+
//Print message indicating automation test generation has started..
41+
console.log(`${'\x1b[34m'}Generating automation test..${'\x1b[0m'}`)
42+
43+
//Call the generate function to generate automation tests.
44+
generate(answers.jsonFileQ.includes('"') ? answers.jsonFileQ.replace(/"/g, '') : answers.jsonFileQ)
45+
})
46+
.catch((err) => {
47+
console.log(err);
48+
console.log('Please type correct answer!');
49+
generateCommand()
50+
})
4451
}
45-
// Parse the JSON object from stdout, and assign any dependencies to packageList variable
46-
const packageList = JSON.parse(stdout).dependencies;
47-
// Set packagesExist variable to list of object keys if packageList is not undefined, otherwise set packagesExist to an empty array
48-
const packagesExist = packageList !== undefined ? Object.keys(packageList) : [];
49-
50-
let beInstall = ['chai', 'mocha', 'chai-http', 'chai-json-schema', 'dotenv', 'to-json-schema']
51-
// let beInstall = ['chai']
52-
let matchedPack = beInstall.filter(key => !packagesExist.includes(key))
53-
let strPack = matchedPack.join(' ')
54-
55-
let mochaExist = packagesExist.includes('mocha') ? false : true
56-
let eslintExist = packagesExist.includes('eslint') ? false : true
57-
let mochaweExist = packagesExist.includes('mochawesome') ? false : true
58-
59-
inquirer
60-
.prompt([
61-
{
62-
type: 'list',
63-
name: 'frameworkQ',
64-
message: 'What framework will be used?',
65-
choices: ["Mocha chai"],
66-
when: () => mochaExist
67-
},
68-
{
69-
type: 'list',
70-
name: 'eslintQ',
71-
message: 'Do you want to install ESlint?',
72-
choices: ["Yes", "No"],
73-
when: () => eslintExist
74-
},
75-
{
76-
type: 'list',
77-
name: 'mochaweQ',
78-
message: 'Do you want to install Mochawesome?',
79-
choices: ["Yes", "No"],
80-
when: () => mochaweExist
81-
},
82-
{
83-
type: 'input',
84-
name: 'jsonFileQ',
85-
message: 'Type your json file to be generate :'
86-
}
87-
])
88-
.then((answers) => {
89-
let npm = ''
90-
if(answers.eslintQ == 'Yes') {
91-
npm += ' eslint'
92-
93-
// Write eslint configuration
94-
fs.writeFile('.eslintrc.json',eslintConfig, function (err) { if (err) throw err ; });
52+
generateCommand()
53+
} else {
54+
exec('npm list --json', (error, stdout) => {
55+
if (error) {
56+
console.error(`exec error: ${error}`);
57+
return;
9558
}
96-
if(answers.mochaweQ == 'Yes') {
97-
npm += ' mochawesome'
59+
// Parse the JSON object from stdout, and assign any dependencies to packageList variable
60+
const packageList = JSON.parse(stdout).dependencies;
61+
// Set packagesExist variable to list of object keys if packageList is not undefined, otherwise set packagesExist to an empty array
62+
const packagesExist = packageList !== undefined ? Object.keys(packageList) : [];
63+
64+
let beInstall = ['chai', 'mocha', 'chai-http', 'chai-json-schema', 'dotenv', 'to-json-schema']
65+
// let beInstall = ['chai']
66+
let matchedPack = beInstall.filter(key => !packagesExist.includes(key))
67+
let strPack = matchedPack.join(' ')
68+
69+
let mochaExist = packagesExist.includes('mocha') ? false : true
70+
let eslintExist = packagesExist.includes('eslint') ? false : true
71+
72+
function mochaweExist(answers) {
73+
if (answers.hasOwnProperty('frameworkQ')) {
74+
return answers.frameworkQ == "Mocha chai" && !packagesExist.includes('mochawesome') ? true : false
75+
} else {
76+
return !packagesExist.includes('mochawesome') ? true : false
77+
}
9878
}
9979

100-
if(strPack != '' && npm != '') {
101-
// This line of code will print "Installing dependencies..." on the console.
102-
console.log("Installing dependencies...");
103-
installPackage(strPack, npm, answers.jsonFileQ)
104-
} else
105-
if(strPack != '' && npm == '') {
106-
// This line of code will print "Installing dependencies..." on the console.
107-
console.log("Installing dependencies...");
108-
installPackage(strPack, npm, answers.jsonFileQ)
109-
} else
110-
if(strPack == '' && npm != '') {
111-
// This line of code will print "Installing dependencies..." on the console.
112-
console.log("Installing dependencies...");
113-
installDevPackge(npm, answers.jsonFileQ)
114-
} else {
115-
//Print message indicating automation test generation has started..
116-
console.log(`${'\x1b[34m'}Generating automation test..${'\x1b[0m'}`)
117-
118-
//Call the generate function to generate automation tests.
119-
generate(answers.jsonFileQ.includes('"') ? answers.jsonFileQ.replace(/"/g, '') : answers.jsonFileQ)
80+
function projectModules() {
81+
return true
82+
}
12083

121-
// write test script for run the regression test
122-
addScriptRunner()
84+
function question() {
85+
inquirer
86+
.prompt([
87+
{
88+
type: 'list',
89+
name: 'frameworkQ',
90+
message: 'What framework will be used?',
91+
choices: ["Mocha chai", "WebDriverIO"],
92+
when: () => mochaExist
93+
},
94+
{
95+
type: 'list',
96+
name: 'moduleQ',
97+
message: 'What type of modules does your project use?',
98+
choices: ["Javascript modules (import/export)", "CommonJS (require/exports)"],
99+
when: () => projectModules
100+
},
101+
{
102+
type: 'list',
103+
name: 'eslintQ',
104+
message: 'Do you want to install ESlint?',
105+
choices: ["Yes", "No"],
106+
when: () => eslintExist
107+
},
108+
{
109+
type: 'list',
110+
name: 'mochaweQ',
111+
message: 'Do you want to install Mochawesome?',
112+
choices: ["Yes", "No"],
113+
when: (answers) => mochaweExist(answers)
114+
},
115+
{
116+
type: 'input',
117+
name: 'jsonFileQ',
118+
message: 'Type your json file to be generate (example.json):',
119+
validate: validateInput
120+
}
121+
])
122+
.then((answers) => {
123+
let npm = ''
124+
if (answers.eslintQ == 'Yes') {
125+
npm += ' eslint'
126+
127+
// Write eslint configuration
128+
fs.writeFile('.eslintrc.json', eslintConfig, function (err) { if (err) throw err; });
129+
}
130+
if (answers.mochaweQ == 'Yes') {
131+
npm += ' mochawesome'
132+
}
133+
134+
if (strPack != '' && npm != '') {
135+
// This line of code will print "Installing dependencies..." on the console.
136+
console.log("Installing dependencies...");
137+
installPackage(strPack, npm, answers.jsonFileQ)
138+
} else if (strPack != '' && npm == '') {
139+
// This line of code will print "Installing dependencies..." on the console.
140+
console.log("Installing dependencies...");
141+
installPackage(strPack, npm, answers.jsonFileQ)
142+
} else if (strPack == '' && npm != '') {
143+
// This line of code will print "Installing dependencies..." on the console.
144+
console.log("Installing dependencies...");
145+
installDevPackge(npm, answers.jsonFileQ)
146+
} else {
147+
//Print message indicating automation test generation has started..
148+
console.log(`${'\x1b[34m'}Generating automation test..${'\x1b[0m'}`)
149+
150+
//Call the generate function to generate automation tests.
151+
generate(answers.jsonFileQ.includes('"') ? answers.jsonFileQ.replace(/"/g, '') : answers.jsonFileQ)
152+
153+
// write test script for run the regression test
154+
addScriptRunner()
155+
}
156+
157+
})
158+
.catch((err) => {
159+
console.log('Please type correct answer!');
160+
question()
161+
})
123162
}
163+
question()
164+
})
165+
}
166+
167+
function validateInput(input) {
168+
return input.includes('json') ? true : 'Please type correct answer, the file must be json format!'
169+
}
170+
171+
function addScriptRunner() {
172+
const scriptName = 'test:regression-dev'; // Name of your new script
173+
const scriptCommand = 'mocha runner/regression.js --timeout 15000'; // Command to execute your script
124174

125-
});
126-
})
175+
// Read the package.json answers.jsonFileQ
176+
const packageJson = JSON.parse(fs.readFileSync('./package.json'));
177+
178+
// Add the new script to the scripts object
179+
packageJson.scripts[scriptName] = scriptCommand;
180+
181+
// Write the updated package.json answers.jsonFileQ
182+
fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
183+
}
127184

128185
function installPackage(strPack, npm, jsonfile) {
129186
const installProcess = exec('npm install ' + strPack);

0 commit comments

Comments
 (0)