|
| 1 | +import { pascalCase, uppercaseFirstLetter } from "@root/api/writer-generator/utils"; |
| 2 | +import { Writer, type WriterOptions } from "@root/api/writer-generator/writer"; |
| 3 | +import type { |
| 4 | + Identifier, |
| 5 | + ProfileTypeSchema, |
| 6 | + RegularTypeSchema, |
| 7 | + TypeSchema, |
| 8 | +} from "@root/typeschema/types"; |
| 9 | +import { isProfileTypeSchema } from "@root/typeschema/types"; |
| 10 | +import { groupByPackages, type TypeSchemaIndex } from "@root/typeschema/utils"; |
| 11 | +import { |
| 12 | + generateComplexTypeReexports, |
| 13 | + generateDependenciesImports, |
| 14 | +} from "./imports"; |
| 15 | +import { |
| 16 | + generateAttachProfile, |
| 17 | + generateExtractProfile, |
| 18 | + generateProfileType, |
| 19 | +} from "./profile"; |
| 20 | +import { generateProfileAdapter } from "./profile-adapter"; |
| 21 | +import { generateProfileFactory } from "./profile-factory"; |
| 22 | +import { generateNestedTypes, generateType } from "./resource"; |
| 23 | +import { |
| 24 | + tsFhirPackageDir, |
| 25 | + tsModuleFileName, |
| 26 | + tsModuleName, |
| 27 | + tsResourceName, |
| 28 | +} from "./utils"; |
| 29 | + |
| 30 | +export type TypeScriptOptions = WriterOptions; |
| 31 | + |
| 32 | +/** |
| 33 | + * TypeScript code generator |
| 34 | + * Extends Writer base class for file system operations |
| 35 | + */ |
| 36 | +export class TypeScript extends Writer { |
| 37 | + /** |
| 38 | + * Generate package index file (exports all types) |
| 39 | + */ |
| 40 | + generateFhirPackageIndexFile(schemas: TypeSchema[]): void { |
| 41 | + this.cat("index.ts", () => { |
| 42 | + let exports = schemas |
| 43 | + .map((schema) => ({ |
| 44 | + identifier: schema.identifier, |
| 45 | + tsPackageName: tsModuleName(schema.identifier), |
| 46 | + resourceName: tsResourceName(schema.identifier), |
| 47 | + })) |
| 48 | + .sort((a, b) => a.resourceName.localeCompare(b.resourceName)); |
| 49 | + |
| 50 | + // FIXME: actually, duplication may mean internal error... |
| 51 | + exports = Array.from( |
| 52 | + new Map( |
| 53 | + exports.map((exp) => [exp.resourceName.toLowerCase(), exp]), |
| 54 | + ).values(), |
| 55 | + ).sort((a, b) => a.resourceName.localeCompare(b.resourceName)); |
| 56 | + |
| 57 | + for (const exp of exports) { |
| 58 | + this.debugComment(exp.identifier); |
| 59 | + this.lineSM( |
| 60 | + `export type { ${exp.resourceName} } from "./${exp.tsPackageName}"`, |
| 61 | + ); |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Generate a single resource/profile module file |
| 68 | + */ |
| 69 | + generateResourceModule(tsIndex: TypeSchemaIndex, schema: TypeSchema): void { |
| 70 | + this.cat(tsModuleFileName(schema.identifier), () => { |
| 71 | + this.generateDisclaimer(); |
| 72 | + |
| 73 | + if (["complex-type", "resource", "logical"].includes(schema.identifier.kind)) { |
| 74 | + this.generateResourceFile(tsIndex, schema as RegularTypeSchema); |
| 75 | + } else if (isProfileTypeSchema(schema)) { |
| 76 | + this.generateProfileFile(tsIndex, schema); |
| 77 | + } else { |
| 78 | + throw new Error( |
| 79 | + `Profile generation not implemented for kind: ${schema.identifier.kind}`, |
| 80 | + ); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Generate resource/complex type file |
| 87 | + */ |
| 88 | + private generateResourceFile( |
| 89 | + tsIndex: TypeSchemaIndex, |
| 90 | + schema: RegularTypeSchema, |
| 91 | + ): void { |
| 92 | + // Generate imports |
| 93 | + generateDependenciesImports(this, schema); |
| 94 | + |
| 95 | + // Re-export complex types |
| 96 | + generateComplexTypeReexports(this, schema); |
| 97 | + |
| 98 | + // Generate nested types |
| 99 | + generateNestedTypes(this, tsIndex, schema); |
| 100 | + |
| 101 | + // Add canonical URL comment |
| 102 | + this.comment("CanonicalURL:", schema.identifier.url); |
| 103 | + |
| 104 | + // Generate main interface |
| 105 | + generateType(this, tsIndex, schema); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Generate profile file using adapter pattern |
| 110 | + */ |
| 111 | + private generateProfileFile( |
| 112 | + tsIndex: TypeSchemaIndex, |
| 113 | + schema: ProfileTypeSchema, |
| 114 | + ): void { |
| 115 | + // Generate imports for base resource and dependencies |
| 116 | + generateDependenciesImports(this, schema); |
| 117 | + |
| 118 | + // Add canonical URL comment |
| 119 | + this.comment("CanonicalURL:", schema.identifier.url || ""); |
| 120 | + this.line(); |
| 121 | + |
| 122 | + // Generate adapter class |
| 123 | + generateProfileAdapter(this, tsIndex, schema); |
| 124 | + this.line(); |
| 125 | + |
| 126 | + // Generate factory function |
| 127 | + generateProfileFactory(this, schema); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Main generation entry point |
| 132 | + */ |
| 133 | + override generate(tsIndex: TypeSchemaIndex): void { |
| 134 | + // Collect all types to generate |
| 135 | + const typesToGenerate = [ |
| 136 | + ...tsIndex.collectComplexTypes(), |
| 137 | + ...tsIndex.collectResources(), |
| 138 | + // ...tsIndex.collectLogicalModels(), |
| 139 | + ...tsIndex |
| 140 | + .collectProfiles() |
| 141 | + // NOTE: because non Resource don't have `meta` field |
| 142 | + .filter((p) => tsIndex.isWithMetaField(p)), |
| 143 | + ]; |
| 144 | + |
| 145 | + // Group by package |
| 146 | + const grouped = groupByPackages(typesToGenerate); |
| 147 | + |
| 148 | + // Generate files |
| 149 | + this.cd("/", () => { |
| 150 | + for (const [packageName, packageSchemas] of Object.entries(grouped)) { |
| 151 | + const tsPackageDir = tsFhirPackageDir(packageName); |
| 152 | + this.cd(tsPackageDir, () => { |
| 153 | + for (const schema of packageSchemas) { |
| 154 | + this.generateResourceModule(tsIndex, schema); |
| 155 | + } |
| 156 | + this.generateFhirPackageIndexFile(packageSchemas); |
| 157 | + }); |
| 158 | + } |
| 159 | + }); |
| 160 | + } |
| 161 | +} |
0 commit comments