forked from launchdarkly-labs/project-migrator-script
-
Notifications
You must be signed in to change notification settings - Fork 3
/
source.ts
81 lines (71 loc) · 2.02 KB
/
source.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
import yargs from "https://cdn.deno.land/yargs/versions/yargs-v16.2.1-deno/raw/deno.ts";
import {
ensureDir,
ensureDirSync,
} from "https://deno.land/[email protected]/fs/mod.ts";
import { consoleLogger, ldAPIRequest, writeSourceData } from "./utils.ts";
interface Arguments {
projKey: string;
apikey: string;
domain: string;
}
let inputArgs: Arguments = yargs(Deno.args)
.alias("p", "projKey")
.alias("k", "apikey")
.alias("u", "domain")
.default("u", "app.launchdarkly.com").argv;
// Project Data //
const projResp = await fetch(
ldAPIRequest(
inputArgs.apikey,
inputArgs.domain,
`projects/${inputArgs.projKey}?expand=environments`,
),
);
if (projResp == null) {
console.log("Failed getting project");
Deno.exit(1);
}
const projData = await projResp.json();
const projPath = `./source/project/${inputArgs.projKey}`;
ensureDirSync(projPath);
await writeSourceData(projPath, "project", projData);
// Segment Data //
if (projData.environments.items.length > 0) {
projData.environments.items.forEach(async (env: any) => {
console.log(env.key);
const segmentResp = await fetch(
ldAPIRequest(
inputArgs.apikey,
inputArgs.domain,
`segments/${inputArgs.projKey}/${env.key}`,
),
);
if (segmentResp == null) {
console.log("Failed getting Segments");
Deno.exit(1);
}
const segmentData = await segmentResp.json();
await writeSourceData(projPath, `segment-${env.key}`, segmentData);
const end = Date.now() + 2_000;
while (Date.now() < end);
});
}
// Flag Data //
const flagResp = await fetch(
ldAPIRequest(
inputArgs.apikey,
inputArgs.domain,
`flags/${inputArgs.projKey}?summary=false`,
),
);
if (flagResp.status > 201) {
consoleLogger(flagResp.status, `Error getting flags: ${flagResp.status}`);
consoleLogger(flagResp.status, await flagResp.text());
}
if (flagResp == null) {
console.log("Failed getting Flags");
Deno.exit(1);
}
const flagData = await flagResp.json();
await writeSourceData(projPath, "flag", flagData);