Skip to content

Instantly share code, notes, and snippets.

@timostamm
Created October 7, 2022 12:38
Show Gist options
  • Save timostamm/2cff5e10131c0c6715a86ff37bce130b to your computer and use it in GitHub Desktop.
Save timostamm/2cff5e10131c0c6715a86ff37bce130b to your computer and use it in GitHub Desktop.
A very simple protobuf plugin, using @bufbuild/protoplugin
version: v1
plugins:
- name: foo
path: ./protoc-gen-foo.ts
out: gen
opt: target=ts
# run `buf generate` to generate code for example.proto
syntax="proto3";
message Foo {
string foo = 1;
bool bar = 2;
}
{
"name": "pluginfoo",
"version": "1.0.0",
"dependencies": {
"@bufbuild/protoplugin": "0.1.1",
"tsx": "^3.10.1"
}
}
#!/usr/bin/env npx tsx
// make sure to make this file executable with `chmod +x protoc-gen-foo.ts`
import {createEcmaScriptPlugin, runNodeJs} from "@bufbuild/protoplugin";
const plugin = createEcmaScriptPlugin({name: "foo", version: "0.0.1"},
schema => {
for (const file of schema.files) {
const openApi = {
messages: [],
};
for (const message of file.messages) {
openApi.messages.push(message.proto.toJson());
}
const f = schema.generateFile(file.name + "_openapi.json");
f.print(JSON.stringify(openApi));
}
});
runNodeJs(plugin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment