Skip to content

Commit ecdd29b

Browse files
committed
fix handle module type
1 parent b259ea0 commit ecdd29b

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

index.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ let eslintConfig =
2222

2323
function projectModules() {
2424
const packageJson = JSON.parse(fs.readFileSync('./package.json'))
25-
const packageType = packageJson.type || 'commonjs'
25+
return packageJson.type == undefined ? true : false
26+
}
2627

27-
return packageType === 'commonjs'
28+
function existModuleType() {
29+
const packageJson = JSON.parse(fs.readFileSync('./package.json'))
30+
if (packageJson.type != undefined) {
31+
return packageJson.type == 'module' ? "Javascript modules (import/export)" : "CommonJS (require/exports)"
32+
}
2833
}
2934

3035
function validateInput(input) {
@@ -47,9 +52,9 @@ function generateCommand() {
4752
console.log(`${'\x1b[34m'}Generating automation test..${'\x1b[0m'}`)
4853

4954
//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)")
55+
generate(answers.jsonFileQ.includes('"') ? answers.jsonFileQ.replace(/"/g, '') : answers.jsonFileQ, existModuleType())
5156
// write test script for run the regression test
52-
addScriptRunner()
57+
rebuildPackagejson()
5358
})
5459
.catch((err) => {
5560
console.log(err);
@@ -60,7 +65,7 @@ function generateCommand() {
6065

6166
const argRunner = process.argv[process.argv.length - 1]
6267

63-
if (argRunner != 'undefined' && argRunner == 'generate') {
68+
if (argRunner == 'generate') {
6469
generateCommand()
6570
} else {
6671
exec('npm list --json', (error, stdout) => {
@@ -133,7 +138,7 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
133138
npm += ' eslint'
134139

135140
// Write eslint configuration
136-
let moduleType = answers.moduleQ || "Javascript modules (import/export)"
141+
let moduleType = answers.moduleQ || existModuleType()
137142
if (moduleType == "Javascript modules (import/export)") {
138143
const jsonConfig = JSON.parse(eslintConfig)
139144
jsonConfig.parserOptions = { ecmaVersion: 'latest', sourceType: 'module' }
@@ -164,10 +169,10 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
164169
console.log(`${'\x1b[34m'}Generating automation test..${'\x1b[0m'}`)
165170

166171
//Call the generate function to generate automation tests.
167-
generate(answers.jsonFileQ.includes('"') ? answers.jsonFileQ.replace(/"/g, '') : answers.jsonFileQ, answers.moduleQ || "Javascript modules (import/export)")
172+
generate(answers.jsonFileQ.includes('"') ? answers.jsonFileQ.replace(/"/g, '') : answers.jsonFileQ, answers.moduleQ || existModuleType())
168173

169174
// write test script for run the regression test
170-
addScriptRunner()
175+
rebuildPackagejson(answers.moduleQ)
171176
}
172177

173178
})
@@ -180,15 +185,22 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
180185
})
181186
}
182187

183-
function addScriptRunner() {
188+
function rebuildPackagejson(answers) {
184189
const scriptName = 'test:dev'; // Name of your new script
185190
const scriptCommand = 'cross-env NODE_ENV=dev mocha runner/regression.js --timeout 15000'; // Command to execute your script
191+
192+
const typeKey = 'type'; // Name of your new script
193+
const typeValue = answers == "CommonJS (require/exports)" ? 'commonjs' : 'module'; // Command to execute your script
186194

187195
// Read the package.json answers.jsonFileQ
188196
const packageJson = JSON.parse(fs.readFileSync('./package.json'));
189197

190198
// Add the new script to the scripts object
191199
packageJson.scripts[scriptName] = scriptCommand;
200+
// Add type project
201+
if (answers != undefined) {
202+
packageJson[typeKey] = typeValue
203+
}
192204

193205
// Write the updated package.json answers.jsonFileQ
194206
fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
@@ -215,10 +227,10 @@ function installPackage(strPack, npm, jsonfile, moduleQ) {
215227
console.log(`${'\x1b[34m'}Generating automation test..${'\x1b[0m'}`)
216228

217229
//Call the generate function to generate automation tests.
218-
generate(jsonfile.includes('"') ? jsonfile.replace(/"/g, '') : jsonfile, moduleQ || "Javascript modules (import/export)")
230+
generate(jsonfile.includes('"') ? jsonfile.replace(/"/g, '') : jsonfile, moduleQ || existModuleType())
219231

220232
// write test script for run the regression test
221-
addScriptRunner()
233+
rebuildPackagejson(moduleQ)
222234
}
223235
})
224236
}
@@ -240,10 +252,10 @@ function installDevPackge(npm, jsonfile, moduleQ) {
240252
console.log(`${'\x1b[34m'}Generating automation test..${'\x1b[0m'}`)
241253

242254
//Call the generate function to generate automation tests.
243-
generate(jsonfile.includes('"') ? jsonfile.replace(/"/g, '') : jsonfile, moduleQ || "Javascript modules (import/export)")
255+
generate(jsonfile.includes('"') ? jsonfile.replace(/"/g, '') : jsonfile, moduleQ || existModuleType())
244256

245257
// write test script for run the regression test
246-
addScriptRunner()
258+
rebuildPackagejson(moduleQ)
247259
})
248260
}
249261

0 commit comments

Comments
 (0)