Skip to content

Commit 1c37e15

Browse files
committed
change to module import
1 parent e835b9d commit 1c37e15

File tree

11 files changed

+99
-79
lines changed

11 files changed

+99
-79
lines changed

lib/component/data.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
const fs = require('fs');
1+
import fs from "fs";
22

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

55
async function writeData() {
6-
76
// Here, a constant variable dataDir is declared and assigned to the string 'tests/data/file'.
87

9-
const dataDir = 'tests/data/file';
8+
const dataDir = "tests/data/file";
109

11-
// The method mkdirSync of the filesystem module (fs) is called with two parameters:
10+
// The method mkdirSync of the filesystem module (fs) is called with two parameters:
1211
// dataDir and { recursive: true }.
1312
// The former specifies the directory path to be created, and the latter enables
14-
// recursive directory creation such that any non-existent parent directories along
13+
// recursive directory creation such that any non-existent parent directories along
1514
// the specified path are also created.
1615

1716
fs.mkdirSync(dataDir, { recursive: true });
1817
}
1918

20-
21-
module.exports = writeData
19+
export default writeData

lib/component/helper.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
1-
const fs = require('fs');
2-
const isFileExisted = require('../utils/check_dir')
3-
const basePath = require('../utils/base_path')
1+
import fs from "fs"
2+
import isFileExisted from "../utils/check_dir.js";
3+
import basePath from "../utils/base_path.js";
44

55
// Asynchronous function to write data into directory
66
async function writeHelper() {
7-
// create helper directory if it doesn't exists
8-
const helperDir = 'tests/helper';
9-
fs.mkdirSync(helperDir, { recursive: true })
7+
// create helper directory if it doesn't exists
8+
const helperDir = "tests/helper";
9+
fs.mkdirSync(helperDir, { recursive: true });
1010

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-
await isFileExisted('tests', 'request.helper.js', function(data) {
14-
if (!data[0] && data[1] == 'tests/helper') {
15-
fs.writeFile('tests/helper/request.helper.js',
16-
fs.readFileSync(basePath() + 'lib/template/requestHelper.dot', 'utf8') , function (err) { if (err) throw err ; });
17-
}
18-
})
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+
await isFileExisted("tests", "request.helper.js", function (data) {
14+
if (!data[0] && data[1] == "tests/helper") {
15+
fs.writeFile(
16+
"tests/helper/request.helper.js",
17+
fs.readFileSync(basePath() + "lib/template/requestHelper.dot", "utf8"),
18+
function (err) {
19+
if (err) throw err;
20+
}
21+
);
22+
}
23+
});
1924

20-
// Check if a file named 'general.helper.js' exists in the tests/helper dir
21-
// If it does not exist then create a new file based on the template file 'generalHelper.dot'
22-
await isFileExisted('tests', 'general.helper.js', function(data) {
23-
if (!data[0] && data[1] == 'tests/helper') {
24-
fs.writeFile('tests/helper/general.helper.js',
25-
fs.readFileSync(basePath() + 'lib/template/generalHelper.dot', 'utf8') , function (err) { if (err) throw err ; });
26-
}
27-
})
25+
// Check if a file named 'general.helper.js' exists in the tests/helper dir
26+
// If it does not exist then create a new file based on the template file 'generalHelper.dot'
27+
await isFileExisted("tests", "general.helper.js", function (data) {
28+
if (!data[0] && data[1] == "tests/helper") {
29+
fs.writeFile(
30+
"tests/helper/general.helper.js",
31+
fs.readFileSync(basePath() + "lib/template/generalHelper.dot", "utf8"),
32+
function (err) {
33+
if (err) throw err;
34+
}
35+
);
36+
}
37+
});
2838
}
2939

30-
31-
module.exports = writeHelper
40+
export default writeHelper

lib/component/json_schema.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const fs = require('fs');
2-
const isFileExisted = require('../utils/check_dir')
3-
const basePath = require('../utils/base_path')
1+
import fs from "fs"
2+
import isFileExisted from "../utils/check_dir.js"
3+
import basePath from "../utils/base_path.js"
44

