Skip to content

Commit c681582

Browse files
authored
refactor: parse options with protoplugin api (#427)
1 parent b14c9f0 commit c681582

File tree

9 files changed

+75
-53
lines changed

9 files changed

+75
-53
lines changed

.changeset/quick-tips-mix.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@proto-graphql/protoc-plugin-helpers": patch
3+
"protoc-gen-pothos": patch
4+
"protoc-gen-nexus": patch
5+
---
6+
7+
refactor: parse options with protoplugin api
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export { createTsGenerator } from "./process";
2+
export { parseNexusOptions, parsePothosOptions } from "./options";
3+
export type { Options } from "./options";

packages/@proto-graphql/protoc-plugin-helpers/src/parseParams.test.ts renamed to packages/@proto-graphql/protoc-plugin-helpers/src/options.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,63 @@
11
import { describe, expect, it } from "vitest";
2-
import { parseParams } from "./parseParams";
2+
import { parseOptions } from "./options";
33

4-
describe("parseParams", () => {
4+
describe("parseOptions", () => {
55
it("reutrns true if value is empty", () => {
66
expect(
7-
parseParams("use_protobufjs=true", "nexus").printer.protobuf,
7+
parseOptions([{ key: "use_protobufjs", value: "true" }], "nexus").printer
8+
.protobuf,
89
).toEqual("protobufjs");
910
});
1011

1112
it('parses "true" string to true', () => {
1213
expect(
13-
parseParams("use_protobufjs=true", "nexus").printer.protobuf,
14+
parseOptions([{ key: "use_protobufjs", value: "true" }], "nexus").printer
15+
.protobuf,
1416
).toEqual("protobufjs");
1517
});
1618

1719
it('parses "true" string to false', () => {
1820
expect(
19-
parseParams("use_protobufjs=false", "nexus").printer.protobuf,
21+
parseOptions([{ key: "use_protobufjs", value: "false" }], "nexus").printer
22+
.protobuf,
2023
).toEqual("google-protobuf");
2124
});
2225

2326
it("parses importPrefix", () => {
2427
expect(
25-
parseParams("import_prefix=@foobar/baz", "nexus").printer.importPrefix,
28+
parseOptions([{ key: "import_prefix", value: "@foobar/baz" }], "nexus")
29+
.printer.importPrefix,
2630
).toEqual("@foobar/baz");
2731
});
2832

2933
it("parses fileLayout", () => {
3034
expect(
31-
parseParams("file_layout=graphql_type", "nexus").printer.fileLayout,
35+
parseOptions([{ key: "file_layout", value: "graphql_type" }], "nexus")
36+
.printer.fileLayout,
3237
).toEqual("graphql_type");
3338
});
3439

3540
it("throws an erorr when useProtobufjs is string", () => {
3641
expect(() => {
37-
parseParams("use_protobufjs=foobar", "nexus");
42+
parseOptions([{ key: "use_protobufjs", value: "foobar" }], "nexus");
3843
}).toThrow();
3944
});
4045

4146
it("throws an erorr when importString is boolean", () => {
4247
expect(() => {
43-
parseParams("import_prefix", "nexus");
48+
parseOptions([{ key: "import_prefix", value: "" }], "nexus");
4449
}).toThrow();
4550
});
4651

4752
it("throws an erorr when invalid fileLayout", () => {
4853
expect(() => {
49-
parseParams("file_layout=foobar", "nexus");
54+
parseOptions([{ key: "file_layout", value: "foobar" }], "nexus");
5055
}).toThrow();
5156
});
5257

5358
it("throws an erorr when received unknown params", () => {
5459
expect(() => {
55-
parseParams("foobar=qux", "nexus");
60+
parseOptions([{ key: "foobar", value: "qux" }], "nexus");
5661
}).toThrow();
5762
});
5863
});

packages/@proto-graphql/protoc-plugin-helpers/src/parseParams.ts renamed to packages/@proto-graphql/protoc-plugin-helpers/src/options.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { RawPluginOptions } from "@bufbuild/protoplugin/dist/cjs/parameter";
12
import {
23
type PrinterOptions,
34
type TypeOptions,
@@ -7,13 +8,27 @@ import {
78
protobufLibs,
89
} from "@proto-graphql/codegen-core";
910

10-
export function parseParams<DSL extends PrinterOptions["dsl"]>(
11-
input: string | undefined,
12-
dsl: DSL,
13-
): {
11+
export type Options<DSL extends PrinterOptions["dsl"]> = {
1412
type: TypeOptions;
1513
printer: Extract<PrinterOptions, { dsl: DSL }>;
16-
} {
14+
};
15+
16+
export function parsePothosOptions(
17+
rawOptions: RawPluginOptions,
18+
): Options<"pothos"> {
19+
return parseOptions(rawOptions, "pothos");
20+
}
21+
22+
export function parseNexusOptions(
23+
rawOptions: RawPluginOptions,
24+
): Options<"nexus"> {
25+
return parseOptions(rawOptions, "nexus");
26+
}
27+
28+
export function parseOptions<DSL extends PrinterOptions["dsl"]>(
29+
rawOptions: RawPluginOptions,
30+
dsl: DSL,
31+
): Options<DSL> {
1732
const params = {
1833
type: {
1934
partialInputs: false,
@@ -31,8 +46,8 @@ export function parseParams<DSL extends PrinterOptions["dsl"]>(
3146
} as Extract<PrinterOptions, { dsl: DSL }>,
3247
};
3348

34-
const boolParam = (name: string, v: string | undefined): boolean => {
35-
if (!v || v === "true") return true;
49+
const boolParam = (name: string, v: string): boolean => {
50+
if (v === "" || v === "true") return true;
3651
if (v === "false") return false;
3752
throw new Error(`${name} should be bool, got string: ${v}`);
3853
};
@@ -48,11 +63,7 @@ export function parseParams<DSL extends PrinterOptions["dsl"]>(
4863
return whitelist.includes(v as any);
4964
}
5065

51-
const kvs = input ? input.split(",") : [];
52-
for (const kv of kvs) {
53-
const idx = kv.indexOf("=");
54-
const [k, v] =
55-
idx === -1 ? [kv, ""] : [kv.slice(0, idx), kv.slice(idx + 1)];
66+
for (const { key: k, value: v } of rawOptions) {
5667
switch (k) {
5768
case "use_protobufjs": {
5869
if (boolParam(k, v)) params.printer.protobuf = "protobufjs";
@@ -114,7 +125,7 @@ export function parseParams<DSL extends PrinterOptions["dsl"]>(
114125
// no-op
115126
break;
116127
default:
117-
throw new Error(`unknown param: ${kv}`);
128+
throw new Error(`unknown param: ${k}=${v}`);
118129
}
119130
}
120131

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import type { DescFile } from "@bufbuild/protobuf";
22
import type { Schema } from "@bufbuild/protoplugin";
3-
import type { PrinterOptions, TypeOptions } from "@proto-graphql/codegen-core";
4-
5-
import { parseParams } from "./parseParams";
3+
import type { PrinterOptions } from "@proto-graphql/codegen-core";
4+
import { type Options, parseOptions } from "./options";
65

76
export function createTsGenerator<DSL extends PrinterOptions["dsl"]>({
87
generateFiles,
98
dsl,
109
}: {
11-
generateFiles: (
12-
schema: Schema,
13-
file: DescFile,
14-
opts: {
15-
type: TypeOptions;
16-
printer: Extract<PrinterOptions, { dsl: DSL }>;
17-
},
18-
) => void;
10+
generateFiles: (schema: Schema<Options<DSL>>, file: DescFile) => void;
1911
dsl: DSL;
20-
}): (schema: Schema) => void {
21-
return function generateTs(schema: Schema) {
22-
const params = parseParams(schema.proto.parameter, dsl);
12+
}): (schema: Schema<Options<DSL>>) => void {
13+
return function generateTs(schema: Schema<Partial<Options<DSL>>>) {
14+
if (schema.options.printer?.dsl == null) {
15+
Object.assign(schema.options, parseOptions([], dsl));
16+
}
2317
for (const file of schema.files) {
24-
generateFiles(schema, file, params);
18+
generateFiles(schema as Schema<Options<DSL>>, file);
2519
}
2620
};
2721
}

packages/protoc-gen-nexus/src/plugin.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
2-
import { createTsGenerator } from "@proto-graphql/protoc-plugin-helpers";
2+
import {
3+
createTsGenerator,
4+
parseNexusOptions,
5+
} from "@proto-graphql/protoc-plugin-helpers";
36

47
import { version } from "../package.json";
58
import { generateFiles } from "./printer";
@@ -11,9 +14,7 @@ export const protocGenNexus = createEcmaScriptPlugin({
1114
generateFiles,
1215
dsl: "nexus",
1316
}),
14-
parseOptions(rawOptions) {
15-
return {};
16-
},
17+
parseOptions: parseNexusOptions,
1718
// NOTE: force `target=ts`
1819
transpile: (files) => {
1920
return files;

packages/protoc-gen-nexus/src/printer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import type { DescFile, Registry } from "@bufbuild/protobuf";
22
import type { Schema } from "@bufbuild/protoplugin";
33
import {
4-
type TypeOptions,
54
collectTypesFromFile,
65
createRegistryFromSchema,
76
filename,
87
filenameFromProtoFile,
98
printCodes,
109
} from "@proto-graphql/codegen-core";
10+
import type { Options } from "@proto-graphql/protoc-plugin-helpers";
1111
import type { Code } from "ts-poet";
1212

1313
import { createTypeDslCodes } from "./dslgen";
1414
import type { NexusPrinterOptions } from "./dslgen/printers/util";
1515

1616
export function generateFiles(
17-
schema: Schema,
17+
schema: Schema<Options<"nexus">>,
1818
file: DescFile,
19-
opts: { type: TypeOptions; printer: NexusPrinterOptions },
2019
): void {
20+
const opts = schema.options;
2121
const registry = createRegistryFromSchema(schema);
2222
const types = collectTypesFromFile(file, opts.type, schema.allFiles);
2323

packages/protoc-gen-pothos/src/plugin.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
2-
import { createTsGenerator } from "@proto-graphql/protoc-plugin-helpers";
2+
import {
3+
createTsGenerator,
4+
parsePothosOptions,
5+
} from "@proto-graphql/protoc-plugin-helpers";
36

47
import { version } from "../package.json";
58
import { generateFiles } from "./printer";
@@ -11,9 +14,7 @@ export const protocGenPothos = createEcmaScriptPlugin({
1114
generateFiles,
1215
dsl: "pothos",
1316
}),
14-
parseOptions(rawOptions) {
15-
return {};
16-
},
17+
parseOptions: parsePothosOptions,
1718
// NOTE: force `target=ts`
1819
transpile: (files) => {
1920
return files;

packages/protoc-gen-pothos/src/printer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { DescFile, Registry } from "@bufbuild/protobuf";
22
import type { Schema } from "@bufbuild/protoplugin";
33
import {
4-
type TypeOptions,
54
collectTypesFromFile,
65
createRegistryFromSchema,
76
filename,
@@ -10,16 +9,18 @@ import {
109
} from "@proto-graphql/codegen-core";
1110
import type { Code } from "ts-poet";
1211

12+
import type { Options } from "@proto-graphql/protoc-plugin-helpers";
1313
import { createTypeDslCodes } from "./dslgen";
1414
import type { PothosPrinterOptions } from "./dslgen/printers/util";
1515

1616
const allowedProtobufs = ["ts-proto", "protobuf-es"];
1717

1818
export function generateFiles(
19-
schema: Schema,
19+
schema: Schema<Options<"pothos">>,
2020
file: DescFile,
21-
opts: { type: TypeOptions; printer: PothosPrinterOptions },
2221
): void {
22+
const opts = schema.options;
23+
2324
if (!allowedProtobufs.includes(opts.printer.protobuf)) {
2425
opts.printer.protobuf = "ts-proto"; // default
2526
}

0 commit comments

Comments
 (0)