Skip to content

Commit

Permalink
feat: implemented emitImportedFiles flag (#302)
Browse files Browse the repository at this point in the history
* feat: implemented emitImportedFiles flag

Fixes #294.
This commit puts changes from #283 behind the flag.

* fix: revert changes from #283
  • Loading branch information
doochik committed May 23, 2021
1 parent c89d3f7 commit 16b4aca
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
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
4 changes: 2 additions & 2 deletions integration/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parse } from 'path';
import { promisify } from 'util';
import { generateFile, makeUtils } from '../src/main';
import { createTypeMap } from '../src/types';
import { prefixDisableLinter, protoFilesToGenerate } from '../src/utils';
import { prefixDisableLinter } from '../src/utils';
import { getTsPoetOpts, optionsFromParameter } from '../src/options';
import { Context } from '../src/context';
import { generateTypeRegistry } from '../src/generate-type-registry';
Expand All @@ -31,7 +31,7 @@ async function generate(binFile: string, baseDir: string, parameter: string) {
const options = optionsFromParameter(parameter || '');
const typeMap = createTypeMap(request, options);

for (let file of protoFilesToGenerate(request)) {
for (let file of request.protoFile) {
// Make a different utils per file to track per-file usage
const utils = makeUtils(options);
const ctx: Context = { options, typeMap, utils };
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) => {
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

0 comments on commit 16b4aca

Please sign in to comment.