-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_types.ts
89 lines (79 loc) · 2.23 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
89
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as path from "std/path/mod.ts";
import { Project } from "ts_morph/mod.ts";
import { name, paramDesc } from "mtkruto/mod.ts";
import { typeNameMap } from "./tl_json.ts";
const ROOT = import.meta.dirname + "";
const CLIENT = path.join(ROOT, "client");
const TYPES = path.join(CLIENT, "types.ts");
const ENUMS = path.join(CLIENT, "enums.ts");
const FUNCTIONS = path.join(CLIENT, "functions.ts");
try {
Deno.removeSync(TYPES);
} catch {
//
}
try {
Deno.removeSync(ENUMS);
} catch {
//
}
try {
Deno.removeSync(FUNCTIONS);
} catch {
//
}
const project = new Project();
function getType(type: any): any {
if (type == Uint8Array) {
return "Uint8Array";
} else if (Array.isArray(type)) {
return `${getType(type[0])}[]`;
} else if (typeof type === "function" && name in type) {
let n = type.name;
if (n.startsWith("_")) {
n = n.slice(1);
}
if (n.endsWith("_")) {
n = n.slice(0, -1);
}
return "enum_" + n;
} else {
return type;
}
}
const typesFile = project.createSourceFile(TYPES);
for (let [k, v] of Object.entries(typeNameMap)) {
k = "type_" + k.replace(".", "_");
const params = v[paramDesc] as any[];
typesFile.addInterface({
name: k,
isExported: true,
properties: params
.filter(([, , ntype]) => ntype != "#")
.map(([name, type, ntype]) => ({
name,
hasQuestionToken: type == "true" && ntype.includes("."),
type: getType(type),
})),
});
}
project.saveSync();