-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.ts
411 lines (392 loc) Β· 9.85 KB
/
types.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import type { Element } from "./htmlisp/types.ts";
type Utilities =
& Record<
string,
(
this: { context: Context; props: Context },
// deno-lint-ignore no-explicit-any
...args: any
) => unknown | Promise<unknown> | void
>
& {
_onRenderStart?: (context: Context) => void;
_onRenderEnd?: (context: Context) => void;
};
type Utility = {
utility: string;
parameters?: (string | Utility)[];
};
type Props = Record<string, string | undefined>;
// deno-lint-ignore no-explicit-any
type Attributes = Record<string, any>;
type Category = { id: string; title: string; url: string };
type DataContext = Record<string, unknown> | Record<string, unknown>[];
type ParentCategory = { title: string; children: Category[] };
type DataSourcesModule = {
init(o: DataSourcesApi): DataSources;
};
type DataSourcesApi = {
load: LoadApi;
render: Render;
renderSync: RenderSync;
};
type Render = (
o: {
componentName?: string;
htmlInput?: string;
context?: Context;
props?: Context;
},
) => Promise<string>;
type RenderSync = (
o: {
componentName?: string;
htmlInput?: string;
context?: Context;
props?: Context;
},
) => string;
type DataSource = { operation: string; parameters?: unknown[] };
type DataSources = Record<string, (...args: any) => unknown>;
type LoadedPlugin = {
plugin: { meta: Plugin["meta"]; api: PluginApi; context: Context };
tasks: Tasks;
};
type PluginsDefinition = {
env: Record<string, string>;
plugins: PluginOptions[];
};
type PluginOptions = {
// The idea is that the consumer can provide an already loaded module. This
// is needed for environments like the browser or edge computing platforms.
module: Plugin;
path: never;
options: Record<string, unknown>;
} | {
module: never;
path: string;
options: Record<string, unknown>;
};
type Meta = Record<string, string>;
type Mode = "development" | "production";
// This is the context used when rendering a page
type Context = Record<string, unknown>;
// TODO: Rename as PluginModule for clarity
type Plugin<O = Record<string, unknown>, C = Context> = {
meta: {
name: string;
description: string;
dependsOn?: string[];
};
init(args: PluginParameters<O>): Promise<PluginApi<C>> | PluginApi<C>;
};
type PluginParameters<O = Record<string, unknown>> = {
cwd: string;
load: LoadApi;
renderComponent: Render;
renderComponentSync: RenderSync;
mode: Mode;
outputDirectory: string;
options: O;
};
type InitLoadApi = (tasks: Tasks) => LoadApi;
type LoadApi = {
dir({ path, extension, recursive, type }: {
path: string;
type: string;
extension?: string;
recursive?: boolean;
}): Promise<{ name: string; path: string }[]>;
json<T>({ path, type }: { path: string; type: string }): Promise<T>;
module<T>({ path, type }: { path: string; type: string }): Promise<T>;
textFile(path: string): Promise<string>;
textFileSync(path: string): string;
};
type PluginApi<C = Context> = {
// Set up initial plugin context during plugin initialization phase.
initPluginContext?(): C | Promise<C>;
// Send messages to other plugins before other hooks are applied. This
// is useful for giving specific instructions on what to do.
sendMessages?(
{ send, pluginContext }: { send: Send; pluginContext: C },
): Promise<Tasks | void> | Tasks | void;
// Return additional tasks to perform per build preparation
prepareBuild?(
{ send, pluginContext }: { send: Send; pluginContext: C },
): Promise<Tasks | void> | Tasks | void;
// Return additional tasks to perform per build finishing
finishBuild?(
{ send, pluginContext }: { send: Send; pluginContext: C },
): Promise<Tasks | void> | Tasks | void;
// Optional cleanup for global operations that should happen only once per process
cleanUp?(
{ routes, pluginContext }: { routes: Routes; pluginContext: C },
): void;
// Run setup before context is resolved or add something to it
prepareContext?(o: {
send: Send;
route: Route;
url: string;
pluginContext: C;
}):
| Promise<{ context: Record<string, unknown> } | void>
| {
context: Record<string, unknown>;
}
| void;
beforeEachRender?(o: {
context: Context;
send: Send;
route: Route;
url: string;
pluginContext: C;
}):
| Promise<
Tasks | void
>
| Tasks
| void;
renderLayout?(o: {
matchRoute: MatchRoute;
route: Route;
context: Context;
send: Send;
url: string;
pluginContext: C;
}): Promise<string> | string;
renderComponent?(o: {
matchRoute: MatchRoute;
componentName?: string;
htmlInput?: string;
context: Context;
props: Context;
pluginContext: C;
}): Promise<string>;
renderComponentSync?(o: {
matchRoute: MatchRoute;
componentName?: string;
htmlInput?: string;
context: Context;
props: Context;
pluginContext: C;
}): string;
afterEachRender?(o: {
markup: string;
context: Context;
route: Route;
send: Send;
url: string;
pluginContext: C;
}): Promise<{ markup: string }> | { markup: string };
onMessage?(o: { message: SendMessageEvent; pluginContext: C; send: Send }):
| void
| {
send?: SendMessageEvent[];
pluginContext?: Partial<C>;
result?: unknown;
}
| Promise<
void | {
send?: SendMessageEvent[];
pluginContext?: Partial<C>;
result?: unknown;
}
>;
getAllRoutes?(o: { pluginContext: C }):
| Promise<{ routes: Record<string, Route>; tasks: Tasks }>
| { routes: Record<string, Route>; tasks: Tasks };
matchRoute?(
url: string,
pluginContext: C,
): Promise<Route | undefined> | undefined;
onTasksRegistered?({ send, tasks }: { tasks: Tasks; send: Send }): void;
};
type Send = (
pluginName: string,
{ type, payload }: SendMessageEvent,
) => Promise<unknown> | unknown;
// TODO: Compose this from plugin types
// TODO: Model an accurate return type for each event (fixes plugins.ts a lot)
type SendMessageEvent =
| { type: "ping"; payload: undefined }
| { type: "getStyleSetupPath"; payload: undefined }
| { type: "getMeta"; payload: undefined }
| { type: "getComponents"; payload: undefined }
| { type: "updateComponents"; payload: Record<string, Element> }
| { type: "getRenderer"; payload: string }
// websocket plugin
| { type: "reloadPage"; payload: undefined }
| {
type: "fileChanged";
payload: {
path: string;
// Duplicated from Deno type to avoid shimming with dnt
// event: Deno.FsEvent;
event: {
/** The kind/type of the file system event. */
kind: "any" | "access" | "create" | "modify" | "remove" | "other";
/** An array of paths that are associated with the file system event. */
paths: string[];
/** Any additional flags associated with the event. */
flag?: "rescan";
};
extension: string;
name: string;
type: string;
};
}
// router plugin
| {
type: "addDynamicRoute";
payload: {
path: string;
};
}
// editor plugin
| {
type: "addGlobalScripts";
payload: {
type: string;
src: string;
}[];
}
| {
type: "addScripts";
payload: {
isExternal?: boolean;
localPath: string;
remotePath: string;
name: string;
externals?: string[];
}[];
};
// This type is specific to breezewind-router so it probably doesn't belong here
type Routes = Record<string, Route>;
type Route = {
context?: DataContext;
// TODO: This should come as an extension from the renderer plugin
// if it is enabled
layout: string;
// TODO: This should come as an extension from the script plugin
// if it is enabled
scripts?: { name: string }[]; // These point to scripts directory by name
routes?: Routes;
parentDataSources?: Record<string, unknown>;
dataSources?: Record<string, DataSource>;
expand?: {
context?: DataContext;
matchBy?: {
indexer: DataSource;
slug: string;
name: string;
};
dataSources?: Record<string, DataSource>;
layout: string;
};
};
type Scripts = Script[];
type Script = { type: string; src: string };
type Tasks = (BuildWorkerEvent)[];
type BuildWorkerEvent =
| {
type: "init";
payload: {
cwd: string;
pluginDefinitions: PluginOptions[];
outputDirectory: string;
};
}
| {
type: "build";
payload: {
route: Route;
dir: string;
url: string;
};
}
| {
type: "listDirectory";
payload: { path: string; type: string };
}
| {
type: "loadJSON";
payload: { path: string; type: string };
}
| {
type: "loadModule";
payload: { path: string; type: string };
}
| {
type: "readTextFile";
payload: { path: string; type: string };
}
| {
type: "writeFile";
payload: {
outputDirectory: string;
file: string;
data: string | Uint8Array;
};
}
| {
type: "writeTextFile";
payload: {
outputDirectory: string;
file: string;
data: string | Uint8Array;
};
}
| {
type: "copyFiles";
payload: {
inputDirectory: string;
outputDirectory: string;
outputPath: string;
};
};
type BuildWorkerMessageTypes = "finished" | "addTasks";
type GlobalUtilities = {
init: (o: {
load: LoadApi;
render: Render;
renderSync: RenderSync;
matchRoute: MatchRoute;
url: string;
}) => Utilities;
};
type MatchRoute = (url: string) => Promise<Route | undefined>;
export type {
Attributes,
BuildWorkerEvent,
BuildWorkerMessageTypes,
Category,
Context,
DataContext,
DataSource,
DataSources,
DataSourcesApi,
DataSourcesModule,
GlobalUtilities,
InitLoadApi,
LoadApi,
LoadedPlugin,
MatchRoute,
Meta,
Mode,
ParentCategory,
Plugin,
PluginApi,
PluginOptions,
PluginParameters,
PluginsDefinition,
Props,
Render,
RenderSync,
Route,
Routes,
Scripts,
Send,
Tasks,
Utilities,
Utility,
};