ts-proto 1.125.0 → 1.126.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.126.1](https://github.com/stephenh/ts-proto/compare/v1.126.0...v1.126.1) (2022-09-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **options:** initializes M opt to empty object ([#673](https://github.com/stephenh/ts-proto/issues/673)) ([cb76c5e](https://github.com/stephenh/ts-proto/commit/cb76c5ea565081d610be08451e452269c5d3837c))
7
+
8
+ # [1.126.0](https://github.com/stephenh/ts-proto/compare/v1.125.0...v1.126.0) (2022-09-21)
9
+
10
+
11
+ ### Features
12
+
13
+ * **options:** adds protoc-gen-go-like M option ([#672](https://github.com/stephenh/ts-proto/issues/672)) ([9304e5d](https://github.com/stephenh/ts-proto/commit/9304e5db7172db53530fb08fe0486f56b2a17181)), closes [#596](https://github.com/stephenh/ts-proto/issues/596)
14
+
1
15
  # [1.125.0](https://github.com/stephenh/ts-proto/compare/v1.124.0...v1.125.0) (2022-09-03)
2
16
 
3
17
 
package/README.markdown CHANGED
@@ -450,6 +450,14 @@ Generated code will be placed in the Gradle build directory.
450
450
 
451
451
  - With `--ts_proto_opt=initializeFieldsAsUndefined=false`, all optional field initializers will be omited from the generated base instances.
452
452
 
453
+ - With `--ts_proto_opt=Mgoogle/protobuf/empty.proto=./google3/protobuf/empty`, ('M' means 'importMapping', similar to [protoc-gen-go](https://developers.google.com/protocol-buffers/docs/reference/go-generated#package)), the generated code import path for `./google/protobuf/empty.ts` will reflect the overridden value:
454
+
455
+ - `Mfoo/bar.proto=@myorg/some-lib` will map `foo/bar.proto` imports into `import ... from '@myorg/some-lib'`.
456
+ - `Mfoo/bar.proto=./some/local/lib` will map `foo/bar.proto` imports into `import ... from './some/local/lib'`.
457
+ - `Mfoo/bar.proto=some-modules/some-lib` will map `foo/bar.proto` imports into `import ... from 'some-module/some-lib'`.
458
+ - **Note**: Uses are accummulated, so multiple values are expected in the form of `--ts_proto_opt=M... --ts_proto_opt=M...` (one `ts_proto_opt` per mapping).
459
+ - **Note**: Proto files that match mapped imports **will not be generated**.
460
+
453
461
  ### NestJS Support
454
462
 
455
463
  We have a great way of working together with [nestjs](https://docs.nestjs.com/microservices/grpc). `ts-proto` generates `interfaces` and `decorators` for you controller, client. For more information see the [nestjs readme](NESTJS.markdown).
package/build/options.js CHANGED
@@ -72,6 +72,7 @@ function defaultOptions() {
72
72
  useJsonWireFormat: false,
73
73
  useNumericEnumForJson: false,
74
74
  initializeFieldsAsUndefined: true,
75
+ M: {},
75
76
  };
76
77
  }
77
78
  exports.defaultOptions = defaultOptions;
@@ -156,11 +157,28 @@ function optionsFromParameter(parameter) {
156
157
  exports.optionsFromParameter = optionsFromParameter;
157
158
  // A very naive parse function, eventually could/should use iots/runtypes
158
159
  function parseParameter(parameter) {
159
- const options = {};
160
- const pairs = parameter.split(",").map((s) => s.split("="));
161
- pairs.forEach(([key, _value]) => {
162
- const value = _value === "true" ? true : _value === "false" ? false : _value;
163
- if (options[key]) {
160
+ const options = { M: {} };
161
+ parameter.split(",").forEach((param) => {
162
+ // same as protoc-gen-go https://github.com/protocolbuffers/protobuf-go/blob/bf9455640daabb98c93b5b5e71628f3f813d57bb/compiler/protogen/protogen.go#L168-L171
163
+ const optionSeparatorPos = param.indexOf("=");
164
+ const key = param.substring(0, optionSeparatorPos);
165
+ const value = parseParamValue(param.substring(optionSeparatorPos + 1));
166
+ if (key.charAt(0) === "M") {
167
+ if (typeof value !== "string") {
168
+ console.warn(`ignoring invalid M option: '${param}'`);
169
+ }
170
+ else {
171
+ const mKey = key.substring(1);
172
+ if (options.M[mKey]) {
173
+ console.warn(`received conflicting M options: '${param}' will override 'M${mKey}=${options.M[mKey]}'`);
174
+ }
175
+ if (param.endsWith(".ts")) {
176
+ console.warn(`received M option '${param}' ending in '.ts' this is usually a mistake`);
177
+ }
178
+ options.M[mKey] = value;
179
+ }
180
+ }
181
+ else if (options[key]) {
164
182
  options[key] = [options[key], value];
165
183
  }
166
184
  else {
@@ -169,6 +187,9 @@ function parseParameter(parameter) {
169
187
  });
170
188
  return options;
171
189
  }
190
+ function parseParamValue(value) {
191
+ return value === "true" ? true : value === "false" ? false : value;
192
+ }
172
193
  function getTsPoetOpts(_options) {
173
194
  const imports = ["protobufjs/minimal" + _options.importSuffix];
174
195
  return {
package/build/plugin.js CHANGED
@@ -18,7 +18,9 @@ async function main() {
18
18
  const utils = (0, main_1.makeUtils)(options);
19
19
  const ctx = { typeMap, options, utils };
20
20
  const filesToGenerate = options.emitImportedFiles ? request.protoFile : (0, utils_1.protoFilesToGenerate)(request);
21
- const files = await Promise.all(filesToGenerate.map(async (file) => {
21
+ const files = await Promise.all(filesToGenerate
22
+ .filter((file) => !options.M[file.name])
23
+ .map(async (file) => {
22
24
  const [path, code] = (0, main_1.generateFile)(ctx, file);
23
25
  const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
24
26
  return { name: path, content };
package/build/utils.js CHANGED
@@ -178,12 +178,11 @@ function impFile(options, spec) {
178
178
  }
179
179
  exports.impFile = impFile;
180
180
  function impProto(options, module, type) {
181
- const importString = `${type}@./${module}${options.fileSuffix}${options.importSuffix}`;
182
- if (options.onlyTypes) {
183
- return (0, ts_poet_1.imp)("t:" + importString);
184
- }
185
- else {
186
- return (0, ts_poet_1.imp)(importString);
181
+ const prefix = options.onlyTypes ? "t:" : "";
182
+ const protoFile = `${module}.proto`;
183
+ if (options.M[protoFile]) {
184
+ return (0, ts_poet_1.imp)(`${prefix}${type}@${options.M[protoFile]}`);
187
185
  }
186
+ return (0, ts_poet_1.imp)(`${prefix}${type}@./${module}${options.fileSuffix}${options.importSuffix}`);
188
187
  }
189
188
  exports.impProto = impProto;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.125.0",
3
+ "version": "1.126.1",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",