Skip to content

Commit dcf04da

Browse files
authored
Merge pull request #8 from hadiindrawan/dev
handle module export js
2 parents 08e5567 + b2dce48 commit dcf04da

25 files changed

+495
-104
lines changed

index.js

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,63 @@ import inquirer from 'inquirer';
44
import { exec } from 'child_process';
55
import 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+
2461
const argRunner = process.argv[process.argv.length - 1]
2562

2663
if (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-
168185
function 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()

lib/component/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "fs";
22

33
// This is an asynchronous function called writeData.
44

5-
async function writeData() {
5+
async function writeData(moduleType) {
66
// Here, a constant variable dataDir is declared and assigned to the string 'tests/data/file'.
77

88
const dataDir = "tests/data/file";

lib/component/helper.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import isFileExisted from "../utils/check_dir.js";
33
import basePath from "../utils/base_path.js";
44

55
// Asynchronous function to write data into directory
6-
async function writeHelper() {
6+
async function writeHelper(moduleType) {
7+
// template dir name
8+
const templateDirRequest = moduleType == "Javascript modules (import/export)" ? "lib/template/jsimport/requestHelper.dot" : "lib/template/commonjs/requestHelper.dot"
9+
const templateDirGeneral = moduleType == "Javascript modules (import/export)" ? "lib/template/jsimport/generalHelper.dot" : "lib/template/commonjs/generalHelper.dot"
710
// create helper directory if it doesn't exists
811
const helperDir = "tests/helper";
912
fs.mkdirSync(helperDir, { recursive: true });
@@ -15,7 +18,7 @@ async function writeHelper() {
1518
if (!data[0]) {
1619
fs.writeFile(
1720
"tests/helper/request.helper.js",
18-
fs.readFileSync(basePath() + "lib/template/requestHelper.dot", "utf8"),
21+
fs.readFileSync(basePath() + templateDirRequest, "utf8"),
1922
function (err) {
2023
if (err) throw err;
2124
}
@@ -31,7 +34,7 @@ async function writeHelper() {
3134
if (!data[0]) {
3235
fs.writeFile(
3336
"tests/helper/general.helper.js",
34-
fs.readFileSync(basePath() + "lib/template/generalHelper.dot", "utf8"),
37+
fs.readFileSync(basePath() + templateDirGeneral, "utf8"),
3538
function (err) {
3639
if (err) throw err;
3740
}

lib/component/json_schema.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import isFileExisted from "../utils/check_dir.js"
33
import basePath from "../utils/base_path.js"
44

55
// Json schema file generator
6-
async function writeJsonSchema(element, jsonSchemaPath) {
6+
async function writeJsonSchema(element, jsonSchemaPath, moduleType) {
7+
// template dir name
8+
const templateDir = moduleType == "Javascript modules (import/export)" ? "lib/template/jsimport/json_responses.dot" : "lib/template/commonjs/json_responses.dot"
79
// The following code creates a variable called 'name' and assigns it the value obtained from the 'name' property of the 'element' object, which is then converted to lowercase and all spaces in it are removed.
810
let name = (element.name).toLowerCase().replace(/\s/g, '');
911
name = name.replace(/\//g, '');
@@ -15,7 +17,7 @@ async function writeJsonSchema(element, jsonSchemaPath) {
1517
if (!data[0]) {
1618
// create file test
1719
fs.writeFile(jsonSchemaPath + '/' + method + '_' +name + '.schema.js',
18-
fs.readFileSync(basePath() + 'lib/template/json_responses.dot', 'utf8'), function (err) { if (err) throw err; });
20+
fs.readFileSync(basePath() + templateDir, 'utf8'), function (err) { if (err) throw err; });
1921
}
2022
})
2123
.catch((err) => console.log(err));

lib/component/pages.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import isFileExisted from "../utils/check_dir.js"
55
import basePath from "../utils/base_path.js"
66

77
// Pages file generator
8-
async function writePages(element, path, jsonSchemaRelativePath, helperPath, mainpagesPath) {
9-
let contents = fs.readFileSync(basePath() + 'lib/template/request.dot', 'utf8');
8+
async function writePages(element, path, jsonSchemaRelativePath, helperPath, mainpagesPath, moduleType) {
9+
// template dir name
10+
const templateDir = moduleType == "Javascript modules (import/export)" ? "lib/template/jsimport/request.dot" : "lib/template/commonjs/request.dot"
11+
const templateDirAttach = moduleType == "Javascript modules (import/export)" ? "lib/template/jsimport/requestWithAttach.dot" : "lib/template/commonjs/requestWithAttach.dot"
12+
const templateDirMain = moduleType == "Javascript modules (import/export)" ? "lib/template/jsimport/mainPages.dot" : "lib/template/commonjs/mainPages.dot"
13+
// read template file
14+
let contents = fs.readFileSync(basePath() + templateDir, 'utf8');
1015

1116
let name = (element.name).toLowerCase().replace(/\s/g, '');
1217
name = name.replace(/\//g, '');
@@ -29,7 +34,7 @@ async function writePages(element, path, jsonSchemaRelativePath, helperPath, mai
2934
attKey += '{'+'\r\n'+'\t\t\t'
3035

3136
if (data.some(datas => datas['type'] === 'file')) {
32-
contents = fs.readFileSync(basePath() + 'lib/template/requestWithAttach.dot', 'utf8');
37+
contents = fs.readFileSync(basePath() + templateDirAttach, 'utf8');
3338

3439
asyncForEach(data, async (body) => {
3540
if (body.disabled != true) {
@@ -55,7 +60,7 @@ async function writePages(element, path, jsonSchemaRelativePath, helperPath, mai
5560
}
5661
})
5762
} else {
58-
contents = fs.readFileSync(basePath() + 'lib/template/request.dot', 'utf8');
63+
contents = fs.readFileSync(basePath() + templateDir, 'utf8');
5964
asyncForEach(data, async (body) => {
6065
if (body.disabled != true) {
6166
if (first === false) bodyRaw += ','+'\r\n'+'\t\t\t';
@@ -147,17 +152,17 @@ async function writePages(element, path, jsonSchemaRelativePath, helperPath, mai
147152
if (!data[0]) {
148153
// create file test
149154
fs.writeFile('tests/pages/main.pages.js',
150-
fs.readFileSync(basePath() + 'lib/template/mainPages.dot', 'utf8'), function (err) { if (err) throw err; });
155+
fs.readFileSync(basePath() + templateDirMain, 'utf8'), function (err) { if (err) throw err; });
151156
}
152157
})
153158
.catch((err) => console.log(err))
154159

155160
// check if pages file exists
156-
isFileExisted(path, method + '_' + name + '.js')
161+
isFileExisted(path, method + '_' + name + '.pages.js')
157162
.then((data) => {
158163
if (!data[0]) {
159164
// create file test
160-
fs.writeFile(path + '/' + method + '_' + name + '.js',
165+
fs.writeFile(path + '/' + method + '_' + name + '.pages.js',
161166
code, function (err) { if (err) throw err; });
162167
}
163168
})

0 commit comments

Comments
 (0)