Created
October 7, 2022 12:38
-
-
Save timostamm/2cff5e10131c0c6715a86ff37bce130b to your computer and use it in GitHub Desktop.
A very simple protobuf plugin, using @bufbuild/protoplugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: v1 | |
plugins: | |
- name: foo | |
path: ./protoc-gen-foo.ts | |
out: gen | |
opt: target=ts | |
# run `buf generate` to generate code for example.proto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax="proto3"; | |
message Foo { | |
string foo = 1; | |
bool bar = 2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "pluginfoo", | |
"version": "1.0.0", | |
"dependencies": { | |
"@bufbuild/protoplugin": "0.1.1", | |
"tsx": "^3.10.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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