forked from launchdarkly-labs/project-migrator-script
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.ts
143 lines (125 loc) · 3.58 KB
/
utils.ts
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
import * as Colors from "https://deno.land/std/fmt/colors.ts";
export async function getJson(filePath: string) {
try {
return JSON.parse(await Deno.readTextFile(filePath));
} catch (e) {
console.log(filePath + ": " + e.message);
}
}
export async function rateLimitRequest(req: Request, path: String) {
return rateLimitRequestLogs(req, path, 1);
}
export async function rateLimitRequestLogs(req: Request, path: String, recurseIndex: number) {
console.log(`Rate Limit call on request ${req.url} for the ${recurseIndex} time`);
const rateLimitReq = req.clone();
const res = await fetch(req);
let newRes = res;
if (res.status == 409 && path == `projects`) {
console.warn(Colors.yellow(`It looks like this project has already been created in the destination`));
console.warn(Colors.yellow(`To avoid errors and possible overwrite, please either:`));
console.warn(Colors.yellow(`Update your name to a new one, or delete the existing project in the destination instance`));
Deno.exit(1);
}
if (res.status == 429) {
const rateLimit = res.headers.get("x-ratelimit-reset");
const end = Number(rateLimit) + 1_000;
const d = new Date(0);
console.log(`${res.statusText}`);
d.setUTCMilliseconds(end);
console.log(`Rate Limited until: ${d} for request ${req.url}`);
while (Date.now() < end);
console.log(`Making new request for request ${req.url}`);
newRes = await rateLimitRequestLogs(rateLimitReq, path, recurseIndex + 1);
}
return newRes;
}
export function ldAPIPostRequest(
apiKey: string,
domain: string,
path: string,
body: any,
) {
const req = new Request(
`https://${domain}/api/v2/${path}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": apiKey,
},
body: JSON.stringify(body),
},
);
return req;
}
export function ldAPIPatchRequest(
apiKey: string,
domain: string,
path: string,
body: any,
) {
const req = new Request(
`https://${domain}/api/v2/${path}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json",
"Authorization": apiKey,
},
body: JSON.stringify(body),
},
);
return req;
}
export function buildPatch(key: string, op: string, value: any) {
return {
path: "/" + key,
op,
value,
};
}
export function buildRules(
rules: any,
env?: string,
): { path: string; op: string; value: any }[] {
const newRules: { path: string; op: string; value: any }[] = [];
const path = env ? `${env}/rules/-` : "rules/-";
rules.map(({ clauses, _id, generation, deleted, version, ref, ...rest }) => {
const newRule = rest;
const newClauses = { clauses: clauses.map(({ _id, ...rest }) => rest) };
Object.assign(newRule, newClauses);
newRules.push(buildPatch(path, "add", newRule));
});
return newRules;
}
export async function writeSourceData(
projPath: string,
dataType: string,
data: any,
) {
return await writeJson(`${projPath}/${dataType}.json`, data);
}
export function ldAPIRequest(apiKey: string, domain: string, path: string) {
const req = new Request(
`https://${domain}/api/v2/${path}`,
{
headers: {
"Authorization": apiKey,
},
},
);
return req;
}
async function writeJson(filePath: string, o: any) {
try {
await Deno.writeTextFile(filePath, JSON.stringify(o));
} catch (e) {
console.log(e);
}
}
export function consoleLogger(status: number, message: string) {
if (status > 201 && status != 429) {
return console.warn(Colors.yellow(message));
}
return console.log(message);
}