55
// Json schema file generator
66
async function writeJsonSchema(element, jsonSchemaPath) {
@@ -18,4 +18,4 @@ async function writeJsonSchema(element, jsonSchemaPath) {
1818
})
1919
}
2020

21-
module.exports = writeJsonSchema
21+
export default writeJsonSchema

lib/component/pages.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const fs = require('fs');
2-
const asyncForEach = require('../utils/foreach')
3-
const waitFor = require('../utils/wait')
4-
const isFileExisted = require('../utils/check_dir')
5-
const basePath = require('../utils/base_path')
1+
import fs from "fs"
2+
import asyncForEach from "../utils/foreach.js"
3+
import waitFor from "../utils/wait.js"
4+
import isFileExisted from "../utils/check_dir.js"
5+
import basePath from "../utils/base_path.js"
66

77
// Pages file generator
8-
async function writePages(element, path, jsonSchemaRelativePath, helperPath) {
8+
async function writePages(element, path, jsonSchemaRelativePath, helperPath, mainpagesPath) {
99
let contents = fs.readFileSync(basePath() + 'lib/template/request.dot', 'utf8');
1010

1111
let name = (element.name).toLowerCase().replace(/\s/g, '');
@@ -137,14 +137,15 @@ async function writePages(element, path, jsonSchemaRelativePath, helperPath) {
137137
code = code.replace("{{bodyFunc}}", bodyFunc)
138138
code = code.replace("{{rawAtt}}", attKey)
139139
code = code.replace("{{helperPath}}", helperPath)
140-
140+
code = code.replace("{{mainpagesPath}}", mainpagesPath)
141+
141142
await waitFor(50);
142143

143144
// check if pages file exists
144145
await isFileExisted('tests/pages', 'main.pages.js', function(data) {
145146
if (!data[0] && data[1] == path) {
146147
// create file test
147-
fs.writeFile(path + '/' + 'main.pages.js',
148+
fs.writeFile('tests/pages/main.pages.js',
148149
fs.readFileSync(basePath() + 'lib/template/mainPages.dot', 'utf8'), function (err) { if (err) throw err; });
149150
}
150151
})
@@ -159,4 +160,4 @@ async function writePages(element, path, jsonSchemaRelativePath, helperPath) {
159160
})
160161
}
161162

162-
module.exports = writePages
163+
export default writePages

lib/component/runner.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs');
2-
const asyncForEach = require('../utils/foreach')
3-
const waitFor = require('../utils/wait')
4-
const isFileExisted = require('../utils/check_dir')
1+
import fs from "fs"
2+
import asyncForEach from "../utils/foreach.js"
3+
import waitFor from "../utils/wait.js"
4+
import isFileExisted from "../utils/check_dir.js"
55

66

77
// Runner file generator
@@ -55,4 +55,4 @@ async function writeRunner(element, testPath, runPath) {
5555
})
5656
}
5757

58-
module.exports = writeRunner
58+
export default writeRunner

lib/component/tests.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require('fs');
2-
const waitFor = require('../utils/wait')
3-
const isFileExisted = require('../utils/check_dir')
4-
const basePath = require('../utils/base_path')
1+
import fs from "fs"
2+
import waitFor from "../utils/wait.js"
3+
import isFileExisted from "../utils/check_dir.js"
4+
import basePath from "../utils/base_path.js"
55

66
// Test file generator
77
async function writeTest(element, path, pagesPath) {
@@ -80,4 +80,4 @@ let data = [
8080
})
8181
}
8282

83-
module.exports = writeTest
83+
export default writeTest

