Coder Social home page Coder Social logo

node-protoc-plugin's Introduction

node-protoc-plugin

Create protoc code-generation plugins easily in nodejs

installation

npm i -S protoc-plugin

usage

You can checkout the code in the example/ dir, but here is a quick example:

#! /usr/bin/env node
const protocPlugin = require('protoc-plugin')

protocPlugin(protos => {
  // do stuff here with protos
  // return array like [{name: 'filename', content: 'CONTENTS'}]
})

Make sure not to output anything to stdout (for example with console.log) because protoc uses stdout. I use console.error to output stuff to the user.

Since it's a promise, you can throw or just return Promise.reject('reason'), and you can do async stuff with promises.

Once you have made your plugin, save it as protoc-gen-NAME, give it executable permissions, then run it like this:

protoc --plugin=protoc-gen-NAME --NAME_out=generated yourfile.proto

If you put it in your path, you don't need the --plugin=protoc-gen-NAME part.

PRO TIP - use npm's bin in your package.json to get your plugin script installed, cross-platform, in the user's path.

findCommentByPath

There is a utility included for finding comments in various places in the protobuf file. It's a lil obtuse, but you can look in the spec for more info.

Here are some locationList addresses I use a lot in protoc plugins:

 * [4, m] - message comments
 * [4, m, 2, f] - field comments in message
 * [6, s] - service comments
 * [6, s, 2, r] - rpc comments in service

where:

  • m - the method count in the proto, from index 0
  • f - the field-count in the method, from index 0
  • s - the service definition in the proto, from index 0
  • r - the RPC definition in the service, from index 0

like this:

// [4, 0] is right here 
message MyMessage {
  // [4, 0, 2, 0] is right here
  int32 field1 = 1;
}

// [6, 0] is right here
service MyService {
  // [6, 0, 2, 0] is here!
  rpc (MyMessage) returns (MyMessage);
}

There are more addresses, but you will have to look at the the spec to figure it out.

usage

const protocPlugin = require('protoc-plugin')
const findCommentByPath = protocPlugin.findCommentByPath

// output comments for services & messages to stderr
protocPlugin(protos => {
  protos.forEach(proto => {
    proto.serviceList.forEach((service, s) => {
      console.error('SERVICE', service.name, findCommentByPath([6, s], proto.sourceCodeInfo.locationList))
      service.methodList.forEach((rpc, r) => {
        console.error('RPC', rpc.name, findCommentByPath([6, s, 2, r], proto.sourceCodeInfo.locationList))
      })
    })
    proto.messageList.forEach((message, m) => {
      console.error('MESSAGE', message.name, findCommentByPath([4, m], proto.sourceCodeInfo.locationList))
      message.fieldList.forEach((field, f) => {
        console.error('FIELD', field.name, findCommentByPath([4, m, 2, f], proto.sourceCodeInfo.locationList))
      })
    })
  })
  
  // no files written
  return []
})

advanced usage

If you need more from the incoming stdin CodeGeneratorRequest have a look at example/protoc-gen-extendedlogger.

extensions

I am currently including google/api/annotations proto file, so gRPC-annotions will work out of the box (for example see proto/helloworld.proto) For any other extensions, you will need to generate the google-protobuf representation, and require it before parsing. You can easily generate them with a command like this:

protoc --js_out=import_style=commonjs,binary:YOURDIR/ -I PROTODIR/ PROTODIR/YOURFILE.proto

then require like this:

require('./NAMESPACE_pb')

You can see how I have done this with google/api/annotations_pb in index.js.

node-protoc-plugin's People

Contributors

konsumer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

node-protoc-plugin's Issues

Convert to TypeScript

Hello ๐Ÿ‘‹

I am one of the maintainers of improbable-eng/grpc-web and improbable-eng/ts-protoc-gen. I am interested in integrating protoc-gen into our toolchain but the lack of TypeScript definitions is currently blocking this. I currently see two path forward:

  1. Convert this project (node-protoc-plugin) to be written in TypeScript
  2. Create (and maintain) an independent set of TypeScript definitions as part of the DefinitelyTyped repo.

Option 1 would be my preference as it's easier to maintain (option 2 can easily result in the definitions going out of sync with this package) - I'd be happy to put in the work (raise the PR) to convert the project; however this clearly has implications on your project :)

Please let me know your thoughts, and thanks for creating this project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.