forked from TinkerWorX/war3-types-strict
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
178 lines (147 loc) · 5.26 KB
/
build.ts
File metadata and controls
178 lines (147 loc) · 5.26 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
console.time('build');
console.log("Generating definitions");
import * as fs from "fs";
type typeDeclaration = {
description?: string,
name: string,
extends: string
}
type globalDeclaration = {
description?: string,
isConstant: boolean,
type: string,
isArray: boolean,
value: string,
isNullable: boolean
}
type params = {
description?: string,
name: string,
type: string,
isNullable: boolean
}
type nativeDeclaration = {
description?: string,
takes: Array<params>,
returns: string,
isNullable: boolean
}
type functionDeclaration = {
description?: string,
takes: Array<params>,
returns: string,
isNullable: boolean
}
type data = {
types: Record<string, typeDeclaration>,
globals: Record<string, globalDeclaration>,
natives: Record<string, nativeDeclaration>,
functions: Record<string, functionDeclaration>
}
const filePaths: Set<string> = new Set();
for (let i = 2; i < process.argv.length; i++) {
filePaths.add("./" + process.argv[i]);
}
for (const inPath of filePaths) {
const outPath = inPath.replace(".json", ".d.ts");
console.timeLog('build', inPath);
const data: data = require(inPath);
const stream = fs.createWriteStream(outPath);
stream.write("/** @noSelfInFile */\n\n");
// Types
let keys = Object.keys(data.types);
if (keys.length > 0) {
stream.write("// ===============\n");
stream.write("// ==== TYPES ====\n");
stream.write("// ===============\n");
for (const key of keys) {
const type = data.types[key];
stream.write(`declare interface ${type.name} extends ${type.extends} { __${type.name}: never; }\n`);
}
stream.write("\n")
}
// Globals
keys = Object.keys(data.globals);
if (keys.length > 0) {
stream.write("// =================\n");
stream.write("// ==== GLOBALS ====\n");
stream.write("// =================\n");
for (const key of keys) {
const global = data.globals[key];
stream.write(`declare ${global.isConstant ? "const" : "var"} ${key}`);
if (global.isArray) {
stream.write(`: Record<number, ${global.type}${global.isNullable ? " | undefined" : ""}>;`);
}
else {
stream.write(`: ${global.type}${global.isNullable ? " | undefined" : ""};`);
}
stream.write("\n");
}
stream.write("\n");
}
// Natives
keys = Object.keys(data.natives);
if (keys.length > 0) {
stream.write("// =================\n");
stream.write("// ==== NATIVES ====\n");
stream.write("// =================\n");
for (const key of keys) {
const native = data.natives[key];
stream.write(`declare function ${key}(`);
if (native.takes.length > 0) {
const isDefaultable: Array<boolean> = [];
let allowDefault = true;
for (let i = native.takes.length - 1; i >= 0; i--) {
isDefaultable[i] = allowDefault = allowDefault && native.takes[i].isNullable;
}
stream.write(
native.takes.map((param, i) => {
if (param.isNullable) {
if (isDefaultable[i]) {
return `${param.name}${param.isNullable ? "?" : ""}: ${param.type}`;
} else {
return `${param.name}: ${param.type} | undefined`;
}
}
return `${param.name}: ${param.type}`;
}).join(", ")
)
}
stream.write(`): ${native.returns}${native.isNullable ? " | undefined" : ""};\n`);
}
stream.write("\n");
}
// Functions
keys = Object.keys(data.functions);
if (keys.length > 0) {
stream.write("// ===================\n");
stream.write("// ==== FUNCTIONS ====\n");
stream.write("// ===================\n");
for (const key of keys) {
const func = data.functions[key];
stream.write(`declare function ${key}(`);
if (func.takes.length > 0) {
const isDefaultable: Array<boolean> = [];
let allowDefault = true;
for (let i = func.takes.length - 1; i >= 0; i--) {
isDefaultable[i] = allowDefault = allowDefault && func.takes[i].isNullable;
}
stream.write(
func.takes.map((param, i) => {
if (param.isNullable) {
if (isDefaultable[i]) {
return `${param.name}${param.isNullable ? "?" : ""}: ${param.type}`;
} else {
return `${param.name}: ${param.type} | undefined`;
}
}
return `${param.name}: ${param.type}`;
}).join(", ")
)
}
stream.write(`): ${func.returns}${func.isNullable ? " | undefined" : ""};\n`);
}
}
stream.end();
}
console.timeEnd('build')