-
Notifications
You must be signed in to change notification settings - Fork 1
/
CodeGenHelper.js
190 lines (162 loc) · 7.18 KB
/
CodeGenHelper.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var fs = require('fs');
const mustache = require('mustache');
const path = require('path');
const chalk = require('chalk');
module.exports = class CodeGenHelper {
static buildFileNameFromName(outputPath, requestName, pathMap) {
var path = pathMap[requestName];
requestName = requestName.replaceAll('/', '_');
return outputPath + path + `${requestName}.json`
}
static buildMustacheHashFromEnvFile(env) {
var resultHash = {};
if (env) {
env.values.forEach(function(value){
resultHash[value.key] = value.value;
});
}
//console.log("resultHash: " + JSON.stringify(resultHash));
return resultHash;
}
static buildMockStubs(options, collection) {
var templateContents = fs.readFileSync(options.mustache);
var managerTemplateContents = fs.readFileSync(options.managerTemplate, 'utf8');
var envContents = JSON.parse(fs.readFileSync(options.environment, 'utf8'));
var collectionContents = fs.readFileSync(collection);
var mustacheHash = this.buildMustacheHashFromEnvFile(envContents);
collectionContents = this.fixPostmanCollectionTokensForHash(collectionContents, mustacheHash);
var env_collection_combined = mustache.render(collectionContents.toString(), this.buildMustacheHashFromEnvFile(envContents));
env_collection_combined = this.addPathToHash(JSON.parse(env_collection_combined));
env_collection_combined = this.addLastPathToHash(env_collection_combined);
env_collection_combined = this.addLastHeaderToHash(env_collection_combined);
console.log("final:")
env_collection_combined.item = this.fixVariableNamesForSwift(env_collection_combined.item);
//console.log(templateContents.toString());
//console.log(env_collection_combined.item);
env_collection_combined.item = this.addHeaderResponsesToRequests(env_collection_combined.item, options.headers);
env_collection_combined.item = this.markLastHeader(env_collection_combined.item);
var combinedTemplateAndCollection = mustache.render(templateContents.toString(), env_collection_combined.item);
//console.log(combinedTemplateAndCollection);
var collectionName = this.getCollectionName(env_collection_combined);
console.log(env_collection_combined.item);
fs.writeFile(options.output + "/MockNetworkRequestManager+" + collectionName + "." + options.extension, combinedTemplateAndCollection, function (error) {
if (error) { console.error("error writing output" + error); }
console.log(chalk.green(`📼Saved MockNetworkRequestManager+${collectionName}.${options.extension} to ` + options.requestManager + "/MockNetworkRequestManager+" + collectionName + "." + options.extension + " 📼"));
});
fs.writeFile(options.output + "/MockNetworkRequestManager." + options.extension, managerTemplateContents, function (error) {
if (error) { console.error("error writing output" + error); }
console.log(chalk.green(`📼Saved MockNetworkRequestManager.${options.extension} to ` + options.output + "/MockNetworkRequestManager." + options.extension + "📼"));
});
}
static getCollectionName(hash) {
if (hash) {
return hash.info.name;
}
}
static addHeaderResponsesToRequests(hash, headers) {
if (hash) {
var index = 0;
for (var item in hash) {
hash[item].responseHeader = headers[index];
index++;
}
}
return hash;
}
static fixVariableNamesForSwift(hash) {
if (hash) {
for (var item in hash) {
hash[item].name = hash[item].name.replaceAll('/', '');
hash[item].name = hash[item].name.replaceAll('-', '');
}
}
return hash;
}
static markLastHeader(hash) {
if (hash) {
for (var item in hash) {
var headers = hash[item].responseHeader;
if (headers.length > 0) {
for (var i in headers) {
console.log(headers[i].value);
headers[i].value = headers[i].value.replaceAll("\"", "'");
console.log(headers[i].value);
headers[i].last = false;
}
headers[headers.length-1].last = true;
}
}
}
return hash;
}
static fixPostmanCollectionTokensForHash(collection, hash) {
if (hash) {
Object.keys(hash).forEach(function(key) {
try {
var possObj = hash[key];
if (typeof possObj == 'object') {
collection = collection.toString().replace('"{{' + key + '}}"', "{{" + key + "}}" );
}
} catch (e) {
console.log(e);
}
});
}
return collection;
}
static addPathToHash(hash) {
var items = hash.item;
//console.log("addToPath: " + items);
for (var i in items) {
if (items[i].request != undefined) {
if (items[i].request.url != undefined) {
if (items[i].request.url.path != undefined) {
// console.log("path strings: "+ items[i].request.url.path.join('/'));
items[i].request.url.joinedPath = "/" + path.join(items[i].request.url.path.join('/'));
}
}
}
}
hash.item = items;
// console.log("path components: " + hash);
return hash;
}
static addLastPathToHash(hash) {
var items = hash.item;
// console.log("addToPath: " + items);
for (var i in items) {
if (items[i].request != undefined) {
if (items[i].request.url != undefined) {
if (items[i].request.url.path != undefined) {
// console.log("path strings: "+ items[i].request.url.path.join('/'))
}
items[i].last = items.length - 1 == i;
}
}
}
hash.item = items;
//console.log("last: " + JSON.stringify(hash));
return hash;
}
static addLastHeaderToHash(hash) {
var items = hash.item;
for (var i in items) {
if (items[i].request != undefined) {
if (items[i].request.url != undefined) {
if (items[i].request.header != undefined) {
for (var j in items[i].request.header) {
items[i].request.header[j].last = items[i].request.header.length - 1 == j;
}
}
}
}
}
hash.item = items;
//console.log("last: " + JSON.stringify(hash));
return hash;
}
}
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};