-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
71 lines (63 loc) · 1.6 KB
/
index.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
import { InputStream } from "./charInputStream";
import { TokenStream } from "./tokenStream";
import Fs from "node:fs/promises";
function rustTypeToTypeScript(type: string) {
return {
String: "string",
u64: "number",
DatabaseConstraint: "/* @todo */ object",
}[type];
}
async function run() {
const file = await Fs.readFile(__dirname + "/input.rs", "utf8");
const inputStream = InputStream(file);
const tokenStream = TokenStream(inputStream);
const ast = tokenStream.read();
const types = ast.map(({ struct, pragma }) => {
const code = pragma.pragmaParams.find((p) => p.paramName === "code");
const message = pragma.pragmaParams.find((p) => p.paramName === "message");
return `
/**
* ${message?.paramValue}
*/
export interface ${
struct.structName
} extends Prisma.PrismaClientKnownRequestError {
code: "${code?.paramValue}";
meta: {
${struct.structFields
.map((s) =>
`
${
s.fieldComment
? `
/**
* ${s.fieldComment}
*/`
: ``
}
${s.fieldName}: ${rustTypeToTypeScript(s.fieldType)};
`.trim()
)
.join("\n")}
};
}
`.trim();
});
const union =
`export type PrismaErrors = ` +
ast.map(({ struct }) => struct.structName).join(" | ") +
`;`;
const enum_ = `export enum PrismaErrorCode {${ast
.map(({ struct, pragma }) => {
const code = pragma.pragmaParams.find((p) => p.paramName === "code");
return `${struct.structName} = "${code?.paramValue}",`;
})
.join("\n")}}`;
await Fs.writeFile(
__dirname + "/out.ts",
types.join("\n") + "\n\n" + union + "\n\n" + enum_,
"utf-8"
);
}
run();