forked from denoland/deno-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_generate_types.ts
88 lines (69 loc) · 2.38 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
const UNPKG = "https://unpkg.com/@types/[email protected]/";
// Get the index file
const indexReq = await fetch(`${UNPKG}index.d.ts`);
assert(indexReq.ok);
const indexFile = await indexReq.text();
async function extractImports(typesFile: string, baseUrl: string) {
// Extract all imported files from this file
const imports = [...typesFile.matchAll(/\nexport \* from [\"\'](.*)[\"\'];/g)]
.map((
match,
) => [
match,
match[1].replace(/^\.\//, baseUrl).replace(/\/$/, "/index") + ".d.ts",
]);
const files = await Promise.all(imports.map(async (t) => {
const [match, url] = t;
console.log(url.toString());
const req = await fetch(url.toString());
assert(req.ok, "failed to fetch " + url);
const text = await req.text();
if (text.match(/\nexport \* from/g)) {
await extractImports(text, new URL(match[1], baseUrl).href);
}
return [match, text];
}));
for (const t of files) {
typesFile += t[1];
}
return typesFile;
}
let typesFile = await extractImports(indexFile, UNPKG);
console.log([...typesFile.matchAll(/\nexport \* from .*/g)]);
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(/export \* from ["']\..*['"];/g, "");
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",
);
typesFile = "// deno-lint-ignore-file\n" + typesFile;
Deno.writeTextFileSync("./runtime/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(
"./runtime/mod.ts",
`export type {\n ${types.join(",\n ")}\n } from \"./types.d.ts\";\n`,
);
await Deno.run({ cmd: ["deno", "fmt", "runtime/mod.ts", "runtime/types.d.ts"] })
.status();