Skip to content

Commit

Permalink
Only generate files in fileToGenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Sahlén committed Apr 29, 2021
1 parent 7417229 commit 7acf283
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion integration/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { prefixDisableLinter } from '../src/utils';
import { getTsPoetOpts, optionsFromParameter } from '../src/options';
import { Context } from '../src/context';
import { generateTypeRegistry } from '../src/generate-type-registry';
import { protoFilesToGenerate } from '../src/plugin';

/**
* Generates output for our integration tests from their example proto files.
Expand All @@ -31,7 +32,7 @@ async function generate(binFile: string, baseDir: string, parameter: string) {
const options = optionsFromParameter(parameter || '');
const typeMap = createTypeMap(request, options);

for (let file of request.protoFile) {
for (let file of protoFilesToGenerate(request)) {
// Make a different utils per file to track per-file usage
const utils = makeUtils(options);
const ctx: Context = { options, typeMap, utils };
Expand Down
13 changes: 11 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CodeGeneratorRequest, CodeGeneratorResponse, CodeGeneratorResponse_Feature } from 'ts-proto-descriptors';
import {
CodeGeneratorRequest,
CodeGeneratorResponse,
CodeGeneratorResponse_Feature,
FileDescriptorProto,
} from 'ts-proto-descriptors';
import { promisify } from 'util';
import { prefixDisableLinter, readToBuffer } from './utils';
import { generateFile, makeUtils } from './main';
Expand All @@ -7,6 +12,10 @@ import { Context } from './context';
import { getTsPoetOpts, optionsFromParameter } from './options';
import { generateTypeRegistry } from './generate-type-registry';

export function protoFilesToGenerate(request: CodeGeneratorRequest): FileDescriptorProto[] {
return request.protoFile.filter((f) => request.fileToGenerate.includes(f.name));
}

// this would be the plugin called by the protoc compiler
async function main() {
const stdin = await readToBuffer(process.stdin);
Expand All @@ -20,7 +29,7 @@ async function main() {
const ctx: Context = { typeMap, options, utils };

const files = await Promise.all(
request.protoFile.map(async (file) => {
protoFilesToGenerate(request).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

0 comments on commit 7acf283

Please sign in to comment.