Skip to content

Commit 0583a6c

Browse files
committed
WIP: moving to new framework
1 parent 311fcba commit 0583a6c

44 files changed

Lines changed: 1607 additions & 1525 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bun.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@inquirer/prompts": "^7.8.1",
7676
"ajv": "^8.17.1",
7777
"handlebars": "^4.7.8",
78+
"mustache": "^4.2.0",
7879
"ora": "^8.2.0",
7980
"picocolors": "^1.1.1",
8081
"yaml": "^2.8.1",
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1+
export type CapitalizeFirst<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;
12

2-
export type CapitalizeFirst<S extends string> = S extends `${infer F}${infer R}`
3-
? `${Uppercase<F>}${R}`
4-
: S;
5-
6-
export type IsPrefixed<T extends string> = `is${CapitalizeFirst<T>}`;
3+
export type IsPrefixed<T extends string> = `is${CapitalizeFirst<T>}`;
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
1-
import {DebugMixin} from "@fscg/generators/mustache/types/DebugMixin";
2-
1+
import type { DebugMixin } from "@mustache/types/DebugMixin";
32

43
export class DebugMixinProvider {
4+
constructor(private readonly mode: "FORMATTED" | "COMPACT") {}
55

6-
constructor(private readonly mode: 'FORMATTED' | 'COMPACT') {
7-
}
8-
9-
public apply<T extends Record<string, unknown>> (target: T): T & DebugMixin {
6+
public apply<T extends Record<string, unknown>>(target: T): T & DebugMixin {
107
return this._addDebug(target) as T & DebugMixin;
118
}
129

1310
private _addDebug(value: unknown): unknown {
1411
if (Array.isArray(value)) {
15-
return value.map(v => this._addDebug(v));
12+
return value.map((v) => this._addDebug(v));
1613
}
1714

1815
if (value !== null && typeof value === "object") {
1916
const obj = value as Record<string, unknown>;
2017
const result: Record<string, unknown> = {};
21-
const debugString = JSON.stringify(
22-
obj,
23-
null,
24-
this.mode === 'FORMATTED' ? 2 : undefined
25-
);
18+
const debugString = JSON.stringify(obj, null, this.mode === "FORMATTED" ? 2 : undefined);
2619
for (const [key, val] of Object.entries(obj)) {
2720
result[key] = this._addDebug(val);
2821
}
29-
result["debug"] = debugString;
22+
result.debug = debugString;
3023
return result;
3124
}
3225

3326
return value;
3427
}
35-
}
28+
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import {NameGenerator} from "@fscg/generators/mustache/generator/NameGenerator";
2-
import {LambdaMixin} from "@fscg/generators/mustache/types/LambdaMixin";
3-
import {camelCase, kebabCase, pascalCase, snakeCase} from "@fscg/utils/code";
4-
1+
import type { NameGenerator } from "@mustache/generator/NameGenerator";
2+
import type { LambdaMixin } from "@mustache/types/LambdaMixin";
3+
import { camelCase, kebabCase, pascalCase, snakeCase } from "@root/api/writer-generator/utils";
54

65
export class LambdaMixinProvider {
7-
private readonly lambda: LambdaMixin['lambda'];
6+
private readonly lambda: LambdaMixin["lambda"];
87
constructor(private readonly nameGenerator: NameGenerator) {
98
this.lambda = {
109
saveTypeName: () => (text, render) => this.nameGenerator.generateType(render(text)),
@@ -17,13 +16,13 @@ export class LambdaMixinProvider {
1716
kebabCase: () => (text, render) => kebabCase(render(text)),
1817
lowerCase: () => (text, render) => render(text).toLowerCase(),
1918
upperCase: () => (text, render) => render(text).toUpperCase(),
20-
}
19+
};
2120
}
2221

23-
public apply<T extends Record<string, unknown>> (target: T): T & LambdaMixin {
22+
public apply<T extends Record<string, unknown>>(target: T): T & LambdaMixin {
2423
return {
2524
...target,
2625
lambda: this.lambda,
2726
};
2827
}
29-
}
28+
}

src/generators/mustache/generator/ListElementInformationMixinProvider.ts renamed to src/api/mustache/generator/ListElementInformationMixinProvider.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import {ListElementInformationMixin} from "@fscg/generators/mustache/types/ListElementInformationMixin";
1+
import type { ListElementInformationMixin } from "@mustache/types/ListElementInformationMixin";
22

33
export class ListElementInformationMixinProvider {
4-
private static _array<T> (value: T[] | Set<T>): T[] {
4+
private static _array<T>(value: T[] | Set<T>): T[] {
55
return Array.isArray(value) ? value : Array.from(value);
66
}
77

8-
public apply<T extends Record<string, unknown>>(source: T): T{
8+
public apply<T extends Record<string, unknown>>(source: T): T {
99
return this._addListElementInformation(source) as T;
1010
}
1111

1212
private _addListElementInformation(value: unknown): unknown {
1313
if (Array.isArray(value) || value instanceof Set) {
1414
return ListElementInformationMixinProvider._array(value).map((v, index, array) => {
15-
if(typeof v === 'object' && v !== null){
15+
if (typeof v === "object" && v !== null) {
1616
return {
1717
...(this._addListElementInformation(v) as Record<string, unknown>),
18-
'-index': index,
19-
'-length': array.length,
20-
'-first': index === 0,
21-
'-last': index === array.length - 1,
22-
} satisfies ListElementInformationMixin
18+
"-index": index,
19+
"-length": array.length,
20+
"-first": index === 0,
21+
"-last": index === array.length - 1,
22+
} satisfies ListElementInformationMixin;
2323
}
2424
return v;
2525
});
@@ -36,4 +36,4 @@ export class ListElementInformationMixinProvider {
3636

3737
return value;
3838
}
39-
}
39+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// import path from "node:path";
2+
3+
// import * as Mustache from "mustache";
4+
// import { Generator, type GeneratorOptions } from "../../generator";
5+
// import { ViewModelCache, ViewModelFactory } from "@mustache/generator/ViewModelFactory";
6+
// import { Rendering } from "@mustache/types/Rendering";
7+
// import * as util from "node:util";
8+
// import { TemplateFileCache } from "@mustache/generator/TemplateFileCache";
9+
// import { NamedViewModel } from "@mustache/types/NamedViewModel";
10+
// import { DistinctNameConfigurationType, NameGenerator, NameTransformation } from "@mustache/generator/NameGenerator";
11+
// import { SchemaLoaderFacade } from "@mustache/generator/SchemaLoaderFacade";
12+
// import { View } from "@mustache/types/View";
13+
// import { LambdaMixinProvider } from "@mustache/generator/LambdaMixinProvider";
14+
// import { PrimitiveType } from "@mustache/types/PrimitiveType";
15+
// import { DebugMixinProvider } from "@mustache/generator/DebugMixinProvider";
16+
// import { spawn } from "child_process";
17+
// import { TypeViewModel } from "@mustache/types/TypeViewModel";
18+
// import { HookType } from "@mustache/types/HookType";
19+
// import { ViewModel } from "@mustache/types/ViewModel";
20+
// import { FilterType } from "@mustache/types/FilterType";
21+
22+
// export interface MustacheGeneratorOptions extends GeneratorOptions {
23+
// debug: "OFF" | "FORMATTED" | "COMPACT";
24+
// hooks: {
25+
// afterGenerate?: HookType[];
26+
// };
27+
// meta: {
28+
// timestamp?: string;
29+
// generator?: string;
30+
// };
31+
// filters: {
32+
// resource?: FilterType;
33+
// complexType?: FilterType;
34+
// };
35+
// sources: {
36+
// templateSource: string;
37+
// staticSource: string;
38+
// };
39+
// typeMap: Partial<Record<PrimitiveType, string>>;
40+
// unsaveCharacterPattern: string | RegExp;
41+
// nameTransformations: DistinctNameConfigurationType<NameTransformation[]>;
42+
// renderings: {
43+
// utility: Rendering[];
44+
// resource: Rendering[];
45+
// complexType: Rendering[];
46+
// };
47+
// }
48+
49+
// export class MustacheGenerator extends Generator {
50+
// private static runCommand(cmd, args: string[] = [], options = {}) {
51+
// return new Promise((resolve, reject) => {
52+
// const child = spawn(cmd, args, {
53+
// stdio: "inherit",
54+
// ...options,
55+
// });
56+
// child.on("error", reject);
57+
// child.on("close", (code) => {
58+
// if (code === 0) resolve(code);
59+
// else reject(new Error(`Prozess beendet mit Fehlercode ${code}`));
60+
// });
61+
// });
62+
// }
63+
64+
// private readonly templateFileCache: TemplateFileCache;
65+
// private readonly mustacheGeneratorOptions: MustacheGeneratorOptions;
66+
// private readonly nameGenerator: NameGenerator;
67+
// private readonly lambdaMixinProvider: LambdaMixinProvider;
68+
// private readonly debugMixinProvider?: DebugMixinProvider;
69+
70+
// constructor(opts: MustacheGeneratorOptions) {
71+
// super({
72+
// ...opts,
73+
// typeMap: opts.typeMap,
74+
// staticDir: path.resolve(opts.sources.staticSource),
75+
// });
76+
// this.mustacheGeneratorOptions = opts;
77+
// this.nameGenerator = new NameGenerator(
78+
// opts.keywords ?? new Set<string>(),
79+
// opts.typeMap,
80+
// opts.nameTransformations,
81+
// opts.unsaveCharacterPattern,
82+
// );
83+
// this.templateFileCache = new TemplateFileCache(opts.sources.templateSource);
84+
// this.lambdaMixinProvider = new LambdaMixinProvider(this.nameGenerator);
85+
// this.debugMixinProvider = opts.debug != "OFF" ? new DebugMixinProvider(opts.debug) : undefined;
86+
// }
87+
// public async generate() {
88+
// this.clear();
89+
// const schemaLoaderFacade = new SchemaLoaderFacade(this.loader, this.mustacheGeneratorOptions.filters);
90+
// const modelFactory = new ViewModelFactory(schemaLoaderFacade, this.nameGenerator);
91+
92+
// const cache: ViewModelCache = {
93+
// resourcesByUri: {},
94+
// complexTypesByUri: {},
95+
// };
96+
// schemaLoaderFacade
97+
// .getComplexTypes()
98+
// .map((typeRef) => modelFactory.createComplexType(typeRef, cache))
99+
// .forEach(this._renderComplexType.bind(this));
100+
101+
// schemaLoaderFacade
102+
// .getResources()
103+
// .map((typeRef) => modelFactory.createResource(typeRef, cache))
104+
// .forEach(this._renderResource.bind(this));
105+
106+
// this._renderUtility(modelFactory.createUtility());
107+
108+
// this.copyStaticFiles();
109+
110+
// await this._runHooks(this.mustacheGeneratorOptions.hooks.afterGenerate);
111+
// }
112+
113+
// private async _runHooks(hooks?: HookType[]) {
114+
// for (const hook of hooks ?? []) {
115+
// console.info(`Running hook: ${hook.cmd} ${hook.args?.join(" ")}`);
116+
// await MustacheGenerator.runCommand(hook.cmd, hook.args ?? [], {
117+
// cwd: this.mustacheGeneratorOptions.outputDir,
118+
// });
119+
// console.info(`Completed hook: ${hook.cmd} ${hook.args?.join(" ")}`);
120+
// }
121+
// }
122+
123+
// private _checkRenderingFilter(model: TypeViewModel, rendering: Rendering): boolean {
124+
// if (!rendering.filter?.whitelist?.length && !rendering.filter?.blacklist?.length) {
125+
// return true;
126+
// }
127+
// if ((rendering.filter?.blacklist ?? []).find((v) => model.name.match(v))) {
128+
// return false;
129+
// }
130+
// if ((rendering.filter?.whitelist ?? []).find((v) => model.name.match(v))) {
131+
// return true;
132+
// }
133+
// return !rendering.filter.whitelist?.length;
134+
// }
135+
136+
// private _renderUtility(model: ViewModel) {
137+
// this.mustacheGeneratorOptions.renderings.utility.forEach((rendering) => {
138+
// this.dir(rendering.path, () => {
139+
// this.file(rendering.fileNameFormat, () => {
140+
// this.write(this._render(model, rendering));
141+
// });
142+
// });
143+
// });
144+
// }
145+
146+
// private _renderResource(model: TypeViewModel) {
147+
// this.mustacheGeneratorOptions.renderings.resource
148+
// .filter((rendering) => this._checkRenderingFilter(model, rendering))
149+
// .forEach((rendering) => {
150+
// this.dir(rendering.path, () => {
151+
// this.file(this._calculateFilename(model, rendering), () => {
152+
// this.write(this._render(model, rendering));
153+
// });
154+
// });
155+
// });
156+
// }
157+
158+
// private _renderComplexType(model: TypeViewModel) {
159+
// this.mustacheGeneratorOptions.renderings.complexType
160+
// .filter((rendering) => this._checkRenderingFilter(model, rendering))
161+
// .forEach((rendering) => {
162+
// this.dir(rendering.path, () => {
163+
// this.file(this._calculateFilename(model, rendering), () => {
164+
// this.write(this._render(model, rendering));
165+
// });
166+
// });
167+
// });
168+
// }
169+
170+
// private _calculateFilename(model: NamedViewModel, rendering: Rendering): string {
171+
// return util.format(rendering.fileNameFormat, model.saveName);
172+
// }
173+
174+
// private _render<T extends ViewModel>(model: T, rendering: Rendering): string {
175+
// let view: View<T> = this.lambdaMixinProvider.apply({
176+
// meta: {
177+
// timestamp: this.mustacheGeneratorOptions.meta.timestamp ?? new Date().toISOString(),
178+
// generator: this.mustacheGeneratorOptions.meta.generator ?? "FHIR-Schema Mustache Generator",
179+
// },
180+
// model: model,
181+
// properties: rendering.properties ?? {},
182+
// });
183+
// if (this.debugMixinProvider) {
184+
// view = this.debugMixinProvider.apply(view);
185+
// }
186+
// return Mustache.render(this.templateFileCache.read(rendering), view, (partialName) =>
187+
// this.templateFileCache.readTemplate(partialName),
188+
// );
189+
// }
190+
// }

0 commit comments

Comments
 (0)