UNPKG

@zakhenry/grpc_tools_node_protoc_ts

Version:

Generate d.ts definitions for generated js files from grpc_tools_node_protoc

83 lines 4.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * This is the ProtoC compiler plugin. * * It only accepts stdin/stdout output according to the protocol * specified in [plugin.proto](https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/plugin.proto). */ const ExportMap_1 = require("./lib/ExportMap"); const Utility_1 = require("./lib/Utility"); const plugin_pb_1 = require("google-protobuf/google/protobuf/compiler/plugin_pb"); const ProtoMsgTsdFormatter_1 = require("./lib/format/ProtoMsgTsdFormatter"); const ProtoSvcTsdFormatter_1 = require("./lib/format/ProtoSvcTsdFormatter"); const TplEngine_1 = require("./lib/TplEngine"); const ProtoIndexFormatter_1 = require("./lib/format/ProtoIndexFormatter"); Utility_1.Utility.withAllStdIn((inputBuff) => { try { const typedInputBuff = new Uint8Array(inputBuff.length); typedInputBuff.set(inputBuff); const codeGenRequest = plugin_pb_1.CodeGeneratorRequest.deserializeBinary(typedInputBuff); const codeGenResponse = new plugin_pb_1.CodeGeneratorResponse(); const exportMap = new ExportMap_1.ExportMap(); const fileNameToDescriptor = {}; codeGenResponse.setSupportedFeatures(plugin_pb_1.CodeGeneratorResponse.Feature.FEATURE_PROTO3_OPTIONAL); const isGrpcJs = ["generate_package_definition", "grpc_js"].indexOf(codeGenRequest.getParameter()) !== -1; codeGenRequest.getProtoFileList().forEach((protoFileDescriptor) => { fileNameToDescriptor[protoFileDescriptor.getName()] = protoFileDescriptor; exportMap.addFileDescriptor(protoFileDescriptor); }); const filesList = []; const interfaces = []; codeGenRequest.getFileToGenerateList().forEach((fileName) => { // message part const msgFileName = Utility_1.Utility.filePathFromProtoWithoutExt(fileName); filesList.push(msgFileName); const msgTsdFile = new plugin_pb_1.CodeGeneratorResponse.File(); msgTsdFile.setName(msgFileName + ".d.ts"); const msgModel = ProtoMsgTsdFormatter_1.ProtoMsgTsdFormatter.format(fileNameToDescriptor[fileName], exportMap); msgTsdFile.setContent(TplEngine_1.TplEngine.render("msg_tsd", msgModel)); codeGenResponse.addFile(msgTsdFile); const msgInterfaceFile = new plugin_pb_1.CodeGeneratorResponse.File(); msgInterfaceFile.setName(msgFileName + ".interface.ts"); interfaces.push(msgFileName + ".interface"); const msgInterfaceModel = ProtoMsgTsdFormatter_1.ProtoMsgTsdFormatter.format(fileNameToDescriptor[fileName], exportMap, true); msgInterfaceFile.setContent(TplEngine_1.TplEngine.render("msg_tsd", msgInterfaceModel)); codeGenResponse.addFile(msgInterfaceFile); // service part const fileDescriptorModel = ProtoSvcTsdFormatter_1.ProtoSvcTsdFormatter.format(fileNameToDescriptor[fileName], exportMap, isGrpcJs); if (fileDescriptorModel != null) { const svcFileName = Utility_1.Utility.svcFilePathFromProtoWithoutExt(fileName); const svtTsdFile = new plugin_pb_1.CodeGeneratorResponse.File(); svtTsdFile.setName(svcFileName + ".d.ts"); svtTsdFile.setContent(TplEngine_1.TplEngine.render("svc_tsd", fileDescriptorModel)); codeGenResponse.addFile(svtTsdFile); // fileList.push(msgFileName); } }); // message js index const indexJsOutput = ProtoIndexFormatter_1.ProtoIndexFormatter.format(filesList, "index_js"); const indexJsFile = new plugin_pb_1.CodeGeneratorResponse.File(); indexJsFile.setName("index.js"); indexJsFile.setContent(indexJsOutput); codeGenResponse.addFile(indexJsFile); // message ts index const indexTsOutput = ProtoIndexFormatter_1.ProtoIndexFormatter.format(filesList, "index_tsd"); const indexTsdFile = new plugin_pb_1.CodeGeneratorResponse.File(); indexTsdFile.setName("index.d.ts"); indexTsdFile.setContent(indexTsOutput); codeGenResponse.addFile(indexTsdFile); // interface index const interfaceTsOutput = ProtoIndexFormatter_1.ProtoIndexFormatter.format(interfaces, "index_tsd"); const interfaceTsdFile = new plugin_pb_1.CodeGeneratorResponse.File(); interfaceTsdFile.setName("interfaces.ts"); interfaceTsdFile.setContent(interfaceTsOutput); codeGenResponse.addFile(interfaceTsdFile); process.stdout.write(Buffer.from(codeGenResponse.serializeBinary())); } catch (err) { console.error("protoc-gen-ts error: " + err.stack + "\n"); process.exit(1); } }); //# sourceMappingURL=index.js.map