UNPKG

5.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var querystring_1 = require("querystring");
4var parameters_1 = require("./parameters");
5function filePathToPseudoNamespace(filePath) {
6 return filePath.replace(".proto", "").replace(/\//g, "_").replace(/\./g, "_").replace(/\-/g, "_") + "_pb";
7}
8exports.filePathToPseudoNamespace = filePathToPseudoNamespace;
9function throwError(message, includeUnexpectedBehaviourMessage) {
10 if (includeUnexpectedBehaviourMessage === void 0) { includeUnexpectedBehaviourMessage = true; }
11 if (includeUnexpectedBehaviourMessage) {
12 throw new Error(message + " - This is unhandled behaviour and should be reported as an issue on https://github.com/improbable-eng/ts-protoc-gen/issues");
13 }
14 else {
15 throw new Error(message);
16 }
17}
18exports.throwError = throwError;
19function stripPrefix(str, prefix) {
20 if (str.substr(0, prefix.length) === prefix) {
21 return str.substr(prefix.length);
22 }
23 return str;
24}
25exports.stripPrefix = stripPrefix;
26function snakeToCamel(str) {
27 return str.replace(/(\_\w)/g, function (m) {
28 return m[1].toUpperCase();
29 });
30}
31exports.snakeToCamel = snakeToCamel;
32function uppercaseFirst(str) {
33 return str.charAt(0).toUpperCase() + str.slice(1);
34}
35exports.uppercaseFirst = uppercaseFirst;
36var PROTO2_SYNTAX = "proto2";
37function isProto2(fileDescriptor) {
38 return (fileDescriptor.getSyntax() === "" || fileDescriptor.getSyntax() === PROTO2_SYNTAX);
39}
40exports.isProto2 = isProto2;
41function oneOfName(name) {
42 return uppercaseFirst(snakeToCamel(name.toLowerCase()));
43}
44exports.oneOfName = oneOfName;
45function generateIndent(indentLevel) {
46 var indent = "";
47 for (var i = 0; i < indentLevel; i++) {
48 indent += " ";
49 }
50 return indent;
51}
52exports.generateIndent = generateIndent;
53function getPathToRoot(fileName) {
54 var depth = fileName.split("/").length;
55 return depth === 1 ? "./" : new Array(depth).join("../");
56}
57exports.getPathToRoot = getPathToRoot;
58function withinNamespaceFromExportEntry(name, exportEntry) {
59 return exportEntry.pkg ? name.substring(exportEntry.pkg.length + 1) : name;
60}
61exports.withinNamespaceFromExportEntry = withinNamespaceFromExportEntry;
62function replaceProtoSuffix(protoFilePath) {
63 var suffix = ".proto";
64 var hasProtoSuffix = protoFilePath.slice(protoFilePath.length - suffix.length) === suffix;
65 return hasProtoSuffix
66 ? protoFilePath.slice(0, -suffix.length) + "_pb"
67 : protoFilePath;
68}
69exports.replaceProtoSuffix = replaceProtoSuffix;
70function withAllStdIn(callback) {
71 var ret = [];
72 var len = 0;
73 var stdin = process.stdin;
74 stdin.on("readable", function () {
75 var chunk;
76 while ((chunk = stdin.read())) {
77 if (!(chunk instanceof Buffer))
78 throw new Error("Did not receive buffer");
79 ret.push(chunk);
80 len += chunk.length;
81 }
82 });
83 stdin.on("end", function () {
84 callback(Buffer.concat(ret, len));
85 });
86}
87exports.withAllStdIn = withAllStdIn;
88function normaliseFieldObjectName(name) {
89 switch (name) {
90 case "abstract":
91 case "boolean":
92 case "break":
93 case "byte":
94 case "case":
95 case "catch":
96 case "char":
97 case "class":
98 case "const":
99 case "continue":
100 case "debugger":
101 case "default":
102 case "delete":
103 case "do":
104 case "double":
105 case "else":
106 case "enum":
107 case "export":
108 case "extends":
109 case "false":
110 case "final":
111 case "finally":
112 case "float":
113 case "for":
114 case "function":
115 case "goto":
116 case "if":
117 case "implements":
118 case "import":
119 case "in":
120 case "instanceof":
121 case "int":
122 case "interface":
123 case "long":
124 case "native":
125 case "new":
126 case "null":
127 case "package":
128 case "private":
129 case "protected":
130 case "public":
131 case "return":
132 case "short":
133 case "static":
134 case "super":
135 case "switch":
136 case "synchronized":
137 case "this":
138 case "throw":
139 case "throws":
140 case "transient":
141 case "try":
142 case "typeof":
143 case "var":
144 case "void":
145 case "volatile":
146 case "while":
147 case "with":
148 return "pb_" + name;
149 }
150 return name;
151}
152exports.normaliseFieldObjectName = normaliseFieldObjectName;
153function getServiceParameter(service) {
154 switch (service) {
155 case "true":
156 console.warn("protoc-gen-ts warning: The service=true parameter has been deprecated. Use service=grpc-web instead.");
157 return parameters_1.ServiceParameter.GrpcWeb;
158 case "grpc-web":
159 return parameters_1.ServiceParameter.GrpcWeb;
160 case "grpc-node":
161 return parameters_1.ServiceParameter.GrpcNode;
162 case undefined:
163 return parameters_1.ServiceParameter.None;
164 default:
165 throw new Error("Unrecognised service parameter: " + service);
166 }
167}
168exports.getServiceParameter = getServiceParameter;
169function getModeParameter(mode) {
170 switch (mode) {
171 case "grpc-js":
172 return parameters_1.ModeParameter.GrpcJs;
173 case undefined:
174 return parameters_1.ModeParameter.None;
175 default:
176 throw new Error("Unrecognised mode parameter: " + mode);
177 }
178}
179exports.getModeParameter = getModeParameter;
180function getParameterEnums(parameter) {
181 var _a = querystring_1.parse(parameter, ","), service = _a.service, mode = _a.mode;
182 return {
183 service: getServiceParameter(service),
184 mode: getModeParameter(mode)
185 };
186}
187exports.getParameterEnums = getParameterEnums;
188//# sourceMappingURL=util.js.map
\No newline at end of file