-
Notifications
You must be signed in to change notification settings - Fork 1
/
typer.ts
79 lines (71 loc) · 2.17 KB
/
typer.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
import { govnData as gd, inflect, path } from "./deps.ts";
// deno-lint-ignore no-empty-interface
export interface LhcFormContainerOptions {
}
export abstract class LhcFormContainer implements gd.StructuredDataTyper {
constructor(readonly options?: LhcFormContainerOptions) {
}
destFileName(fc: gd.FileContext): string {
return fc.forceExtension(".ts");
}
className(fileNameIV: inflect.InflectableValue, fc: gd.FileContext): string {
return inflect.toPascalCase(fileNameIV);
}
isTypeable(
ctx: gd.StructuredDataTyperContext,
): gd.StructuredDataTyperContext | false {
if (gd.isJsonSupplierEntryContext(ctx.udseCtx)) {
const result: gd.JsonTyperContext = {
...ctx,
isJsonTyperContext: true,
jseCtx: ctx.udseCtx,
};
return result;
}
return false;
}
protected typerResult(
ctx: gd.StructuredDataTyperContext,
textResult: string,
destFileName?: string,
): gd.JsonTyperTextResult {
const result: gd.JsonTyperTextResult = {
isStructuredDataTyperResult: true,
isJsonTyperTextResult: true,
udseCtx: ctx.udseCtx,
text: textResult,
};
if (destFileName) {
const enhanced: gd.JsonTyperTextResult & gd.FileDestinationResult = {
...result,
destFileName: destFileName,
destFileNameRel: (relTo: string): string => {
return path.relative(relTo, destFileName);
},
};
return enhanced;
}
return result;
}
protected abstract typerContent(
ctx: gd.JsonTyperContext,
fc: gd.FileContext,
): string;
typeData(
ctx: gd.StructuredDataTyperContext,
): gd.JsonTyperTextResult {
let textResult, destFileName: string | undefined;
if (gd.isJsonTyperContext(ctx)) {
if (gd.isFileContext(ctx.udseCtx)) {
destFileName = this.destFileName(ctx.udseCtx);
textResult = this.typerContent(ctx, ctx.udseCtx);
} else {
textResult =
`ctx.jseCtx is expected to be a FileContext instance: ${ctx.jseCtx}`;
}
} else {
textResult = `ctx is expected to be a JsonTyperContext instance: ${ctx}`;
}
return this.typerResult(ctx, textResult, destFileName);
}
}