Skip to content

Commit 626464e

Browse files
tatethurstonTate
and
Tate
authored
add exclude option to twirp.json (#127)
* add exclude option to twirp.json fixes 113. * Update README.md Co-authored-by: Tate <[email protected]>
1 parent 5fd9292 commit 626464e

File tree

10 files changed

+5082
-11
lines changed

10 files changed

+5082
-11
lines changed

README.md

+59-8
Original file line numberDiff line numberDiff line change
@@ -586,17 +586,53 @@ TwirpScript aims to be zero config, but can be configured by creating a `.twirp.
586586
```
587587
588588
Setting `root` to `src`:
589-
590-
A.proto would `import` B.proto as follows:
591-
592-
```protobuf
593-
import "B.proto";
594-
```
595-
596-
TypeScript projects will generally want to set this value to match their `rootDir`, particularly when using [Protocol Buffers Well-Known Types](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf) so that the generated well-known type files are under the `rootDir`.
589+
590+
// twirp.json
591+
592+
```json
593+
{
594+
"root": "src"
595+
}
596+
```
597+
598+
A.proto would `import` B.proto as follows:
599+
600+
```protobuf
601+
import "B.proto";
602+
```
603+
604+
TypeScript projects will generally want to set this value to match their `rootDir`, particularly when using [Protocol Buffers Well-Known Types](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf) so that the generated well-known type files are under the `rootDir`.
605+
597606
</td>
598607
<td>string (filepath)</td>
599608
</tr>
609+
<tr>
610+
<td>exclude</td>
611+
<td>
612+
An array of patterns that should be skipped when searching for `.proto` files.
613+
614+
Example:
615+
616+
If we have the following project structure:
617+
/src
618+
/foo
619+
A.proto
620+
/bar
621+
B.proto
622+
623+
Setting `exclude` to `["/bar/"]`:
624+
625+
// twirp.json
626+
```json
627+
{
628+
exclude: ["/bar/"]
629+
}
630+
```
631+
632+
Will only process A.proto (B.proto) will be excluded from TwirpScript's code generation.
633+
</td>
634+
<td>string[] (RegExp pattern)</td>
635+
</tr>
600636
<tr>
601637
<td>dest</td>
602638
<td>
@@ -624,6 +660,13 @@ TwirpScript aims to be zero config, but can be configured by creating a `.twirp.
624660
625661
Setting `dest` to `out` will generate the following:
626662
663+
// twirp.json
664+
```json
665+
{
666+
dest: 'out',
667+
}
668+
```
669+
627670
```
628671
/src
629672
A.proto
@@ -637,6 +680,14 @@ TwirpScript aims to be zero config, but can be configured by creating a `.twirp.
637680
Note that the generated directory structure will mirror the `proto` paths exactly as is, only nested under the `dest` directory. If you want to change this, for instance, to omit `src` from the `out` directory above, you can set the `root`.
638681
639682
Setting `root` to `src` (in addition to setting `dest` to `out`) will generate the following:
683+
684+
// twirp.json
685+
```json
686+
{
687+
root: 'src',
688+
dest: 'out',
689+
}
690+
```
640691
641692
```
642693
/src

examples/config-exclude/.twirp.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exclude": ["/bar/"]
3+
}

examples/config-exclude/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# TwirpScript Config Exclude Example
2+
3+
## Getting Started
4+
5+
### Running Locally
6+
7+
1. `yarn install`
8+
2. `yarn build`

examples/config-exclude/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "config-exclude",
3+
"scripts": {
4+
"build": "yarn twirpscript && yarn tsc"
5+
},
6+
"dependencies": {
7+
"twirpscript": "*"
8+
},
9+
"devDependencies": {
10+
"typescript": "^4.3.5"
11+
}
12+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
import "A.proto";
4+
5+
message Bar {
6+
Foo foo = 1;
7+
}
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
// Source: src/foo/A.proto
3+
4+
import type { ByteSource } from "twirpscript";
5+
import { BinaryReader, BinaryWriter } from "twirpscript";
6+
7+
//========================================//
8+
// Types //
9+
//========================================//
10+
11+
export interface Foo {
12+
name: string;
13+
}
14+
15+
//========================================//
16+
// Protobuf Encode / Decode //
17+
//========================================//
18+
19+
export const Foo = {
20+
/**
21+
* Serializes a Foo to protobuf.
22+
*/
23+
encode: function (foo: Partial<Foo>): Uint8Array {
24+
return Foo._writeMessage(foo, new BinaryWriter()).getResultBuffer();
25+
},
26+
27+
/**
28+
* Deserializes a Foo from protobuf.
29+
*/
30+
decode: function (bytes: ByteSource): Foo {
31+
return Foo._readMessage(Foo.initialize(), new BinaryReader(bytes));
32+
},
33+
34+
/**
35+
* Serializes a Foo to JSON.
36+
*/
37+
encodeJSON: function (foo: Partial<Foo>): string {
38+
return JSON.stringify(Foo._writeMessageJSON(foo));
39+
},
40+
41+
/**
42+
* Deserializes a Foo from JSON.
43+
*/
44+
decodeJSON: function (json: string): Foo {
45+
return Foo._readMessageJSON(Foo.initialize(), JSON.parse(json));
46+
},
47+
48+
/**
49+
* Initializes a Foo with all fields set to their default value.
50+
*/
51+
initialize: function (): Foo {
52+
return {
53+
name: "",
54+
};
55+
},
56+
57+
/**
58+
* @private
59+
*/
60+
_writeMessage: function (
61+
msg: Partial<Foo>,
62+
writer: BinaryWriter
63+
): BinaryWriter {
64+
if (msg.name) {
65+
writer.writeString(1, msg.name);
66+
}
67+
return writer;
68+
},
69+
70+
/**
71+
* @private
72+
*/
73+
_writeMessageJSON: function (msg: Partial<Foo>): Record<string, unknown> {
74+
const json: Record<string, unknown> = {};
75+
if (msg.name) {
76+
json.name = msg.name;
77+
}
78+
return json;
79+
},
80+
81+
/**
82+
* @private
83+
*/
84+
_readMessage: function (msg: Foo, reader: BinaryReader): Foo {
85+
while (reader.nextField()) {
86+
const field = reader.getFieldNumber();
87+
switch (field) {
88+
case 1: {
89+
msg.name = reader.readString();
90+
break;
91+
}
92+
default: {
93+
reader.skipField();
94+
break;
95+
}
96+
}
97+
}
98+
return msg;
99+
},
100+
101+
/**
102+
* @private
103+
*/
104+
_readMessageJSON: function (msg: Foo, json: any): Foo {
105+
const name = json.name ?? json.name;
106+
if (name) {
107+
msg.name = name;
108+
}
109+
return msg;
110+
},
111+
};
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
syntax = "proto3";
2+
3+
message Foo {
4+
string name = 1;
5+
}

examples/config-exclude/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"moduleResolution": "Node",
5+
"noEmitOnError": false,
6+
"noUnusedLocals": true,
7+
"outDir": "dist",
8+
"rootDir": ".",
9+
"strict": true,
10+
"target": "ES2019"
11+
},
12+
"include": ["src", "out"]
13+
}

0 commit comments

Comments
 (0)