-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
124 lines (109 loc) · 3.5 KB
/
start.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const { camelCase } = require('change-case');
const handlebars = require('handlebars');
const path = require('path');
const {
loadPostman,
saveNewPostmanData,
deleteOldRepoDirectory,
openFile,
createDirectory
} = require('./utility');
const getPostmanV1 = require('./get_postman_v1');
const getPostmanV2 = require('./get_postman_v2');
const {
POSTMAN_LOCAL_PATH,
OUT_PUT,
REPOSITORY_URL
} = require('./constans');
// define global variables
let requestTemplateString;
let indexTemplateString;
let requestGroupTemplateString;
let requestTemplate;
let indexTemplate;
let requestGroupTemplate;
let isV1 = false;
// define handlebars helper function
handlebars.registerHelper('ifEquals', function (a, b, opts) {
if (a === b) {
return opts.fn(this);
}
return opts.inverse(this);
});
handlebars.registerHelper('camelCase', (name) => camelCase(name));
// open package.json file
// const PACKAGE_JSON = JSON.parse(openFile(PACKAGE_JSON_PATH));
// get config from package.json -> postman
// const outputPath = PACKAGE_JSON.postman.target;
const start = async (postmanData) => {
try {
isV1 = !!postmanData?.folders;
// open template files and store in globals
requestTemplateString = await openFile(
'/generate-postman/templates/request-template.hbs'
);
indexTemplateString = await openFile(
'/generate-postman/templates/index-template.hbs'
);
requestGroupTemplateString = await openFile(
'/generate-postman/templates/request-group-template.hbs'
);
// make template
requestTemplate = await handlebars.compile(requestTemplateString);
indexTemplate = await handlebars.compile(indexTemplateString);
requestGroupTemplate = await handlebars.compile(requestGroupTemplateString);
// cleaning old repository if exist
await deleteOldRepoDirectory(OUT_PUT);
// create new root directory for repository folder
await createDirectory(OUT_PUT);
if (isV1) {
console.log('####################################');
console.log('##### #####');
console.log('#####----> going V1 <----#####');
console.log('##### #####');
console.log('####################################\n\n');
await getPostmanV1({
indexTemplate,
outputPath: OUT_PUT,
postmanData,
requestGroupTemplate,
requestTemplate
});
} else {
console.log('####################################');
console.log('##### #####');
console.log('#####----> going V2 <----#####');
console.log('##### #####');
console.log('####################################\n\n');
await getPostmanV2({
indexTemplate,
outputPath: OUT_PUT,
postmanData: postmanData.item,
requestGroupTemplate,
requestTemplate
});
}
} catch (e) {
console.log('start -> catch -> ', e);
}
};
loadPostman(REPOSITORY_URL)
.then((newPostmanData) => {
// start after response
start(newPostmanData);
saveNewPostmanData(newPostmanData);
})
.catch((e) => {
console.log('\n\n ##-> start => get postman failed');
console.log('##-> start => e -> ', e);
console.log('\n\n ##-> start postman from local');
// open local postman async
try {
openFile(POSTMAN_LOCAL_PATH)
.then((data) => {
start(JSON.parse(data));
});
} catch (err) {
console.log('##-> start postman from local was error => e -> ', err);
}
});