-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddCodeSamplesToOpenAPI.mjs
More file actions
56 lines (51 loc) · 1.5 KB
/
addCodeSamplesToOpenAPI.mjs
File metadata and controls
56 lines (51 loc) · 1.5 KB
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
import fs from "fs";
// Deep merge function
const deepMerge = (obj1, obj2) => {
for (const key in obj2) {
if (obj2.hasOwnProperty(key)) {
if (
obj1[key] &&
typeof obj1[key] === "object" &&
typeof obj2[key] === "object"
) {
// If both objects have the same key and both are objects, merge them recursively
deepMerge(obj1[key], obj2[key]);
} else {
// Otherwise, just assign the value from obj2 to obj1
obj1[key] = obj2[key];
}
}
}
};
// Read the JSON files asynchronously
fs.readFile("tatrapayplus_api_sandbox.json", "utf8", (err, data1) => {
if (err) {
console.error("Error reading tatrapayplus_api_sandbox.json:", err);
return;
}
fs.readFile("code-samples-map.json", "utf8", (err, data2) => {
if (err) {
console.error("Error reading code-samples-map.json:", err);
return;
}
// Parse the JSON strings into JavaScript objects
const json1 = JSON.parse(data1);
const json2 = JSON.parse(data2);
// Deep merge the two JSON objects
deepMerge(json1, json2);
// Optionally, write the merged JSON to a new file
fs.writeFile(
"tatrapayplus_api_sandbox_final.json",
JSON.stringify(json1, null, 4),
(err) => {
if (err) {
console.error("Error writing merged JSON to file:", err);
} else {
console.log(
"Merged JSON has been saved to 'tatrapayplus_api_sandbox_final.json'",
);
}
},
);
});
});