-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.ts
218 lines (209 loc) · 6.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { parse as parseYaml } from "https://deno.land/std@0.147.0/encoding/yaml.ts";
import { build, BuildConfig } from "../../core/schema/builder.ts";
import { Schema } from "../../core/schema/model.ts";
import { replaceTsFileExtensionInImportStatementFromReader } from "../../misc/compat/tsc.ts";
import { CodeEntry } from "../index.ts";
import { join } from "../path.ts";
import genMessages, { Field, getWellKnownTypeMapping } from "./messages.ts";
import genServices from "./services.ts";
import {
createImportBuffer as createImportBufferFn,
CreateImportBufferFn,
ImportBuffer,
} from "./import-buffer.ts";
import { createIndexBuffer, IndexBuffer } from "./index-buffer.ts";
export type GenConfig = Omit<BundleConfig, "units"> & {
messages?: GenMessagesConfig;
services?: GenServicesConfig;
};
export default function gen(
schema: Schema,
config: GenConfig = {},
): AsyncGenerator<CodeEntry> {
const { messages, services } = config;
const units = [{ schema, messages, services }];
return replaceExts(
filterDuplicates(genAll({ ...config, units })),
config.extInImport,
);
}
export interface BundleConfig {
units: GenUnit[];
customTypeMapping?: CustomTypeMapping;
runtime?: GenRuntimeConfig;
indexFilename?: string;
extInImport?: string;
}
export interface GenUnit {
schema: Schema;
messages?: GenMessagesConfig;
services?: GenServicesConfig;
}
export function bundle(config: BundleConfig): AsyncGenerator<CodeEntry> {
return replaceExts(filterDuplicates(genAll(config)), config.extInImport);
}
type GenAllConfig = Omit<BundleConfig, "extInImport">;
async function* genAll(config: GenAllConfig): AsyncGenerator<CodeEntry> {
const { units, customTypeMapping, runtime, indexFilename } = config;
const _runtime = runtime ?? { packageName: "@pbkit/runtime" };
if (_runtime.packageName == null && !_runtime.outDir.startsWith("../")) {
for await (const [filePath, data] of _runtime.iterRuntimeFiles()) {
yield [join(_runtime.outDir, filePath), data];
}
}
const createImportBuffer: CreateImportBufferFn = (config) => (
createImportBufferFn({ ...config, runtime: _runtime })
);
const indexBuffer = createIndexBuffer({ indexFilename });
for await (const unit of units) {
yield* genBuildUnit(
unit,
createImportBuffer,
indexBuffer,
customTypeMapping,
);
}
yield* indexBuffer;
}
async function* genBuildUnit(
unit: GenUnit,
createImportBuffer: CreateImportBufferFn,
indexBuffer: IndexBuffer,
_customTypeMapping?: CustomTypeMapping,
): AsyncGenerator<CodeEntry> {
const { schema } = unit;
const messages = unit.messages ?? { outDir: "messages" };
const services = unit.services ?? { outDir: "services" };
const customTypeMapping: CustomTypeMapping = {
...getWellKnownTypeMapping({ messages }),
..._customTypeMapping,
};
yield* genMessages(schema, {
createImportBuffer,
indexBuffer,
customTypeMapping,
messages,
});
yield* genServices(schema, {
createImportBuffer,
indexBuffer,
messages,
services,
});
}
async function* filterDuplicates(
codes: AsyncGenerator<CodeEntry>,
): AsyncGenerator<CodeEntry> {
const filter = new Set<string>();
for await (const [filePath, data] of codes) {
if (filter.has(filePath)) continue;
filter.add(filePath);
yield [filePath, data];
}
}
async function* replaceExts(
codes: AsyncGenerator<CodeEntry>,
extInImport: string = ".ts",
): AsyncGenerator<CodeEntry> {
const ext = extInImport.trim();
const replaceExt = replaceTsFileExtensionInImportStatementFromReader;
for await (const [filePath, data] of codes) {
yield [filePath, ext !== ".ts" ? await replaceExt(data, ext) : data];
}
}
export interface BundleConfigYaml {
["out-dir"]?: string;
["index-filename"]?: string;
["ext-in-import"]?: string;
["runtime-dir"]?: string;
["runtime-package"]?: string;
["units"]?: {
["proto-paths"]?: string[];
["entry-paths"]?: string[];
["proto-files"]?: string[];
["messages-dir"]?: string;
["services-dir"]?: string;
}[];
}
export interface GetBuildConfigFn {
(
protoPaths: string[],
entryPaths: string[],
protoFiles: string[],
): Promise<BuildConfig>;
}
export async function yamlToBundleConfig(
yaml: BundleConfigYaml,
getBuildConfig: GetBuildConfigFn,
iterRuntimeFiles: () => AsyncGenerator<CodeEntry>,
): Promise<BundleConfig> {
return {
indexFilename: yaml["index-filename"],
extInImport: yaml["ext-in-import"],
runtime: yaml["runtime-package"]
? { packageName: yaml["runtime-package"].trim() }
: { iterRuntimeFiles, outDir: (yaml["runtime-dir"] ?? "runtime").trim() },
units: await Promise.all((yaml["units"] ?? []).map(async (unit) => {
const schema = await build(
await getBuildConfig(
unit["proto-paths"] ?? [],
unit["entry-paths"] ?? [],
unit["proto-files"] ?? [],
),
);
return {
schema,
messages: { outDir: (unit["messages-dir"] ?? "messages").trim() },
services: { outDir: (unit["services-dir"] ?? "services").trim() },
};
})),
};
}
export async function yamlTextToBundleConfig(
configYamlText: string,
getBuildConfig: GetBuildConfigFn,
iterRuntimeFiles: () => AsyncGenerator<CodeEntry>,
): Promise<[string, BundleConfig]> {
const yaml = parseYaml(configYamlText) as BundleConfigYaml;
const outDir = yaml["out-dir"] ?? "out";
return [
outDir,
await yamlToBundleConfig(
yaml,
getBuildConfig,
iterRuntimeFiles,
),
];
}
export interface CustomTypeMapping {
[typePath: string]: {
tsType: string;
getWireValueToTsValueCode: GetFieldCodeFn;
getTsValueToWireValueCode: GetFieldCodeFn;
getTsValueToJsonValueCode: GetFieldCodeFn;
getJsonValueToTsValueCode: GetFieldCodeFn;
};
}
export interface GetFieldCodeFnConfig {
filePath: string;
importBuffer: ImportBuffer;
field: Field;
}
export interface GetFieldCodeFn {
(config: GetFieldCodeFnConfig): string | undefined;
}
export type GenRuntimeConfig = {
iterRuntimeFiles: () => AsyncGenerator<CodeEntry>;
outDir: string;
packageName?: undefined;
} | {
iterRuntimeFiles?: undefined;
outDir?: undefined;
packageName: string;
};
export interface GenMessagesConfig {
outDir: string;
}
export interface GenServicesConfig {
outDir: string;
}