Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can this be used with buf? #93

Closed
atreya2011 opened this issue Apr 1, 2021 · 6 comments
Closed

Can this be used with buf? #93

atreya2011 opened this issue Apr 1, 2021 · 6 comments
Labels
question Further information is requested

Comments

@atreya2011
Copy link

Thank you for making this great tool for generating idiomatic TypeScript code from proto files along with RPC support as well!
I was wondering how to use this with buf. Any advice would be helpful. Thanks in advance 🙇🏼

@atreya2011 atreya2011 changed the title Can this tool be used with buf? Can this be used with buf? Apr 1, 2021
@timostamm
Copy link
Owner

Yes, protobuf-ts is a regular protoc plugin and can be used with buf.

Example buf.gen.yaml:

version: v1beta1

# The plugins to run.
plugins:

    # The name of the plugin.
    # Required.
    # By default, buf generate will look for a binary named protoc-gen-NAME on your $PATH.
  - name: ts

    # The the relative output directory.
    # Required.
    out: gen/ts

    # See "available plugin parameters"
    # https://github.com/timostamm/protobuf-ts/blob/master/MANUAL.md#the-protoc-plugin
    opt: generate_dependencies,long_type_string

    # The custom path to the plugin binary, if not protoc-gen-NAME on your $PATH.
    # Assuming you have installed @protobuf-ts/plugin in the working directory:
    path: ./node_modules/@protobuf-ts/plugin/bin/protoc-gen-ts

See https://docs.buf.build/generate-usage

If you encounter any problems, let me know.

@atreya2011
Copy link
Author

atreya2011 commented Apr 1, 2021

Thank you for the quick response! That worked 🎉
I was not sure about the exact location of the plugin binary. I was using ./node_modules/.bin/protoc-gen-ts which did not work :(
Please correct me if I am wrong, it looks like with npm install @protobuf-ts/plugin, this tool also installs the protoc binary in node_modules/.bin and the plugin calls this protoc binary to generate the TypeScript files?

@atreya2011
Copy link
Author

atreya2011 commented Apr 1, 2021

As a next step, I am planning to try to package the generated files into an npm package to distribute to clients. If all goes well, is it ok to make a PR with an example where buf is used to generate the client.

@timostamm
Copy link
Owner

node_modules/.bin/protoc is provided by the package @protobuf-ts/protoc (which gets automatically installed together with @protobuf-ts/plugin).

It is just a simple wrapper written in Javascript for the real protocol buffer compiler protoc. It automatically downloads the latest release of the official protoc for the current platform and invokes it. In effect, you can run npx protoc and do not have to manually install the compiler. If you already have protoc on your PATH, it uses that one instead of downloading.

Code generation always starts with protoc, the protocol buffer compiler. The compiler supports "plugins". If you invoke protoc --foo_out gen/, protoc looks for a plugin executable named protoc-gen-foo in your PATH. It parses the given proto files and sends the descriptors in a CodeGeneratorRequest to the STDIN of the plugin executable and expects the plugin to write a CodeGeneratorResponse (containing the generated code) to STDOUT. @protobuf-ts/plugin provides protoc-gen-ts which implements this plugin contract.

@timostamm
Copy link
Owner

is it ok to make a PR with an example where buf is used to generate the client.

That would be nice!

Just add packages/example-buf/ with a README

@atreya2011
Copy link
Author

atreya2011 commented Apr 2, 2021

Thank you for the detailed explanation! I am still trying to wrap my head around the gRPC ecosystem and how everything ties together 😅
And understood regarding the PR 🙇🏼

@timostamm timostamm added the question Further information is requested label Apr 10, 2021
@timostamm timostamm pinned this issue Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants