-
Notifications
You must be signed in to change notification settings - Fork 41
/
generate_types.ts
67 lines (52 loc) · 1.79 KB
/
generate_types.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
import { assert } from "https://deno.land/std@0.77.0/testing/asserts.ts";
const unpkg = "https://unpkg.com/@types/aws-lambda@8.10.63/";
// Get the index file
const indexReq = await fetch(`${unpkg}index.d.ts`);
assert(indexReq.ok);
const indexFile = await indexReq.text();
// Extract all imported files from this file
const imports = [...indexFile.matchAll(/\nexport \* from \"(.*)\";/g)].map((
match,
) => match[1].replace(/^\.\//, unpkg).replace(/\/$/, "/index") + ".d.ts");
let typesFile = (indexFile.split("3.0")[0] + "3.0\n\n");
const files = await Promise.all(imports.map(async (url) => {
const req = await fetch(url);
assert(req.ok, "failed to fetch " + url);
return req.text();
}));
for (const file of files) {
typesFile += file;
}
typesFile = typesFile.replaceAll(
/\nimport {(.|\n)*?} from ["'](.*?)["'];?/g,
"",
);
typesFile = typesFile.replace(" callback: Callback<TResult>,\n", "");
typesFile = typesFile.replace("void | Promise<TResult>", "Promise<TResult>");
typesFile = typesFile.replace(
/\n\/\*\*\n \* NodeJS-style(.|\n)*?\nexport type Callback(.|\n)*?;/,
"",
);
typesFile = typesFile.replaceAll(
/\nexport type (.*?) =[\n ]*Callback<(.*?)>;/g,
"",
);
typesFile = typesFile.replaceAll(
/\nexport type (.*?)Callback = .*?;/g,
"",
);
typesFile = typesFile.replaceAll(
/\n\n\/\/(.*?)\n\n/g,
"\n\n",
);
Deno.writeTextFileSync("./types.d.ts", typesFile);
// This file generates mod.ts from types.d.ts
const types = [...typesFile.matchAll(/export (type|interface) (.*?)\s/g)].map((
match,
) => match[2].replaceAll(/<(.*)$/g, "")).filter((t) => !t.includes("Callback"))
.sort();
Deno.writeTextFileSync(
"./mod.ts",
`export type {\n ${types.join(",\n ")}\n } from \"./types.d.ts\";\n`,
);
await Deno.run({ cmd: ["deno", "fmt", "mod.ts", "types.d.ts"] }).status();