Skip to content

TasukuUno/protoc-gen-node-template

Repository files navigation

image

Protoc plugin to generate files by your own templates.

Installation

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 .

Use with default templates

*.proto => *.md

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

See also

*.proto => *.ts interfaces for JSON communication

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

See also

Use with your custom templates (recommended)

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

See also

  • 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

See also

Motivation

motivation

  • 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!!

You can check also...

Don't you like me? You can check also...

Contributing

The author is new to publish npm. Please feel free to make PRs or issues! Thank you for your help! 😄