Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented emitImportedFiles flag #302

Merged
merged 2 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.:
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type Options = {
outputSchema: boolean;
// An alias of !output
onlyTypes: boolean;
emitImportedFiles: boolean;
};

export function defaultOptions(): Options {
Expand Down Expand Up @@ -77,6 +78,7 @@ export function defaultOptions(): Options {
exportCommonSymbols: true,
outputSchema: false,
onlyTypes: false,
emitImportedFiles: true,
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

I've been bitten by this before, but can you update integration/codegen.ts as well? Either with the same logic you have here, or maybe just have it always use request.protoFile? I don't think we have an integration test that explicitly tests that protoFilesToGenerate codepath, which is fine I think...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HI!

I've updated codegen.ts. I think usage of request.protoFile should be enough fo tests.

const [path, code] = generateFile(ctx, file);
const spec = await code.toStringWithImports({ ...getTsPoetOpts(options), path });
return { name: path, content: prefixDisableLinter(spec) };
Expand Down
1 change: 1 addition & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('options', () => {
"addNestjsRestParameter": false,
"constEnums": false,
"context": false,
"emitImportedFiles": true,
"env": "both",
"esModuleInterop": false,
"exportCommonSymbols": true,
Expand Down