Skip to content

Commit 8240070

Browse files
authored
Merge pull request #5 from hadiindrawan/dev
add config for multiple env
2 parents b38127a + f02ed4c commit 8240070

File tree

8 files changed

+121
-33
lines changed

8 files changed

+121
-33
lines changed

index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ if (argRunner != 'undefined' && argRunner == 'generate') {
5959
// Set packagesExist variable to list of object keys if packageList is not undefined, otherwise set packagesExist to an empty array
6060
const packagesExist = packageList !== undefined ? Object.keys(packageList) : [];
6161

62-
let beInstall = ['chai', 'mocha', 'chai-http', 'chai-json-schema', 'dotenv', 'to-json-schema']
63-
// let beInstall = ['chai']
64-
let matchedPack = beInstall.filter(key => !packagesExist.includes(key))
62+
let needPackage = ['chai', 'mocha', 'chai-http', 'chai-json-schema', 'dotenv', 'to-json-schema', 'cross-env']
63+
let matchedPack = needPackage.filter(key => !packagesExist.includes(key))
6564
let strPack = matchedPack.join(' ')
6665

6766
let mochaExist = packagesExist.includes('mocha') ? false : true
@@ -167,8 +166,8 @@ function validateInput(input) {
167166
}
168167

169168
function addScriptRunner() {
170-
const scriptName = 'test:regression-dev'; // Name of your new script
171-
const scriptCommand = 'mocha runner/regression.js --timeout 15000'; // Command to execute your script
169+
const scriptName = 'test:dev'; // Name of your new script
170+
const scriptCommand = 'cross-env NODE_ENV=dev mocha runner/regression.js --timeout 15000'; // Command to execute your script
172171

173172
// Read the package.json answers.jsonFileQ
174173
const packageJson = JSON.parse(fs.readFileSync('./package.json'));

lib/component/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async function writeHelper() {
1010

1111
// Check if a file named 'request.helper.js' exists in the tests/helper dir
1212
// If it does not exist then create a new file based on the template file 'requestHelper.dot'
13-
isFileExisted("tests/helper", "request.helper.js")
13+
isFileExisted(helperDir, "request.helper.js")
1414
.then((data) => {
1515
if (!data[0]) {
1616
fs.writeFile(
@@ -26,7 +26,7 @@ async function writeHelper() {
2626

2727
// Check if a file named 'general.helper.js' exists in the tests/helper dir
2828
// If it does not exist then create a new file based on the template file 'generalHelper.dot'
29-
isFileExisted("tests/helper", "general.helper.js")
29+
isFileExisted(helperDir, "general.helper.js")
3030
.then((data) => {
3131
if (!data[0]) {
3232
fs.writeFile(

lib/component/utils.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from "fs";
2+
import isFileExisted from "../utils/check_dir.js";
3+
import basePath from "../utils/base_path.js";
4+
5+
// Asynchronous function to write data into directory
6+
async function writeUtils() {
7+
// create helper directory if it doesn't exists
8+
const utilsDir = "tests/utils";
9+
fs.mkdirSync(utilsDir, { recursive: true });
10+
11+
// Check if a file named 'request.helper.js' exists in the tests/helper dir
12+
// If it does not exist then create a new file based on the template file 'requestHelper.dot'
13+
isFileExisted(utilsDir, "config.js")
14+
.then((data) => {
15+
if (!data[0]) {
16+
fs.writeFile(
17+
"tests/utils/config.js",
18+
fs.readFileSync(basePath() + "lib/template/config.dot", "utf8"),
19+
function (err) {
20+
if (err) throw err;
21+
}
22+
)
23+
}
24+
})
25+
.catch((err) => console.log(err));
26+
}
27+
28+
export default writeUtils;

lib/generate.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import writeData from './component/data.js'
77
import writeRunner from './component/runner.js'
88
import writeHelper from './component/helper.js'
99
import writeJsonSchema from './component/json_schema.js'
10+
import writeUtils from './component/utils.js';
1011

1112
// main generator
1213
async function generate(file) {
@@ -18,6 +19,8 @@ async function generate(file) {
1819
writeData()
1920
// write helper dir
2021
writeHelper()
22+
// write utils dir
23+
writeUtils()
2124

2225
// write .example.env file
2326
fs.writeFile('.example.env','DEV_MAIN=your_api_url', function (err) { if (err) throw err ; });

lib/template/config.dot

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const dotenv = require('dotenv')
2+
3+
class config {
4+
constructor() {
5+
// Write your constructor here, if you need
6+
}
7+
8+
env() {
9+
// change according to your need
10+
dotenv.config({ path: __dirname + `/../../.env.${process.env.NODE_ENV}` });
11+
12+
// Defining an object named 'env', contained your variables needed
13+
const env = {
14+
host: process.env.MAIN
15+
}
16+
17+
return env
18+
}
19+
20+
}
21+
module.exports = config

lib/template/generalHelper.dot

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class general {
22
constructor() {
33
// Write your constructor here, if you need
4-
54
}
65

76
// Write your method here

lib/template/mainPages.dot

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,15 @@
11
const chai = require('chai')
22
chai.use(require('chai-http'))
3-
require('dotenv').config()
3+
const config = require('../utils/config')
44

55
class main {
66
constructor() {
77
// Write your constructor here, if you need
8-
this.env = process.env.npm_lifecycle_event
98
}
109

11-
// This function is named `mainService` which contains switch statements that return different Chai request urls based on the environment specified
10+
// This function is named `mainService` that return different Chai request urls based on the environment specified
1211
mainService() {
13-
// A Switch statement which will perform action based on the matches of cases
14-
switch (true) {
15-
16-
// The following case checks whether the given environment includes dev in it or not
17-
case this.env.includes('dev'):
18-
// If 'dev' environment, then will return dev url
19-
return chai.request(process.env.DEV_MAIN);
20-
21-
// The following case checks whether the given environment includes staging in it or not
22-
case this.env.includes('staging'):
23-
// If 'staging' environment, then will return staging url
24-
return chai.request(process.env.STAGING_MAIN);
25-
26-
// The following case checks whether the given environment includes prod in it or not
27-
case this.env.includes('prod'):
28-
// If 'prod' environment, then will return prod url
29-
return chai.request(process.env.PROD_MAIN);
30-
31-
// The default case throws an error when none of the above cases are matched, specifying an invalid environemnt
32-
default:
33-
throw new Error(`Invalid environment specified: ${this.env}`);
34-
}
12+
return chai.request(new config().env().host)
3513
}
3614

3715
// write other services here

test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
async function processItem(item, basePath) {
2+
const helperPath = '../../helper/request.helper';
3+
const mainpagesPath = './main.pages'
4+
5+
if (item.hasOwnProperty('item')) {
6+
const testPath = 'tests/scenarios/' + item.name
7+
const testRelativePath = '../tests/scenarios/' + item.name
8+
const pagesPath = 'tests/pages/' + item.name
9+
const pagesPathRelativePath = '../../pages/' + item.name
10+
const jsonSchemaPath = 'tests/schema/' + item.name
11+
const jsonSchemaRelativePath = '../../schema/' + item.name
12+
let runPath = item.hasOwnProperty('item') ? 'runner/' + item.name : 'runner'
13+
14+
fs.mkdirSync(testPath, { recursive: true })
15+
fs.mkdirSync(pagesPath, { recursive: true })
16+
fs.mkdirSync(jsonSchemaPath, { recursive: true })
17+
fs.mkdirSync(runPath, { recursive: true })
18+
writeRunner(item, testRelativePath, runPath)
19+
20+
asyncForEach(item.item, async (data) => {
21+
if (data.hasOwnProperty('item')) {
22+
for (const innerItem of data.item) {
23+
console.log(innerItem);
24+
await processItem(innerItem, 'tests/' + innerItem.name);
25+
}
26+
} else {
27+
writeTest(data, testPath, pagesPathRelativePath)
28+
writePages(data, pagesPath, jsonSchemaRelativePath, helperPath, mainpagesPath)
29+
writeJsonSchema(data, jsonSchemaPath)
30+
await waitFor(10)
31+
}
32+
})
33+
} else {
34+
const testPath = 'tests/scenarios/'
35+
const testRelativePath = '../tests/scenarios'
36+
const pagesPath = 'tests/pages'
37+
const pagesPathRelativePath = '../pages'
38+
const jsonSchemaPath = 'tests/schema'
39+
const jsonSchemaRelativePath = '../schema'
40+
const runPath = 'runner'
41+
42+
fs.mkdirSync(testPath, { recursive: true })
43+
fs.mkdirSync(pagesPath, { recursive: true })
44+
fs.mkdirSync(jsonSchemaPath, { recursive: true })
45+
fs.mkdirSync(runPath, { recursive: true })
46+
47+
writeRunner(item, testRelativePath, runPath)
48+
writeTest(item, testPath, pagesPathRelativePath)
49+
writePages(item, pagesPath, jsonSchemaRelativePath, helperPath, mainpagesPath)
50+
writeJsonSchema(item, jsonSchemaPath)
51+
await waitFor(10)
52+
}
53+
}
54+
55+
asyncForEach(items, async (element) => {
56+
// Recursively process inner items
57+
const basePath = 'tests';
58+
await processItem(element, basePath);
59+
// }
60+
})

0 commit comments

Comments
 (0)