lib/generate.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const fs = require('fs');
2-
const asyncForEach = require('./utils/foreach')
3-
const waitFor = require('./utils/wait')
4-
const writeTest = require('./component/tests')
5-
const writePages = require('./component/pages')
6-
const writeRunner = require('./component/runner')
7-
const writeJsonSchema = require('./component/json_schema')
8-
const writeHelper = require('./component/helper')
9-
const writeData = require('./component/data')
1+
import fs from 'fs'
2+
import asyncForEach from './utils/foreach.js'
3+
import waitFor from './utils/wait.js'
4+
import writeTest from './component/tests.js'
5+
import writePages from './component/pages.js'
6+
import writeData from './component/data.js'
7+
import writeRunner from './component/runner.js'
8+
import writeHelper from './component/helper.js'
9+
import writeJsonSchema from './component/json_schema.js'
1010

1111
// main generator
1212
async function generate(file) {
@@ -20,7 +20,7 @@ async function generate(file) {
2020
writeHelper()
2121

2222
// write .example.env file
23-
fs.writeFile('.example.env','APP_URL=your_api_url', function (err) { if (err) throw err ; });
23+
fs.writeFile('.example.env','DEV_MAIN=your_api_url', function (err) { if (err) throw err ; });
2424

2525
asyncForEach(items, async (element) => {
2626
// console.log(element);
@@ -31,6 +31,8 @@ async function generate(file) {
3131
const testPath = 'tests/scenarios/' + element.name;
3232
const testRelativePath = '../tests/scenarios/' + element.name;
3333
fs.mkdirSync(testPath, { recursive: true })
34+
// main pages path
35+
const mainpagesPath = '../main.pages';
3436
// write pages dir
3537
const pagesPath = 'tests/pages/' + element.name;
3638
const pagesPathRelativePath = '../../pages/' + element.name;
@@ -47,7 +49,7 @@ async function generate(file) {
4749
asyncForEach(element.item, async (second) => {
4850
if (second.hasOwnProperty('item') == false) {
4951
writeTest(second, testPath, pagesPathRelativePath)
50-
writePages(second, pagesPath, jsonSchemaRelativePath, helperPath)
52+
writePages(second, pagesPath, jsonSchemaRelativePath, helperPath, mainpagesPath)
5153
writeJsonSchema(second, jsonSchemaPath)
5254
await waitFor(10)
5355
} else {
@@ -59,6 +61,7 @@ async function generate(file) {
5961
const third_sch = jsonSchemaPath + '/' + second.name;
6062
const third_schRe = '../' + jsonSchemaRelativePath + '/' + second.name;
6163
const third_helpRe = '../' + helperPath;
64+
const third_mainRe = '../' + mainpagesPath;
6265
const third_runPath = runPath + '/' + element.name;
6366

6467
fs.mkdirSync(third_test + '/', { recursive: true })
@@ -68,7 +71,7 @@ async function generate(file) {
6871
writeRunner(second, third_testRe, third_runPath)
6972
if (third.hasOwnProperty('item') == false) {
7073
writeTest(third, third_test, third_pageRe)
71-
writePages(third, third_page, third_schRe, third_helpRe)
74+
writePages(third, third_page, third_schRe, third_helpRe, third_mainRe)
7275
writeJsonSchema(third, third_sch)
7376
await waitFor(10)
7477
} else {
@@ -80,6 +83,7 @@ async function generate(file) {
8083
const fourth_sch = third_sch + '/' + third.name;
8184
const fourth_schRe = '../' + third_schRe + '/' + third.name;
8285
const fourth_helpRe = '../' + third_helpRe;
86+
const fourth_mainRe = '../' + third_mainRe;
8387
const fourth_runPath = third_runPath + '/' + second.name;
8488

8589
fs.mkdirSync(fourth_test + '/', { recursive: true })
@@ -89,7 +93,7 @@ async function generate(file) {
8993
writeRunner(third, fourth_testRe, fourth_runPath)
9094
if (fourth.hasOwnProperty('item') == false) {
9195
writeTest(fourth, fourth_test, fourth_pageRe)
92-
writePages(fourth, fourth_page, fourth_schRe, fourth_helpRe)
96+
writePages(fourth, fourth_page, fourth_schRe, fourth_helpRe, fourth_mainRe)
9397
writeJsonSchema(fourth, fourth_sch)
9498
await waitFor(10)
9599
} else {
@@ -101,6 +105,7 @@ async function generate(file) {
101105
const fifth_sch = fourth_sch + '/' + fourth.name;
102106
const fifth_schRe = '../' + fourth_schRe + '/' + fourth.name;
103107
const fifth_helpRe = '../' + fourth_helpRe;
108+
const fifth_mainRe = '../' + fourth_mainRe;
104109
const fifth_runPath = fourth_runPath + '/' + third.name;
105110

106111
fs.mkdirSync(fifth_test + '/', { recursive: true })
@@ -111,7 +116,7 @@ async function generate(file) {
111116
await waitFor(50)
112117
if (fifth.hasOwnProperty('item') == false) {
113118
writeTest(fifth, fifth_test, fifth_pageRe)
114-
writePages(fifth, fifth_page, fifth_schRe, fifth_helpRe)
119+
writePages(fifth, fifth_page, fifth_schRe, fifth_helpRe, fifth_mainRe)
115120
writeJsonSchema(fifth, fifth_sch)
116121
await waitFor(10)
117122
} else {
@@ -123,6 +128,7 @@ async function generate(file) {
123128
const sixth_sch = fifth_sch + '/' + fifth.name;
124129
const sixth_schRe = '../' + fifth_schRe + '/' + fifth.name;
125130
const sixth_helpRe = '../' + fifth_helpRe;
131+
const sixth_mainRe = '../' + fifth_mainRe;
126132
const sixth_runPath = fifth_runPath + '/' + third.name;
127133

128134
fs.mkdirSync(sixth_test + '/', { recursive: true })
@@ -133,7 +139,7 @@ async function generate(file) {
133139
await waitFor(50)
134140
if (sixth.hasOwnProperty('item') == false) {
135141
writeTest(sixth, sixth_test, sixth_pageRe)
136-
writePages(sixth, sixth_page, sixth_schRe, sixth_helpRe)
142+
writePages(sixth, sixth_page, sixth_schRe, sixth_helpRe, sixth_mainRe)
137143
writeJsonSchema(sixth, sixth_sch)
138144
await waitFor(10)
139145
}
@@ -153,6 +159,8 @@ async function generate(file) {
153159
const testPath = 'tests/scenarios';
154160
const testRelativePath = '../tests/scenarios';
155161
fs.mkdirSync(testPath, { recursive: true })
162+
// main pages path
163+
const mainpagesPath = './main.pages';
156164
// write pages dir
157165
const pagesPath = 'tests/pages';
158166
const pagesPathRelativePath = '../pages';
@@ -167,12 +175,12 @@ async function generate(file) {
167175

168176
writeRunner(element, testRelativePath, runPath)
169177
writeTest(element, testPath, pagesPathRelativePath)
170-
writePages(element, pagesPath, jsonSchemaRelativePath, helperPath)
178+
writePages(element, pagesPath, jsonSchemaRelativePath, helperPath, mainpagesPath)
171179
writeJsonSchema(element, jsonSchemaPath)
172180
await waitFor(10)
173181
}
174182
});
175183
})
176184
}
177185

178-
module.exports = generate
186+
export default generate

lib/utils/base_path.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
function base() {
2-
return './node_modules/test-generator-hadi/'
3-
// return ''
1+
function basePath() {
2+
const env = 'prod'
3+
if(env == 'dev') {
4+
return ''
5+
} else {
6+
return './node_modules/dot-generator-testing/'
7+
}
48
}
59

6-
module.exports = base
10+
export default basePath

lib/utils/check_dir.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs');
1+
import fs from 'fs'
22

33
// check if any file exist
44
async function isFileExisted(path, fileName, res) {
@@ -99,4 +99,4 @@ async function isFileExisted(path, fileName, res) {
9999
}
100100
}
101101

102-
module.exports = isFileExisted
102+
export default isFileExisted

lib/utils/foreach.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ async function asyncForEach(array, callback) {
44
}
55
}
66

7-
module.exports = asyncForEach
7+
export default asyncForEach

0 commit comments

Comments
 (0)