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: added the exportTypeRegistry option. #1070

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=useNullAsOptional=true`, `undefined` values will be converted to `null`, and if you use `optional` label in your `.proto` file, the field will have `undefined` type as well. for example:

- With `--ts_proto_opt=exportTypeRegistry=true`, the generated `index.ts` will export the `typeRegistry.ts` file, only if `outputTypeRegistry=true`.

```protobuf
message ProfileInfo {
int32 id = 1;
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export type Options = {
annotateFilesWithVersion: boolean;
noDefaultsForOptionals: boolean;
bigIntLiteral: boolean;
exportTypeRegistry: boolean;
};

export function defaultOptions(): Options {
Expand Down Expand Up @@ -172,6 +173,7 @@ export function defaultOptions(): Options {
annotateFilesWithVersion: true,
noDefaultsForOptionals: false,
bigIntLiteral: true,
exportTypeRegistry: false,
};
}

Expand Down Expand Up @@ -286,6 +288,10 @@ export function optionsFromParameter(parameter: string | undefined): Options {
options.unrecognizedEnumValue = Number(options.unrecognizedEnumValue);
}

if (!options.outputTypeRegistry) {
options.exportTypeRegistry = false;
}

return options;
}

Expand Down
12 changes: 8 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
FileDescriptorProto,
} from "ts-proto-descriptors";
import { promisify } from "util";
import { generateIndexFiles, getVersions, protoFilesToGenerate, readToBuffer } from "./utils";
import { generateIndexFiles, getVersions, protoFilesToGenerate, readToBuffer, TYPE_REGISTRY_FILENAME } from "./utils";
import { generateFile, makeUtils } from "./main";
import { createTypeMap } from "./types";
import { BaseContext, createFileContext } from "./context";
Expand Down Expand Up @@ -60,11 +60,13 @@ async function main() {
const utils = makeUtils(options);
const ctx: BaseContext = { options, typeMap, utils };

const path = "typeRegistry.ts";
const code = generateTypeRegistry(ctx);

const content = code.toString({ ...getTsPoetOpts(options, tsProtoVersion, protocVersion), path });
files.push({ name: path, content });
const content = code.toString({
...getTsPoetOpts(options, tsProtoVersion, protocVersion),
path: TYPE_REGISTRY_FILENAME,
});
files.push({ name: TYPE_REGISTRY_FILENAME, content });
}

if (options.outputIndex) {
Expand Down Expand Up @@ -93,3 +95,5 @@ main()
process.stderr.write(e.stack);
process.exit(1);
});

export { TYPE_REGISTRY_FILENAME };
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { SourceDescription } from "./sourceInfo";
import { OneofOption, Options, ServiceOption } from "./options";
import { camelCaseGrpc, maybeSnakeToCamel, snakeToCamel } from "./case";

export const TYPE_REGISTRY_FILENAME: string = "typeRegistry.ts";

export function protoFilesToGenerate(request: CodeGeneratorRequest): FileDescriptorProto[] {
return request.protoFile.filter((f) => request.fileToGenerate.includes(f.name));
}
Expand Down Expand Up @@ -47,6 +49,10 @@ export function generateIndexFiles(files: FileDescriptorProto[], options: Option
branch.chunks.push(code`export * from "./${moduleName + options.importSuffix}";`);
}

if (options.exportTypeRegistry) {
packageTree.chunks.push(code`export * from "./${TYPE_REGISTRY_FILENAME}";`);
}

const indexFiles: [string, Code][] = [];
let branches: PackageTree[] = [packageTree];
let currentBranch;
Expand Down
1 change: 1 addition & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("options", () => {
"env": "both",
"esModuleInterop": false,
"exportCommonSymbols": true,
"exportTypeRegistry": false,
"fileSuffix": "",
"forceLong": "number",
"globalThisPolyfill": false,
Expand Down
Loading