Protoc plugin to generate files by your own templates.
This is a plugin for protoc. Install protobuf via Homebrew or chocolatey first.
And you can get this via npm.
npm install protoc-gen-node-template --save-dev
Then you should have the plugin executable in ./node_modules/.bin
.
protoc --plugin=protoc-gen-node-template=./node_modules/.bin/protoc-gen-node-template \
--node-template_out=template=md:./PATH/TO/OUT \
-I ./PATH/TO/PROTO/DIR ./PATH/TO/PROTO/DIR/*.proto
protoc --plugin=protoc-gen-node-template=./node_modules/.bin/protoc-gen-node-template \
--node-template_out=template=json_ts:./PATH/TO/OUT \
-I ./PATH/TO/PROTO/DIR ./PATH/TO/PROTO/DIR/*.proto
You can write your own templates in ejs which is a well-known template engine for Node.js.
I recommend to do this because what you define with the proto file depends on each project.
protoc --plugin=protoc-gen-node-template=./node_modules/.bin/protoc-gen-node-template \
--node-template_out=template=./PATH/TO/template.ejs,ext=md:./PATH/TO/OUT \
-I ./PATH/TO/PROTO/DIR ./PATH/TO/PROTO/DIR/*.proto
- The ejs file under the directory of the specified template file can be implicitly included.
- You can learn more about what variables can be accessed in the template with the
raw
default template below.
protoc --plugin=protoc-gen-node-template=./node_modules/.bin/protoc-gen-node-template \
--node-template_out=template=raw,yourAnyParam=abc:./PATH/TO/OUT \
-I ./PATH/TO/PROTO/DIR ./PATH/TO/PROTO/DIR/*.proto
- Our API server already has proto files and the other clients fetch resources via protobuf binary communication.
- Some of Web (especially browser) developers like me want to communicate via JSON for performance reasons.
- But I really want TypeScript type definition of API which is automatically generated!!
Don't you like me? You can check also...
The author is new to publish npm. Please feel free to make PRs or issues! Thank you for your help! 😄