forked from coinhall/cosmes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprotoc-gen-cosmes.mjs
executable file
·50 lines (45 loc) · 1.4 KB
/
protoc-gen-cosmes.mjs
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
#!/usr/bin/env node
// @ts-check
/**
* This is a custom plugin for `buf` that generates TS files from the services
* defined in the proto files, and is referred to by the root `buf.gen.yaml`.
* Files generated using this plugin contains the `_cosmes` suffix.
*
* Do not convert this to a TS file as it runs 4x slower!
*/
import { createEcmaScriptPlugin, runNodeJs } from "@bufbuild/protoplugin";
import {
literalString,
localName,
makeJsDoc,
} from "@bufbuild/protoplugin/ecmascript";
export function generateTs(schema) {
for (const protoFile of schema.files) {
const file = schema.generateFile(protoFile.name + "_cosmes.ts");
file.preamble(protoFile);
for (const service of protoFile.services) {
generateService(schema, file, service);
}
}
}
function generateService(schema, f, service) {
f.print("const TYPE_NAME = ", literalString(service.typeName), ";");
f.print("");
for (const method of service.methods) {
f.print(makeJsDoc(method));
f.print("export const ", localName(service), method.name, "Service = {");
f.print(" typeName: TYPE_NAME,");
f.print(" method: ", literalString(method.name), ",");
f.print(" Request: ", method.input, ",");
f.print(" Response: ", method.output, ",");
f.print("} as const;");
f.print("");
}
}
runNodeJs(
createEcmaScriptPlugin({
name: "protoc-gen-cosmes",
version: "v0.0.1",
generateTs,
})
);