From b64d851541fbfed32bcfbd2f473e42554d12d1bf Mon Sep 17 00:00:00 2001 From: Aleksei Androsov Date: Sat, 22 May 2021 19:21:31 +0300 Subject: [PATCH] feat: implemented emitImportedFiles flag Fixes #294. This commit puts changes from #283 behind the flag. --- README.markdown | 3 +++ src/options.ts | 2 ++ src/plugin.ts | 3 ++- tests/options-test.ts | 1 + 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 21e60e8ff..917662c3c 100644 --- a/README.markdown +++ b/README.markdown @@ -305,6 +305,9 @@ protoc --plugin=node_modules/ts-proto/protoc-gen-ts_proto ./batching.proto -I. - With `--ts_proto_opt=outputServices=grpc-js`, ts-proto will output service definitions and server / client stubs in [grpc-js](https://github.com/grpc/grpc-node/tree/master/packages/grpc-js) format. +- With `--ts_proto_opt=emitImportedFiles=false`, ts-proto will not emit `google/protobuf/*` files unless you explicit add files to `protoc` like this +`protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto my_message.proto google/protobuf/duration.proto` + ### Only Types If you're looking for `ts-proto` to generate only types for your Protobuf types then passing all three of `outputEncodeMethods`, `outputJsonMethods`, and `outputClientImpl` as `false` is probably what you want, i.e.: diff --git a/src/options.ts b/src/options.ts index ee2e5b8f0..8116aa843 100644 --- a/src/options.ts +++ b/src/options.ts @@ -48,6 +48,7 @@ export type Options = { outputSchema: boolean; // An alias of !output onlyTypes: boolean; + emitImportedFiles: boolean; }; export function defaultOptions(): Options { @@ -77,6 +78,7 @@ export function defaultOptions(): Options { exportCommonSymbols: true, outputSchema: false, onlyTypes: false, + emitImportedFiles: true, }; } diff --git a/src/plugin.ts b/src/plugin.ts index 77174715d..ff20e3617 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -24,8 +24,9 @@ async function main() { const utils = makeUtils(options); const ctx: Context = { typeMap, options, utils }; + const filesToGenerate = options.emitImportedFiles ? request.protoFile : protoFilesToGenerate(request); const files = await Promise.all( - protoFilesToGenerate(request).map(async (file) => { + filesToGenerate.map(async (file) => { const [path, code] = generateFile(ctx, file); const spec = await code.toStringWithImports({ ...getTsPoetOpts(options), path }); return { name: path, content: prefixDisableLinter(spec) }; diff --git a/tests/options-test.ts b/tests/options-test.ts index 99d2e6400..9aaf93393 100644 --- a/tests/options-test.ts +++ b/tests/options-test.ts @@ -8,6 +8,7 @@ describe('options', () => { "addNestjsRestParameter": false, "constEnums": false, "context": false, + "emitImportedFiles": true, "env": "both", "esModuleInterop": false, "exportCommonSymbols": true,