@atomic-ehr/codegen 0.0.2-canary.20251119090005.cdde040 → 0.0.2-canary.20251119112501.fdc096d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/index.js +8 -8
- package/dist/index.d.ts +10 -1
- package/dist/index.js +16 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
2
|
+
import Ii from'yargs';import {hideBin}from'yargs/helpers';import'perf_hooks';import'ora';import b from'picocolors';import*as F from'fs';import F__default,{existsSync,mkdirSync}from'fs';import*as R from'path';import R__default,{dirname,resolve,join,relative}from'path';import*as L from'fs/promises';import {mkdir,writeFile,readFile,readdir,stat,unlink,access,rm}from'fs/promises';import {CanonicalManager}from'@atomic-ehr/fhir-canonical-manager';import*as jt from'@atomic-ehr/fhirschema';import {isStructureDefinition}from'@atomic-ehr/fhirschema';import*as tr from'yaml';var Ae=Object.defineProperty;var xr=Object.getOwnPropertyDescriptor;var wr=Object.getOwnPropertyNames;var kr=Object.prototype.hasOwnProperty;var Cr=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var le=(n,e)=>()=>(n&&(e=n(n=0)),e);var je=(n,e)=>{for(var t in e)Ae(n,t,{get:e[t],enumerable:true});},Fr=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of wr(e))!kr.call(n,i)&&i!==t&&Ae(n,i,{get:()=>e[i],enumerable:!(r=xr(e,i))||r.enumerable});return n};var Ve=n=>Fr(Ae({},"__esModule",{value:true}),n);var $,Ce,ie,B,Fe,$e,ne=le(()=>{$=class extends Error{constructor(t,r,i){super(t);this.phase=r;this.context=i;this.name=this.constructor.name,this.timestamp=new Date,this.errorId=this.generateErrorId(),Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor);}timestamp;errorId;generateErrorId(){return `${this.constructor.name}-${Date.now()}-${Math.random().toString(36).substr(2,9)}`}getDetailedMessage(){let t=[`\u274C ${this.constructor.name}: ${this.message}`,` Error ID: ${this.errorId}`,` Phase: ${this.phase}`,` Time: ${this.timestamp.toISOString()}`];return this.context&&Object.keys(this.context).length>0&&(t.push(""),t.push("\u{1F4CD} Context:"),Object.entries(this.context).forEach(([r,i])=>{t.push(` ${r}: ${this.formatContextValue(i)}`);})),t.join(`
|
|
3
3
|
`)}formatContextValue(t){if(t==null)return String(t);if(typeof t=="string")return `"${t}"`;if(typeof t=="object")try{return JSON.stringify(t,null,2)}catch{return "[Object]"}return String(t)}getSeverity(){return "error"}isRecoverable(){return false}getDocumentationLinks(){return ["https://github.com/atomic-ehr/codegen/docs/troubleshooting.md",`https://github.com/atomic-ehr/codegen/docs/errors/${this.constructor.name}.md`]}},Ce=class extends ${constructor(t,r,i,s){super(t,"validation",{schemaName:r.identifier?.name||"unknown",schemaKind:r.identifier?.kind||"unknown",schemaPackage:r.identifier?.package||"unknown",validationErrors:i,userContext:s});this.schema=r;this.validationErrors=i;this.userContext=s;}getSuggestions(){let t=[];t.push("Verify the schema follows the TypeSchema specification"),t.push("Check that all required fields are present and properly typed");for(let r of this.validationErrors)r.includes("identifier.name")&&(t.push("\u2728 Add a valid identifier.name field to your schema"),t.push('\u{1F4A1} Example: identifier: { name: "Patient", kind: "resource" }')),r.includes("identifier.kind")&&(t.push('\u{1F3AF} Set identifier.kind to: "resource", "complex-type", "profile", or "primitive-type"'),t.push("\u{1F4DA} Check FHIR specification for the correct kind value")),r.includes("circular")&&(t.push("\u{1F504} Remove circular references between schemas"),t.push("\u{1F4A1} Use forward declarations for recursive types"),t.push("\u{1F50D} Look for schemas that reference each other in a loop")),r.includes("fields")&&(t.push("\u{1F4DD} Check that all fields have proper type definitions"),t.push("\u{1F527} Ensure field types reference valid TypeSchema identifiers"));return this.userContext?.isBeginnerMode&&(t.push(""),t.push("\u{1F393} Beginner Tips:"),t.push("\u{1F4D6} Start with the Quick Start guide: docs/getting-started/quick-start.md"),t.push("\u{1F50D} Use --verbose flag for detailed error information"),t.push("\u{1F9EA} Test with a simple schema first to verify your setup"),this.userContext.previousSuccessfulSchemas?.length&&t.push(`\u2705 Compare with your working schema: ${this.userContext.previousSuccessfulSchemas[0]}`)),this.userContext?.isBeginnerMode||(t.push(""),t.push("\u{1F527} Advanced Debugging:"),t.push("\u{1F575}\uFE0F Enable schema validation debugging"),t.push("\u{1F4CA} Check schema statistics and complexity metrics"),t.push("\u26A1 Consider schema preprocessing if dealing with complex inheritance")),t}isRecoverable(){return this.validationErrors.every(t=>!t.includes("circular")&&!t.includes("corruption"))}},ie=class extends ${constructor(t,r,i,s){super(t,"generation",{templateName:r,contextKeys:Object.keys(i),availableTemplates:s?.availableTemplates?.length||0,debugInfo:s});this.templateName=r;this.templateContext=i;this.debugInfo=s;}getSuggestions(){let t=[];if(this.debugInfo?.availableTemplates?.length&&!this.debugInfo.availableTemplates.includes(this.templateName)){t.push(`\u274C Template '${this.templateName}' not found`),t.push("\u{1F4C2} Available templates:"),this.debugInfo.availableTemplates.forEach(i=>{t.push(` \u2022 ${i}`);});let r=this.findSimilarTemplates(this.templateName,this.debugInfo.availableTemplates);r.length>0&&(t.push("\u{1F914} Did you mean:"),r.forEach(i=>{t.push(` \u2022 ${i}`);}));}if(this.debugInfo?.missingVariables?.length){t.push("\u{1F4DD} Missing template variables:"),this.debugInfo.missingVariables.forEach(i=>{t.push(` \u2022 ${i}`);}),t.push("\u{1F4A1} Add these variables to your template context");let r=Object.keys(this.templateContext);this.debugInfo.missingVariables.forEach(i=>{let s=r.filter(o=>this.levenshteinDistance(i.toLowerCase(),o.toLowerCase())<=2);s.length>0&&t.push(` Similar to: ${s.join(", ")}`);});}if(this.debugInfo?.lineNumber&&(t.push(`\u{1F41B} Check template syntax around line ${this.debugInfo.lineNumber}`),this.debugInfo.columnNumber&&t.push(` Column: ${this.debugInfo.columnNumber}`),this.debugInfo.templateSource)){let i=this.debugInfo.templateSource.split(`
|
|
4
4
|
`)[this.debugInfo.lineNumber-1];i&&t.push(` Code: ${i.trim()}`);}return t.push("\u{1F527} Template debugging steps:"),t.push(" \u2022 Enable template debugging: { debug: true }"),t.push(" \u2022 Verify template file exists and has correct syntax"),t.push(" \u2022 Check template variable names match context keys"),t.push(" \u2022 Ensure template engine is properly configured"),t}findSimilarTemplates(t,r){return r.filter(i=>{let s=this.levenshteinDistance(t.toLowerCase(),i.toLowerCase());return s<=2&&s>0}).slice(0,3).sort((i,s)=>this.levenshteinDistance(t.toLowerCase(),i.toLowerCase())-this.levenshteinDistance(t.toLowerCase(),s.toLowerCase()))}levenshteinDistance(t,r){let i=Array(r.length+1).fill(null).map(()=>Array(t.length+1).fill(0));for(let s=0;s<=t.length;s++)i[0][s]=s;for(let s=0;s<=r.length;s++)i[s][0]=s;for(let s=1;s<=r.length;s++)for(let o=1;o<=t.length;o++){let a=t[o-1]===r[s-1]?0:1;i[s][o]=Math.min(i[s]?.[o-1]+1,i[s-1]?.[o]+1,i[s-1]?.[o-1]+a);}return i[r.length]?.[t.length]}isRecoverable(){return true}},B=class extends ${constructor(t,r,i,s,o){super(t,"writing",{operation:r,filePath:i,originalErrorMessage:s?.message,originalErrorCode:s?.code,recoveryOptions:o});this.operation=r;this.filePath=i;this.originalError=s;this.recoveryOptions=o;}getSuggestions(){let t=[],r=this.originalError?.code;switch(this.operation){case "create":case "write":t.push("\u{1F4C1} File writing troubleshooting:"),t.push(" \u2022 Check if the output directory exists"),t.push(" \u2022 Verify write permissions for the target directory"),t.push(" \u2022 Ensure no other process has the file locked"),this.filePath.includes(" ")&&t.push(" \u2022 File path contains spaces - check path escaping"),this.recoveryOptions?.alternativePaths?.length&&(t.push("\u{1F504} Alternative output directories:"),this.recoveryOptions.alternativePaths.forEach(i=>{t.push(` \u2022 ${i}`);}));break;case "read":t.push("\u{1F4D6} File reading troubleshooting:"),t.push(" \u2022 Verify the file exists at the specified path"),t.push(" \u2022 Check file read permissions"),t.push(" \u2022 Ensure file is not corrupted");break;case "delete":t.push("\u{1F5D1}\uFE0F File deletion troubleshooting:"),t.push(" \u2022 Check if file is locked by another process"),t.push(" \u2022 Verify delete permissions for the directory"),t.push(" \u2022 Ensure file exists before attempting deletion");break}switch(r){case "EACCES":t.push("\u{1F510} Permission Error:"),t.push(" \u2022 Current user lacks necessary file permissions"),this.recoveryOptions?.permissionFix?t.push(` \u2022 Fix command: ${this.recoveryOptions.permissionFix}`):t.push(` \u2022 Try: chmod 755 "${this.filePath}"`),t.push(" \u2022 Consider running with elevated permissions"),t.push(" \u2022 Check directory ownership and permissions");break;case "ENOSPC":if(t.push("\u{1F4BE} Disk Space Error:"),t.push(" \u2022 Insufficient disk space available"),t.push(" \u2022 Free up disk space and retry"),t.push(" \u2022 Consider using a different output directory"),this.recoveryOptions?.diskSpaceRequired){let i=Math.ceil(this.recoveryOptions.diskSpaceRequired/1024/1024);t.push(` \u2022 Required space: ~${i}MB`);}break;case "ENOENT":t.push("\u{1F4C2} File Not Found:"),t.push(" \u2022 File or directory does not exist"),t.push(" \u2022 Check the file path for typos"),t.push(" \u2022 Ensure parent directories exist"),t.push(` \u2022 Create directory: mkdir -p "$(dirname "${this.filePath}")"`);break;case "EMFILE":case "ENFILE":t.push("\u{1F4CA} Too Many Open Files:"),t.push(" \u2022 System has reached file handle limit"),t.push(" \u2022 Close unused files and retry"),t.push(" \u2022 Consider processing files in smaller batches");break}return this.recoveryOptions?.canRetry&&(t.push(""),t.push("\u{1F504} Recovery Options:"),t.push(" \u2022 This operation can be retried safely"),t.push(" \u2022 Fix the underlying issue and run again")),t.push(""),t.push("\u{1F50D} General Debugging:"),t.push(" \u2022 Check system logs for more details"),t.push(" \u2022 Verify disk health if errors persist"),t.push(" \u2022 Test with a simpler file path"),t}isRecoverable(){return this.recoveryOptions?.canRetry||false}getRecoveryActions(){let t=[];switch(this.originalError?.code){case "EACCES":t.push({action:"Fix file permissions",command:`chmod 755 "${this.filePath}"`,automatic:false,riskLevel:"medium"});break;case "ENOENT":t.push({action:"Create missing directory",command:`mkdir -p "$(dirname "${this.filePath}")"`,automatic:true,riskLevel:"low"});break;case "ENOSPC":t.push({action:"Free up disk space",automatic:false,riskLevel:"low"});break}return t}},Fe=class extends ${constructor(t,r,i,s,o){super(t,"initialization",{configKey:r,providedValue:i,providedType:typeof i,expectedValue:s,validOptions:o});this.configKey=r;this.providedValue=i;this.expectedValue=s;this.validOptions=o;}getSuggestions(){let t=[];switch(t.push(`\u2699\uFE0F Configuration error for '${this.configKey}'`),t.push(` Provided: ${JSON.stringify(this.providedValue)} (${typeof this.providedValue})`),this.expectedValue&&t.push(` Expected: ${this.expectedValue}`),this.validOptions?.length&&(t.push(" Valid options:"),this.validOptions.forEach(r=>{t.push(` \u2022 ${JSON.stringify(r)}`);})),t.push(""),t.push("\u{1F527} Configuration fixes:"),t.push(` \u2022 Check the '${this.configKey}' value in your configuration`),t.push(" \u2022 Refer to the configuration documentation"),t.push(" \u2022 Use TypeScript for better config validation"),t.push(" \u2022 Check for typos in configuration keys"),this.configKey){case "outputDir":t.push(" \u2022 Ensure the output directory path exists"),t.push(" \u2022 Use absolute paths for better reliability");break;case "logger":t.push(" \u2022 Provide a valid logger instance"),t.push(" \u2022 Use createLogger() from utils/codegen-logger");break;case "validation":t.push(' \u2022 Set to true, false, or "strict"');break}return t}isRecoverable(){return true}},$e=class extends ${constructor(t,r){super(t,"generation",{errorCount:r.length,errorTypes:[...new Set(r.map(i=>i.constructor.name))],phases:[...new Set(r.map(i=>i.phase))]});this.errors=r;}getSuggestions(){let t=this.errors.flatMap(a=>a.getSuggestions()),r=[...new Set(t)],i=[];i.push(`\u{1F4CA} Batch operation failed with ${this.errors.length} errors:`);let s=new Map;this.errors.forEach(a=>{let c=a.constructor.name;s.has(c)||s.set(c,[]),s.get(c)?.push(a);}),i.push(""),i.push("\u{1F50D} Error breakdown:");for(let[a,c]of s)i.push(` \u2022 ${a}: ${c.length} occurrences`);i.push("\u{1F4A1} Most relevant suggestions:");let o=r.slice(0,8).map(a=>` ${a}`);return i.push(...o),i}getErrorBreakdown(){let t=[];return this.errors.forEach((r,i)=>{t.push(`${i+1}. ${r.constructor.name}: ${r.message}`),r.context?.schemaName&&t.push(` Schema: ${r.context.schemaName}`),r.context?.filename&&t.push(` File: ${r.context.filename}`),t.push("");}),t.join(`
|
|
5
5
|
`)}isRecoverable(){return this.errors.some(t=>t.isRecoverable())}getRecoverableErrors(){return this.errors.filter(t=>t.isRecoverable())}getNonRecoverableErrors(){return this.errors.filter(t=>!t.isRecoverable())}};});var nr={};je(nr,{FileBuilder:()=>ut});var ut,sr=le(()=>{ne();ut=class{config;content="";imports=new Map;exports=new Set;metadata=new Map;beforeSaveHooks=[];afterSaveHooks=[];errorHooks=[];options={template:void 0,importStrategy:"auto",validation:"strict",prettify:true,formatting:{indentSize:2,useTabs:false,maxLineLength:100},encoding:"utf-8"};constructor(e){this.config=e;}withContent(e){return this.content=typeof e=="string"?e:e(),this}withTemplate(e,t){if(!this.config.templateEngine)throw new ie(`Template engine is required for template rendering. Template: '${e}'`,e,t);this.options.template=e;try{this.content=this.config.templateEngine.render(e,{...t,imports:this.imports,exports:this.exports,filename:this.config.filename});}catch{throw new ie(`Failed to render template '${e}'`,e,t,{availableTemplates:this.config.templateEngine.getAvailableTemplates?.()||[]})}return this}appendContent(e){return this.content+=e,this}prependContent(e){return this.content=e+this.content,this}withImports(e){this.imports.clear();for(let[t,r]of e)this.imports.set(t,r);return this}addImport(e,t){return this.imports.set(e,t),this}addImports(e,t){for(let r of e)this.imports.set(r,t);return this}withExports(e){this.exports.clear();for(let t of e)this.exports.add(t);return this}addExport(e){return this.exports.add(e),this}addExports(e){for(let t of e)this.exports.add(t);return this}withMetadata(e,t){return this.metadata.set(e,t),this}withOptions(e){return this.options={...this.options,...e},this}onBeforeSave(e){return this.beforeSaveHooks.push(e),this}onAfterSave(e){return this.afterSaveHooks.push(e),this}onError(e){return this.errorHooks.push(e),this}build(){let e=this.buildFinalContent();return {filename:this.config.filename,content:e,imports:new Map(this.imports),exports:new Set(this.exports),metadata:Object.fromEntries(this.metadata),templateName:this.options.template}}async save(){let e=this.build();try{for(let i of this.beforeSaveHooks)await i(e);this.options.validation!=="none"&&await this.validateContent(e.content);let t=await this.config.fileManager.writeFile(this.config.filename,e.content,{encoding:this.options.encoding}),r={size:t.size,generationTime:0,writeTime:t.writeTime};for(let i of this.afterSaveHooks)await i(t.path,r);return this.config.logger.debug(`Saved ${this.config.filename} successfully`),t.path}catch(t){for(let r of this.errorHooks)try{await r(t instanceof Error?t:new Error(String(t)),e);}catch(i){this.config.logger.warn(`Error hook failed: ${i instanceof Error?i.message:String(i)}`);}throw t}}buildFinalContent(){let e=[];this.imports.size>0&&this.options.importStrategy!=="none"&&(e.push(this.generateImportStatements()),e.push("")),this.content&&e.push(this.content),this.exports.size>0&&!this.content.includes("export")&&(e.push(""),e.push(this.generateExportStatements()));let t=e.join(`
|
|
@@ -38,10 +38,10 @@ ${i}`:r}filterAndSortSchemas(e){return this.collectedValueSets=this.collectValue
|
|
|
38
38
|
`)||"",i=[];return this.options.includeDocuments&&(i.push("/**"),i.push(` * ${e.identifier.name} value set`),e.description&&i.push(` * ${e.description}`),e.valueset?.url&&i.push(` * @see ${e.valueset.url}`),e.identifier.package&&i.push(` * @package ${e.identifier.package}`),i.push(" * @generated This file is auto-generated. Do not edit manually."),i.push(" */"),i.push("")),i.push(`export const ${t}Values = [`),r&&i.push(r),i.push("] as const;"),i.push(""),i.push(`export type ${t} = typeof ${t}Values[number];`),this.tsOptions.includeValueSetHelpers&&(i.push(""),i.push(`export const isValid${t} = (value: string): value is ${t} =>`),i.push(` ${t}Values.includes(value as ${t});`)),i.join(`
|
|
39
39
|
`)}async generateValueSetFiles(){if(!(!this.tsOptions.generateValueSets||this.collectedValueSets.size===0)){for(let[e,t]of this.collectedValueSets){let r=this.generateValueSetFile(t),i=`valuesets/${e}.ts`;await this.fileManager.writeFile(i,r),this.logger.info(`Generated value set: ${i}`);}await this.generateValueSetIndexFile();}}async generateValueSetIndexFile(){let e=[];this.tsOptions.includeDocuments&&(e.push("/**"),e.push(" * FHIR Value Sets"),e.push(" * This file re-exports all generated value sets."),e.push(" * "),e.push(" * @generated This file is auto-generated. Do not edit manually."),e.push(" */"),e.push(""));let t=Array.from(this.collectedValueSets.keys()).sort();for(let i of t)e.push(`export * from './${i}.js';`);let r=e.join(`
|
|
40
40
|
`);await this.fileManager.writeFile("valuesets/index.ts",r),this.logger.info(`Generated valuesets/index.ts with ${this.collectedValueSets.size} value sets`);}async generateMainIndexFile(){if(!this.options.generateIndex)return;let e=[];this.tsOptions.includeDocuments&&(e.push("/**"),e.push(" * FHIR R4 TypeScript Types"),e.push(" * Generated from FHIR StructureDefinitions"),e.push(" * "),e.push(" * @generated This file is auto-generated. Do not edit manually."),e.push(" */"),e.push("")),e.push('export * from "./utilities";'),this.tsOptions.generateValueSets&&this.collectedValueSets.size>0&&(e.push(""),e.push("// Value Sets"),e.push('export * from "./valuesets/index";'));let t=e.join(`
|
|
41
|
-
`);await this.fileManager.writeFile("index.ts",t),this.logger.info(`Generated index.ts with type exports${this.tsOptions.generateValueSets&&this.collectedValueSets.size>0?" and value sets":""}`);}};var gi={boolean:"boolean",instant:"string",time:"string",date:"string",dateTime:"string",decimal:"number",integer:"number",unsignedInt:"number",positiveInt:"number",integer64:"number",base64Binary:"string",uri:"string",url:"string",canonical:"string",oid:"string",uuid:"string",string:"string",code:"string",markdown:"string",id:"string",xhtml:"string"},gr=n=>{let e=gi[n];if(e===void 0)throw new Error(`Unknown primitive type ${n}`);return e},di=n=>re(n),fr=n=>n.kind==="profile"?`${x(n)}_profile`:G(n.name),fi=n=>`${fr(n)}.ts`,ft=(n,e=true)=>{if(!n)return;let t=ge(n,e);if(t)return ht(t)},x=n=>{if(n.kind==="nested"){let e=n.url,t=ft(e,false);if(!t)return "";let[r,i]=t.split("#"),s=xe((i??"").split(".")).join("");return ht([r,s].join(""))}return ht(n.name)},hr=new Set(["class","function","return","if","for","while","const","let","var","import","export","interface"]),K=n=>hr.has(n)?`"${n}"`:n.includes(" ")||n.includes("-")?`"${n}"`:n,ht=n=>(hr.has(n)&&(n=`${n}_`),n.replace(/[- ]/g,"_")),H=(n,e)=>e.startsWith('"')?`${n}[${e}]`:`${n}.${e}`,dr=n=>`(${n.map(e=>`"${e}"`).join(" | ")})`,Oe=class extends J{tsImportType(e,...t){this.lineSM(`import type { ${t.join(", ")} } from "${e}"`);}generateFhirPackageIndexFile(e){this.cat("index.ts",()=>{let t=e.flatMap(r=>[{identifier:r.identifier,tsPackageName:fr(r.identifier),resourceName:x(r.identifier),nestedTypes:P(r)&&r.nested?r.nested.map(i=>x(i.identifier)):[],helpers:P(r)?[`is${x(r.identifier)}`]:[]}]).sort((r,i)=>r.resourceName.localeCompare(i.resourceName));t=Array.from(new Map(t.map(r=>[r.resourceName.toLowerCase(),r])).values()).sort((r,i)=>r.resourceName.localeCompare(i.resourceName));for(let r of t)this.debugComment(r.identifier),this.lineSM(`export type { ${[r.resourceName,...r.nestedTypes].join(", ")} } from "./${r.tsPackageName}"`),r.helpers.length>0&&this.lineSM(`export { ${r.helpers.join(", ")} } from "./${r.tsPackageName}"`);});}generateDependenciesImports(e){if(e.dependencies){let t=[],r=[];for(let i of e.dependencies)["complex-type","resource","logical"].includes(i.kind)?t.push({tsPackage:`../${re(i.package)}/${G(i.name)}`,name:E(i.name),dep:i}):N(i)?t.push({tsPackage:`../${re(i.package)}/${G(ft(i.url)??"")}`,name:x(i),dep:i}):r.push(i);t.sort((i,s)=>i.name.localeCompare(s.name));for(let i of t)this.debugComment(i.dep),this.tsImportType(i.tsPackage,i.name);for(let i of r)this.debugComment("skip:",i);this.line();}}generateComplexTypeReexports(e){let t=e.dependencies?.filter(r=>["complex-type"].includes(r.kind)).map(r=>({tsPackage:`../${re(r.package)}/${G(r.name)}`,name:E(r.name)}));if(t&&t.length>0){for(let r of t)this.lineSM(`export type { ${r.name} } from "${r.tsPackage}"`);this.line();}}addFieldExtension(e,t){if(t.type.kind==="primitive-type"){let r=K(`_${e}`);this.lineSM(`${r}?: Element`);}}generateType(e,t){let r;t.identifier.name==="Reference"?r="Reference<T extends string = string>":(t.identifier.kind,r=x(t.identifier));let i;if(t.base&&(i=`extends ${ft(t.base.url)}`),this.debugComment(t.identifier),!t.fields&&!i&&!P(t)){this.lineSM(`export type ${r} = object`);return}this.curlyBlock(["export","interface",r,i],()=>{if(P(t)){let o=[t.identifier];o.push(...e.resourceChildren(t.identifier)),this.lineSM(`resourceType: ${o.sort((a,c)=>a.name.localeCompare(c.name)).map(a=>`"${a.name}"`).join(" | ")}`),this.line();}if(!t.fields)return;let s=Object.entries(t.fields).sort((o,a)=>o[0].localeCompare(a[0]));for(let[o,a]of s){if(q(a))continue;this.debugComment(o,":",a);let c=K(o),l;a.enum?l=dr(a.enum):t.identifier.name==="Reference"&&c==="reference"?l="`${T}/${string}`":a.reference&&a.reference.length>0?l=`Reference<${a.reference.map(g=>`"${g.name}"`).join(" | ")}>`:Je(a.type)?l=gr(a.type.name):N(a.type)?l=x(a.type):l=a.type.name;let p=a.required?"":"?",d=a.array?"[]":"";this.lineSM(`${c}${p}: ${l}${d}`),["resource","complex-type"].includes(t.identifier.kind)&&this.addFieldExtension(o,a);}});}generateResourceTypePredicate(e){if(!P(e))return;let t=x(e.identifier);this.curlyBlock(["export","const",`is${t}`,"=",`(resource: unknown): resource is ${t}`,"=>"],()=>{this.lineSM(`return resource !== null && typeof resource === "object" && (resource as {resourceType: string}).resourceType === "${e.identifier.name}"`);});}generateNestedTypes(e,t){if(t.nested)for(let r of t.nested)this.generateType(e,r),this.line();}generateProfileType(e,t){this.debugComment("flatProfile",t);let r=x(t.identifier);this.debugComment("identifier",t.identifier),this.debugComment("base",t.base),this.curlyBlock(["export","interface",r],()=>{this.lineSM(`__profileUrl: "${t.identifier.url}"`),this.line();for(let[i,s]of Object.entries(t.fields??{})){if(q(s))continue;this.debugComment(i,s);let o=K(i),a;if(s.enum)a=dr(s.enum);else if(s.reference&&s.reference.length>0){let c=e.findLastSpecialization(t);if(!z(c))throw new Error(`Invalid specialization for ${t.identifier}`);let l=c.fields?.[i];if(l===void 0||q(l)||l.reference===void 0)throw new Error(`Invalid field declaration for ${i}`);let p=l.reference.map(m=>m.name),d=s.reference.map(m=>{let g=e.findLastSpecializationByIdentifier(m);return g.name!==m.name?`"${g.name}" /*${m.name}*/`:`'${m.name}'`}).join(" | ");p.length===1&&p[0]==="Resource"&&d!=='"Resource"'?a=`Reference<"Resource" /* ${d} */ >`:a=`Reference<${d}>`;}else if(N(s.type))a=x(s.type);else if(Je(s.type))a=gr(s.type.name);else {if(s.type===void 0)throw new Error(`Undefined type for '${i}' field at ${Wt(t)}`);a=s.type.name;}this.lineSM(`${o}${s.required?"":"?"}: ${a}${s.array?"[]":""}`);}}),this.line();}generateAttachProfile(e){let t=x(e.base),r=x(e.identifier),i=Object.entries(e.fields||{}).filter(([s,o])=>o&&X(o)&&o.type!==void 0).map(([s])=>K(s));this.curlyBlock([`export const attach_${r}_to_${t} =`,`(resource: ${t}, profile: ${r}): ${t}`,"=>"],()=>{this.curlyBlock(["return"],()=>{this.line("...resource,"),this.curlyBlock(["meta:"],()=>{this.line(`profile: ['${e.identifier.url}']`);},[","]),i.forEach(s=>{this.line(`${s}: ${H("profile",s)},`);});});}),this.line();}generateExtractProfile(e,t){let r=x(t.base),i=x(t.identifier),s=Object.entries(t.fields||{}).filter(([c,l])=>X(l)&&l.type!==void 0).map(([c])=>c),o=e.findLastSpecialization(t);if(!z(o))throw new Error(`Specialization not found for ${t.identifier.url}`);let a={};this.curlyBlock([`export const extract_${i}_from_${r} =`,`(resource: ${r}): ${i}`,"=>"],()=>{s.forEach(c=>{let l=K(c),p=t.fields?.[c],d=o.fields?.[c];if(!X(p)||!X(d))return;p.required&&!d.required&&this.curlyBlock([`if (${H("resource",l)} === undefined)`],()=>this.lineSM(`throw new Error("'${l}' is required for ${t.identifier.url}")`));let m=p?.reference?.map(h=>h.name),g=d?.reference?.map(h=>h.name);if(m&&g&&m.length!==g.length){let h=`reference_is_valid_${l}`;this.curlyBlock(["const",h,"=","(ref?: Reference)","=>"],()=>{this.line("return !ref"),this.indentBlock(()=>{g.forEach(f=>{this.line(`|| ref.reference?.startsWith('${f}/')`);}),this.line(";");});});let u=p?.required?"":`!${H("resource",l)} || `;p.array?u+=`${H("resource",l)}.every( (ref) => ${h}(ref) )`:u+=`!${h}(${H("resource",l)})`,this.curlyBlock(["if (",u,")"],()=>{this.lineSM(`throw new Error("'${c}' has different references in profile and specialization")`);}),this.line(),a[c]=true;}}),this.curlyBlock(["return"],()=>{this.line(`__profileUrl: '${t.identifier.url}',`),s.forEach(c=>{let l=K(c);a[c]?this.line(`${l}:`,`${H("resource",l)} as ${i}['${l}'],`):this.line(`${l}:`,`${H("resource",l)},`);});});});}generateResourceModule(e,t){this.cat(`${fi(t.identifier)}`,()=>{if(this.generateDisclaimer(),["complex-type","resource","logical"].includes(t.identifier.kind))this.generateDependenciesImports(t),this.generateComplexTypeReexports(t),this.generateNestedTypes(e,t),this.comment("CanonicalURL:",t.identifier.url),this.generateType(e,t),this.generateResourceTypePredicate(t);else if(de(t)){let r=e.flatProfile(t);this.generateDependenciesImports(r),this.comment("CanonicalURL:",t.identifier.url),this.generateProfileType(e,r),this.generateAttachProfile(r),this.generateExtractProfile(e,r);}else throw new Error(`Profile generation not implemented for kind: ${t.identifier.kind}`)});}async generate(e){let t=[...e.collectComplexTypes(),...e.collectResources(),...this.opts.generateProfile?e.collectProfiles().filter(i=>e.isWithMetaField(i)):[]],r=at(t);this.cd("/",()=>{for(let[i,s]of Object.entries(r)){let o=di(i);this.cd(o,()=>{for(let a of s)this.generateResourceModule(e,a);this.generateFhirPackageIndexFile(s);});}});}};var mr=n=>{let e=()=>n.writtenFiles().map(t=>({path:R.normalize(R.join(n.opts.outputDir,t)),filename:t.replace(/^.*[\\/]/,""),content:"",exports:[],size:0,timestamp:new Date}));return {generate:async({index:t})=>(await n.generate(t),e()),setOutputDir:t=>n.opts.outputDir=t,build:async t=>e()}},yr=n=>{let e=n.replace(/[^a-zA-Z0-9\-_.@#()]/g,"");return e.length===0?"unknown":e},yi=async(n,e)=>{e.info("Cleaning outputs...");try{e.info(`Clean ${n.outputDir}`),F.rmSync(n.outputDir,{recursive:!0,force:!0}),n.typeSchemaOutputDir&&(e.info(`Clean ${n.typeSchemaOutputDir}`),F.rmSync(n.typeSchemaOutputDir,{recursive:!0,force:!0})),n.exportTypeTree&&(e.info(`Clean ${n.exportTypeTree}`),F.rmSync(n.exportTypeTree,{recursive:!0,force:!0}));}catch(t){e.warn(`Error cleaning output directory: ${t instanceof Error?t.message:String(t)}`);}},Si=async(n,e,t)=>{await L.mkdir(e,{recursive:true}),t.info(`Writing TypeSchema files to ${e}/...`);let r={};for(let i of n){let s={name:i.identifier.package,version:i.identifier.version},o=yr(S(s)),a=yr(`${i.identifier.name}(${ge(i.identifier.url)})`),c=JSON.stringify(i,null,2),l=R.join(e,o,a);r[l]||(r[l]=[]),r[l]?.some(p=>p===c)||r[l].push(c);}for(let[i,s]of Object.entries(r))await Promise.all(s.map(async(o,a)=>{let c;a===0?c=`${i}.typeschema.json`:c=`${i}-${a}.typeschema.json`,await L.mkdir(R.dirname(c),{recursive:true}),await L.writeFile(c,o);}));},bi=async(n,e,t)=>{t.info(`Writing TypeSchema files to: ${e}`),await L.mkdir(R.dirname(e),{recursive:true}),t.info(`Writing TypeSchemas to one file ${e}...`);for(let r of n){let i=JSON.stringify(r,null,2);await L.appendFile(e,`${i}
|
|
42
|
-
`);}},vi=async(n,e,t)=>{if(e.typeSchemaOutputDir)try{R.extname(e.typeSchemaOutputDir)===".ndjson"?await bi(n,e.typeSchemaOutputDir,t):await Si(n,e.typeSchemaOutputDir,t),t.info("Writing TypeSchema - DONE");}catch(r){if(t.error("Failed to write TypeSchema output",r instanceof Error?r:new Error(String(r))),e.throwException)throw r}},oe=class{schemas=[];options;generators=new Map;cache;pendingOperations=[];typeSchemaGenerator;logger;packages=[];progressCallback;typeSchemaConfig;constructor(e={}){this.options={outputDir:e.outputDir||"./generated",verbose:e.verbose??false,overwrite:e.overwrite??true,cache:e.cache??true,cleanOutput:e.cleanOutput??true,typeSchemaConfig:e.typeSchemaConfig,manager:e.manager||null,throwException:e.throwException||false,typeSchemaOutputDir:e.typeSchemaOutputDir,exportTypeTree:e.exportTypeTree,treeShake:e.treeShake},this.typeSchemaConfig=e.typeSchemaConfig,this.logger=e.logger||I({verbose:this.options.verbose,prefix:"API"}),this.options.cache&&(this.cache=new O(this.typeSchemaConfig));}fromPackage(e,t){let r=qe({name:e,version:t||"latest"});return this.packages.push(r),this}fromPackageRef(e){return this.packages.push(e),this}fromFiles(...e){this.logger.debug(`Loading from ${e.length} TypeSchema files`);let t=this.loadFromFiles(e);return this.pendingOperations.push(t),this}fromSchemas(e){return this.logger.debug(`Adding ${e.length} TypeSchemas to generation`),this.schemas=[...this.schemas,...e],this}typescriptDepricated(e={}){let t=`${this.options.outputDir}/types`,r=new se({outputDir:t,moduleFormat:e.moduleFormat||"esm",generateIndex:e.generateIndex??true,includeDocuments:e.includeDocuments??true,namingConvention:e.namingConvention||"PascalCase",includeExtensions:e.includeExtensions??false,includeProfiles:e.includeProfiles??false,generateValueSets:e.generateValueSets??false,includeValueSetHelpers:e.includeValueSetHelpers??false,valueSetStrengths:e.valueSetStrengths??["required"],logger:this.logger.child("TS"),valueSetMode:e.valueSetMode??"required-only",valueSetDirectory:e.valueSetDirectory??"valuesets",verbose:this.options.verbose,validate:true,overwrite:this.options.overwrite});return this.generators.set("typescript",r),this.logger.debug(`Configured TypeScript generator (${e.moduleFormat||"esm"})`),this}typescript(e){let t={outputDir:R.join(this.options.outputDir,"/types"),tabSize:4,withDebugComment:false,commentLinePrefix:"//",generateProfile:true,exportTypeTree:this.options.exportTypeTree},r={logger:this.logger,...t,...e},i=mr(new Oe(r));return this.generators.set("typescript",i),this.logger.debug(`Configured TypeScript generator (${JSON.stringify(r,void 0,2)})`),this}csharp(e,t){let r=mr(new we({outputDir:R.join(this.options.outputDir,"/types"),staticSourceDir:t??void 0,targetNamespace:e,logger:new _({prefix:"C#",timestamp:true,verbose:true,suppressLoggingLevel:[]})}));return this.generators.set("C#",r),this.logger.debug("Configured C# generator"),this}onProgress(e){return this.progressCallback=e,this}outputTo(e){this.logger.debug(`Setting output directory: ${e}`),this.options.outputDir=e;for(let t of this.generators.values())t.setOutputDir&&t.setOutputDir(e);return this}verbose(e=true){return this.options.verbose=e,this.logger?.configure({verbose:e}),this}throwException(e=true){return this.options.throwException=e,this}cleanOutput(e=true){return this.options.cleanOutput=e,this}writeTypeTree(e){return this.options.exportTypeTree=e,this}treeShake(e){return this.options.treeShake=e,this}writeTypeSchemas(e){return this.options.typeSchemaOutputDir=e,this}async generate(){let e=performance.now(),t={success:false,outputDir:this.options.outputDir,filesGenerated:[],errors:[],warnings:[],duration:0};this.logger.debug(`Starting generation with ${this.generators.size} generators`);try{this.options.cleanOutput&&yi(this.options,this.logger),this.logger.info("Initialize Canonical Manager");let r=CanonicalManager({packages:this.packages,workingDir:".codegen-cache/canonical-manager-cache"}),i=await r.init(),s=Object.values(i),o=await ee(r,{logger:this.logger,focusedPackages:s}),a=await _t(o,this.logger);await vi(a,this.options,this.logger);let c=ct(a,this.logger);this.options.treeShake&&(c=rr(c,this.options.treeShake,this.logger)),this.options.exportTypeTree&&await c.exportTree(this.options.exportTypeTree),this.logger.debug(`Executing ${this.generators.size} generators`),await this.executeGenerators(t,{schemas:a,index:c}),this.logger.info("Generation completed successfully"),t.success=t.errors.length===0,this.logger.debug(`Generation completed: ${t.filesGenerated.length} files`);}catch(r){if(this.logger.error("Code generation failed",r instanceof Error?r:new Error(String(r))),t.errors.push(r instanceof Error?r.message:String(r)),this.options.throwException)throw r}return {...t,success:t.errors.length===0,duration:performance.now()-e}}async build(){let e={};for(let[t,r]of this.generators.entries())r.build&&(e[t]=await r.build(this.schemas));return e}reset(){return this.schemas=[],this.generators.clear(),this.progressCallback=void 0,this}getSchemas(){return [...this.schemas]}getGenerators(){return Array.from(this.generators.keys())}async loadFromFiles(e){this.typeSchemaGenerator||(this.typeSchemaGenerator=new D({verbose:this.options.verbose,logger:this.logger.child("Schema"),treeshake:this.typeSchemaConfig?.treeshake},this.typeSchemaConfig));let r=await new W({format:"auto"}).parseFromFiles(e);this.schemas=[...this.schemas,...r],this.cache&&this.cache.setMany(r);}async executeGenerators(e,t){for(let[r,i]of this.generators.entries()){this.logger.info(`Generating ${r}...`);try{let s=await i.generate(t);e.filesGenerated.push(...s.map(o=>o.path||o.filename)),this.logger.info(`Generating ${r} finished successfully`);}catch(s){if(e.errors.push(`${r} generator failed: ${s instanceof Error?s.message:String(s)}`),this.options.throwException)throw s}}}};var mt={outputDir:"./generated",verbose:false,overwrite:true,validate:true,cache:true,cleanOutput:true,typescript:{moduleFormat:"esm",generateIndex:true,includeDocuments:false,namingConvention:"PascalCase",strictMode:true,includeProfiles:true,includeExtensions:false,includeCodeSystems:false,includeOperations:false,generateValueSets:false,valueSetDirectory:"valuesets",valueSetMode:"required-only",valueSetStrengths:["required"],includeValueSetHelpers:false,fhirVersion:"R4",resourceTypes:[],maxDepth:10,profileOptions:{generateKind:"interface",includeConstraints:true,includeDocumentation:true,strictMode:false,subfolder:"profiles"},generateBuilders:false,builderOptions:{includeValidation:true,includeFactoryMethods:true,includeInterfaces:true,generateNestedBuilders:true,includeHelperMethods:true,supportPartialBuild:true,includeJSDoc:true,generateFactories:true,includeTypeGuards:true,handleChoiceTypes:true,generateArrayHelpers:true},validatorOptions:{includeCardinality:true,includeTypes:true,includeConstraints:true,includeInvariants:false,validateRequired:true,allowAdditional:false,strictValidation:false,collectMetrics:false,generateAssertions:true,generatePartialValidators:true,optimizePerformance:true,includeJSDoc:true,generateCompositeValidators:true},guardOptions:{includeRuntimeValidation:true,includeErrorMessages:true,treeShakeable:true,targetTSVersion:"5.0",strictGuards:false,includeNullChecks:true,verbose:false}},typeSchema:{enablePersistence:true,cacheDir:".typeschema-cache",maxAge:1440*60*1e3,validateCached:true,forceRegenerate:false,shareCache:true,cacheKeyPrefix:"",treeshake:[],singleFile:false,profiles:{autoDetect:true}},packages:[],files:[],$schema:""},Be=["atomic-codegen.config.ts","atomic-codegen.config","atomic-codegen.config.json",".atomic-codegenrc","atomic-codegen.json",".atomic-codegen.json","codegen.config.json","codegen.json"],yt=class{validate(e){let t={valid:true,errors:[],warnings:[]};if(!e||typeof e!="object")return t.valid=false,t.errors.push({path:"root",message:"Configuration must be an object",value:e}),t;let r=e;r.outputDir!==void 0&&typeof r.outputDir!="string"&&t.errors.push({path:"outputDir",message:"outputDir must be a string",value:r.outputDir});let i=["verbose","overwrite","validate","cache"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.errors.push({path:s,message:`${s} must be a boolean`,value:r[s]});if(r.typescript!==void 0){let s=this.validateTypeScriptConfig(r.typescript);t.errors.push(...s);}if(r.typeSchema!==void 0){let s=this.validateTypeSchemaConfig(r.typeSchema);t.errors.push(...s);}return r.packages!==void 0&&(Array.isArray(r.packages)?r.packages.forEach((s,o)=>{typeof s!="string"&&t.errors.push({path:`packages[${o}]`,message:"package name must be a string",value:s});}):t.errors.push({path:"packages",message:"packages must be an array",value:r.packages})),r.files!==void 0&&(Array.isArray(r.files)?r.files.forEach((s,o)=>{typeof s!="string"&&t.errors.push({path:`files[${o}]`,message:"file path must be a string",value:s});}):t.errors.push({path:"files",message:"files must be an array",value:r.files})),t.valid=t.errors.length===0,t.valid&&(t.config=r),t}validateTypeScriptConfig(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript",message:"typescript config must be an object",value:e}),t;let r=e;r.moduleFormat!==void 0&&(["esm","cjs"].includes(r.moduleFormat)||t.push({path:"typescript.moduleFormat",message:'moduleFormat must be "esm" or "cjs"',value:r.moduleFormat})),r.namingConvention!==void 0&&(["PascalCase","camelCase"].includes(r.namingConvention)||t.push({path:"typescript.namingConvention",message:'namingConvention must be "PascalCase" or "camelCase"',value:r.namingConvention}));let i=["generateIndex","includeDocuments","strictMode","includeProfiles","includeExtensions","includeCodeSystems","includeOperations","generateValueSets","includeValueSetHelpers"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typescript.${s}`,message:`${s} must be a boolean`,value:r[s]});if(r.validatorOptions!==void 0){let s=this.validateValidatorOptions(r.validatorOptions);t.push(...s);}if(r.guardOptions!==void 0){let s=this.validateGuardOptions(r.guardOptions);t.push(...s);}if(r.profileOptions!==void 0){let s=this.validateProfileOptions(r.profileOptions);t.push(...s);}return t}validateValidatorOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.validatorOptions",message:"validatorOptions must be an object",value:e}),t;let r=e,i=["includeCardinality","includeTypes","includeConstraints","includeInvariants","validateRequired","allowAdditional","strictValidation","collectMetrics","generateAssertions","generatePartialValidators","optimizePerformance","includeJSDoc","generateCompositeValidators"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typescript.validatorOptions.${s}`,message:`${s} must be a boolean`,value:r[s]});return t}validateGuardOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.guardOptions",message:"guardOptions must be an object",value:e}),t;let r=e;r.targetTSVersion!==void 0&&(["3.8","4.0","4.5","5.0"].includes(r.targetTSVersion)||t.push({path:"typescript.guardOptions.targetTSVersion",message:'targetTSVersion must be one of: "3.8", "4.0", "4.5", "5.0"',value:r.targetTSVersion}));let i=["includeRuntimeValidation","includeErrorMessages","treeShakeable","strictGuards","includeNullChecks","verbose"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typescript.guardOptions.${s}`,message:`${s} must be a boolean`,value:r[s]});return t}validateProfileOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.profileOptions",message:"profileOptions must be an object",value:e}),t;let r=e;r.generateKind!==void 0&&(["interface","type","both"].includes(r.generateKind)||t.push({path:"typescript.profileOptions.generateKind",message:'generateKind must be "interface", "type", or "both"',value:r.generateKind})),r.subfolder!==void 0&&typeof r.subfolder!="string"&&t.push({path:"typescript.profileOptions.subfolder",message:"subfolder must be a string",value:r.subfolder});let i=["includeConstraints","includeDocumentation","strictMode"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typescript.profileOptions.${s}`,message:`${s} must be a boolean`,value:r[s]});return t}validateTypeSchemaConfig(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typeSchema",message:"typeSchema config must be an object",value:e}),t;let r=e,i=["enablePersistence","validateCached","forceRegenerate","shareCache"];for(let o of i)r[o]!==void 0&&typeof r[o]!="boolean"&&t.push({path:`typeSchema.${o}`,message:`${o} must be a boolean`,value:r[o]});let s=["cacheDir","cacheKeyPrefix"];for(let o of s)r[o]!==void 0&&typeof r[o]!="string"&&t.push({path:`typeSchema.${o}`,message:`${o} must be a string`,value:r[o]});if(r.maxAge!==void 0&&(typeof r.maxAge!="number"||r.maxAge<=0)&&t.push({path:"typeSchema.maxAge",message:"maxAge must be a positive number",value:r.maxAge}),r.profiles!==void 0)if(typeof r.profiles!="object"||r.profiles===null)t.push({path:"typeSchema.profiles",message:"profiles must be an object",value:r.profiles});else {let o=r.profiles;o.autoDetect!==void 0&&typeof o.autoDetect!="boolean"&&t.push({path:"typeSchema.profiles.autoDetect",message:"autoDetect must be a boolean",value:o.autoDetect});}return t}},St=class{validator=new yt;async autoload(e=process.cwd()){let t=await this.findConfigFile(e);return t?this.loadFromFile(t):{...mt}}async loadFromFile(e){try{let t;if(e.endsWith(".ts")||e.endsWith("")){let s=await import(resolve(e));t=s.default||s;}else {let i=await readFile(e,"utf-8");t=JSON.parse(i);}let r=this.validator.validate(t);if(!r.valid){let i=r.errors.map(s=>`${s.path}: ${s.message}`).join(`
|
|
41
|
+
`);await this.fileManager.writeFile("index.ts",t),this.logger.info(`Generated index.ts with type exports${this.tsOptions.generateValueSets&&this.collectedValueSets.size>0?" and value sets":""}`);}};var gi={boolean:"boolean",instant:"string",time:"string",date:"string",dateTime:"string",decimal:"number",integer:"number",unsignedInt:"number",positiveInt:"number",integer64:"number",base64Binary:"string",uri:"string",url:"string",canonical:"string",oid:"string",uuid:"string",string:"string",code:"string",markdown:"string",id:"string",xhtml:"string"},gr=n=>{let e=gi[n];if(e===void 0)throw new Error(`Unknown primitive type ${n}`);return e},di=n=>re(n),fr=n=>n.kind==="profile"?`${x(n)}_profile`:G(n.name),fi=n=>`${fr(n)}.ts`,ft=(n,e=true)=>{if(!n)return;let t=ge(n,e);if(t)return ht(t)},x=n=>{if(n.kind==="nested"){let e=n.url,t=ft(e,false);if(!t)return "";let[r,i]=t.split("#"),s=xe((i??"").split(".")).join("");return ht([r,s].join(""))}return ht(n.name)},hr=new Set(["class","function","return","if","for","while","const","let","var","import","export","interface"]),K=n=>hr.has(n)?`"${n}"`:n.includes(" ")||n.includes("-")?`"${n}"`:n,ht=n=>(hr.has(n)&&(n=`${n}_`),n.replace(/[- ]/g,"_")),H=(n,e)=>e.startsWith('"')?`${n}[${e}]`:`${n}.${e}`,dr=n=>`(${n.map(e=>`"${e}"`).join(" | ")})`,Oe=class extends J{tsImportType(e,...t){this.lineSM(`import type { ${t.join(", ")} } from "${e}"`);}generateFhirPackageIndexFile(e){this.cat("index.ts",()=>{let t=e.flatMap(r=>[{identifier:r.identifier,tsPackageName:fr(r.identifier),resourceName:x(r.identifier),nestedTypes:P(r)&&r.nested?r.nested.map(i=>x(i.identifier)):[],helpers:P(r)?[`is${x(r.identifier)}`]:[]}]).sort((r,i)=>r.resourceName.localeCompare(i.resourceName));t=Array.from(new Map(t.map(r=>[r.resourceName.toLowerCase(),r])).values()).sort((r,i)=>r.resourceName.localeCompare(i.resourceName));for(let r of t)this.debugComment(r.identifier),this.lineSM(`export type { ${[r.resourceName,...r.nestedTypes].join(", ")} } from "./${r.tsPackageName}"`),r.helpers.length>0&&this.lineSM(`export { ${r.helpers.join(", ")} } from "./${r.tsPackageName}"`);});}generateDependenciesImports(e){if(e.dependencies){let t=[],r=[];for(let i of e.dependencies)["complex-type","resource","logical"].includes(i.kind)?t.push({tsPackage:`../${re(i.package)}/${G(i.name)}`,name:E(i.name),dep:i}):N(i)?t.push({tsPackage:`../${re(i.package)}/${G(ft(i.url)??"")}`,name:x(i),dep:i}):r.push(i);t.sort((i,s)=>i.name.localeCompare(s.name));for(let i of t)this.debugComment(i.dep),this.tsImportType(i.tsPackage,i.name);for(let i of r)this.debugComment("skip:",i);this.line();}}generateComplexTypeReexports(e){let t=e.dependencies?.filter(r=>["complex-type"].includes(r.kind)).map(r=>({tsPackage:`../${re(r.package)}/${G(r.name)}`,name:E(r.name)}));if(t&&t.length>0){for(let r of t)this.lineSM(`export type { ${r.name} } from "${r.tsPackage}"`);this.line();}}addFieldExtension(e,t){if(t.type.kind==="primitive-type"){let r=K(`_${e}`);this.lineSM(`${r}?: Element`);}}generateType(e,t){let r;t.identifier.name==="Reference"?r="Reference<T extends string = string>":(t.identifier.kind,r=x(t.identifier));let i;if(t.base&&(i=`extends ${ft(t.base.url)}`),this.debugComment(t.identifier),!t.fields&&!i&&!P(t)){this.lineSM(`export type ${r} = object`);return}this.curlyBlock(["export","interface",r,i],()=>{if(P(t)){let o=[t.identifier];o.push(...e.resourceChildren(t.identifier));let a=this.opts.openResourceTypeSet&&o.length>1?" | string":"";this.lineSM(`resourceType: ${o.sort((c,l)=>c.name.localeCompare(l.name)).map(c=>`"${c.name}"`).join(" | ")}${a}`),this.line();}if(!t.fields)return;let s=Object.entries(t.fields).sort((o,a)=>o[0].localeCompare(a[0]));for(let[o,a]of s){if(q(a))continue;this.debugComment(o,":",a);let c=K(o),l;a.enum?l=dr(a.enum):t.identifier.name==="Reference"&&c==="reference"?l="`${T}/${string}`":a.reference&&a.reference.length>0?l=`Reference<${a.reference.map(g=>`"${g.name}"`).join(" | ")}>`:Je(a.type)?l=gr(a.type.name):N(a.type)?l=x(a.type):l=a.type.name;let p=a.required?"":"?",d=a.array?"[]":"";this.lineSM(`${c}${p}: ${l}${d}`),["resource","complex-type"].includes(t.identifier.kind)&&this.addFieldExtension(o,a);}});}generateResourceTypePredicate(e){if(!P(e))return;let t=x(e.identifier);this.curlyBlock(["export","const",`is${t}`,"=",`(resource: unknown): resource is ${t}`,"=>"],()=>{this.lineSM(`return resource !== null && typeof resource === "object" && (resource as {resourceType: string}).resourceType === "${e.identifier.name}"`);});}generateNestedTypes(e,t){if(t.nested)for(let r of t.nested)this.generateType(e,r),this.line();}generateProfileType(e,t){this.debugComment("flatProfile",t);let r=x(t.identifier);this.debugComment("identifier",t.identifier),this.debugComment("base",t.base),this.curlyBlock(["export","interface",r],()=>{this.lineSM(`__profileUrl: "${t.identifier.url}"`),this.line();for(let[i,s]of Object.entries(t.fields??{})){if(q(s))continue;this.debugComment(i,s);let o=K(i),a;if(s.enum)a=dr(s.enum);else if(s.reference&&s.reference.length>0){let c=e.findLastSpecialization(t);if(!z(c))throw new Error(`Invalid specialization for ${t.identifier}`);let l=c.fields?.[i];if(l===void 0||q(l)||l.reference===void 0)throw new Error(`Invalid field declaration for ${i}`);let p=l.reference.map(m=>m.name),d=s.reference.map(m=>{let g=e.findLastSpecializationByIdentifier(m);return g.name!==m.name?`"${g.name}" /*${m.name}*/`:`'${m.name}'`}).join(" | ");p.length===1&&p[0]==="Resource"&&d!=='"Resource"'?a=`Reference<"Resource" /* ${d} */ >`:a=`Reference<${d}>`;}else if(N(s.type))a=x(s.type);else if(Je(s.type))a=gr(s.type.name);else {if(s.type===void 0)throw new Error(`Undefined type for '${i}' field at ${Wt(t)}`);a=s.type.name;}this.lineSM(`${o}${s.required?"":"?"}: ${a}${s.array?"[]":""}`);}}),this.line();}generateAttachProfile(e){let t=x(e.base),r=x(e.identifier),i=Object.entries(e.fields||{}).filter(([s,o])=>o&&X(o)&&o.type!==void 0).map(([s])=>K(s));this.curlyBlock([`export const attach_${r}_to_${t} =`,`(resource: ${t}, profile: ${r}): ${t}`,"=>"],()=>{this.curlyBlock(["return"],()=>{this.line("...resource,"),this.curlyBlock(["meta:"],()=>{this.line(`profile: ['${e.identifier.url}']`);},[","]),i.forEach(s=>{this.line(`${s}: ${H("profile",s)},`);});});}),this.line();}generateExtractProfile(e,t){let r=x(t.base),i=x(t.identifier),s=Object.entries(t.fields||{}).filter(([c,l])=>X(l)&&l.type!==void 0).map(([c])=>c),o=e.findLastSpecialization(t);if(!z(o))throw new Error(`Specialization not found for ${t.identifier.url}`);let a={};this.curlyBlock([`export const extract_${i}_from_${r} =`,`(resource: ${r}): ${i}`,"=>"],()=>{s.forEach(c=>{let l=K(c),p=t.fields?.[c],d=o.fields?.[c];if(!X(p)||!X(d))return;p.required&&!d.required&&this.curlyBlock([`if (${H("resource",l)} === undefined)`],()=>this.lineSM(`throw new Error("'${l}' is required for ${t.identifier.url}")`));let m=p?.reference?.map(h=>h.name),g=d?.reference?.map(h=>h.name);if(m&&g&&m.length!==g.length){let h=`reference_is_valid_${l}`;this.curlyBlock(["const",h,"=","(ref?: Reference)","=>"],()=>{this.line("return !ref"),this.indentBlock(()=>{g.forEach(f=>{this.line(`|| ref.reference?.startsWith('${f}/')`);}),this.line(";");});});let u=p?.required?"":`!${H("resource",l)} || `;p.array?u+=`${H("resource",l)}.every( (ref) => ${h}(ref) )`:u+=`!${h}(${H("resource",l)})`,this.curlyBlock(["if (",u,")"],()=>{this.lineSM(`throw new Error("'${c}' has different references in profile and specialization")`);}),this.line(),a[c]=true;}}),this.curlyBlock(["return"],()=>{this.line(`__profileUrl: '${t.identifier.url}',`),s.forEach(c=>{let l=K(c);a[c]?this.line(`${l}:`,`${H("resource",l)} as ${i}['${l}'],`):this.line(`${l}:`,`${H("resource",l)},`);});});});}generateResourceModule(e,t){this.cat(`${fi(t.identifier)}`,()=>{if(this.generateDisclaimer(),["complex-type","resource","logical"].includes(t.identifier.kind))this.generateDependenciesImports(t),this.generateComplexTypeReexports(t),this.generateNestedTypes(e,t),this.comment("CanonicalURL:",t.identifier.url),this.generateType(e,t),this.generateResourceTypePredicate(t);else if(de(t)){let r=e.flatProfile(t);this.generateDependenciesImports(r),this.comment("CanonicalURL:",t.identifier.url),this.generateProfileType(e,r),this.generateAttachProfile(r),this.generateExtractProfile(e,r);}else throw new Error(`Profile generation not implemented for kind: ${t.identifier.kind}`)});}async generate(e){let t=[...e.collectComplexTypes(),...e.collectResources(),...this.opts.generateProfile?e.collectProfiles().filter(i=>e.isWithMetaField(i)):[]],r=at(t);this.cd("/",()=>{for(let[i,s]of Object.entries(r)){let o=di(i);this.cd(o,()=>{for(let a of s)this.generateResourceModule(e,a);this.generateFhirPackageIndexFile(s);});}});}};var mr=n=>{let e=()=>n.writtenFiles().map(t=>({path:R.normalize(R.join(n.opts.outputDir,t)),filename:t.replace(/^.*[\\/]/,""),content:"",exports:[],size:0,timestamp:new Date}));return {generate:async({index:t})=>(await n.generate(t),e()),setOutputDir:t=>n.opts.outputDir=t,build:async t=>e()}},yr=n=>{let e=n.replace(/[^a-zA-Z0-9\-_.@#()]/g,"");return e.length===0?"unknown":e},mi=async(n,e)=>{e.info("Cleaning outputs...");try{e.info(`Clean ${n.outputDir}`),F.rmSync(n.outputDir,{recursive:!0,force:!0}),n.typeSchemaOutputDir&&(e.info(`Clean ${n.typeSchemaOutputDir}`),F.rmSync(n.typeSchemaOutputDir,{recursive:!0,force:!0})),n.exportTypeTree&&(e.info(`Clean ${n.exportTypeTree}`),F.rmSync(n.exportTypeTree,{recursive:!0,force:!0}));}catch(t){e.warn(`Error cleaning output directory: ${t instanceof Error?t.message:String(t)}`);}},yi=async(n,e,t)=>{await L.mkdir(e,{recursive:true}),t.info(`Writing TypeSchema files to ${e}/...`);let r={};for(let i of n){let s={name:i.identifier.package,version:i.identifier.version},o=yr(S(s)),a=yr(`${i.identifier.name}(${ge(i.identifier.url)})`),c=JSON.stringify(i,null,2),l=R.join(e,o,a);r[l]||(r[l]=[]),r[l]?.some(p=>p===c)||r[l].push(c);}for(let[i,s]of Object.entries(r))await Promise.all(s.map(async(o,a)=>{let c;a===0?c=`${i}.typeschema.json`:c=`${i}-${a}.typeschema.json`,await L.mkdir(R.dirname(c),{recursive:true}),await L.writeFile(c,o);}));},Si=async(n,e,t)=>{t.info(`Writing TypeSchema files to: ${e}`),await L.mkdir(R.dirname(e),{recursive:true}),t.info(`Writing TypeSchemas to one file ${e}...`);for(let r of n){let i=JSON.stringify(r,null,2);await L.appendFile(e,`${i}
|
|
42
|
+
`);}},bi=async(n,e,t)=>{if(e.typeSchemaOutputDir)try{R.extname(e.typeSchemaOutputDir)===".ndjson"?await Si(n,e.typeSchemaOutputDir,t):await yi(n,e.typeSchemaOutputDir,t),t.info("Writing TypeSchema - DONE");}catch(r){if(t.error("Failed to write TypeSchema output",r instanceof Error?r:new Error(String(r))),e.throwException)throw r}},oe=class{schemas=[];options;generators=new Map;cache;pendingOperations=[];typeSchemaGenerator;logger;packages=[];progressCallback;typeSchemaConfig;constructor(e={}){this.options={outputDir:e.outputDir||"./generated",verbose:e.verbose??false,overwrite:e.overwrite??true,cache:e.cache??true,cleanOutput:e.cleanOutput??true,typeSchemaConfig:e.typeSchemaConfig,manager:e.manager||null,throwException:e.throwException||false,typeSchemaOutputDir:e.typeSchemaOutputDir,exportTypeTree:e.exportTypeTree,treeShake:e.treeShake},this.typeSchemaConfig=e.typeSchemaConfig,this.logger=e.logger||I({verbose:this.options.verbose,prefix:"API"}),this.options.cache&&(this.cache=new O(this.typeSchemaConfig));}fromPackage(e,t){let r=qe({name:e,version:t||"latest"});return this.packages.push(r),this}fromPackageRef(e){return this.packages.push(e),this}fromFiles(...e){this.logger.debug(`Loading from ${e.length} TypeSchema files`);let t=this.loadFromFiles(e);return this.pendingOperations.push(t),this}fromSchemas(e){return this.logger.debug(`Adding ${e.length} TypeSchemas to generation`),this.schemas=[...this.schemas,...e],this}typescriptDepricated(e={}){let t=`${this.options.outputDir}/types`,r=new se({outputDir:t,moduleFormat:e.moduleFormat||"esm",generateIndex:e.generateIndex??true,includeDocuments:e.includeDocuments??true,namingConvention:e.namingConvention||"PascalCase",includeExtensions:e.includeExtensions??false,includeProfiles:e.includeProfiles??false,generateValueSets:e.generateValueSets??false,includeValueSetHelpers:e.includeValueSetHelpers??false,valueSetStrengths:e.valueSetStrengths??["required"],logger:this.logger.child("TS"),valueSetMode:e.valueSetMode??"required-only",valueSetDirectory:e.valueSetDirectory??"valuesets",verbose:this.options.verbose,validate:true,overwrite:this.options.overwrite});return this.generators.set("typescript",r),this.logger.debug(`Configured TypeScript generator (${e.moduleFormat||"esm"})`),this}typescript(e){let i={...{...{logger:this.logger,outputDir:R.join(this.options.outputDir,"/types"),tabSize:4,withDebugComment:false,commentLinePrefix:"//",generateProfile:true},openResourceTypeSet:false},...Object.fromEntries(Object.entries(e).filter(([o,a])=>a!==void 0))},s=mr(new Oe(i));return this.generators.set("typescript",s),this.logger.debug(`Configured TypeScript generator (${JSON.stringify(i,void 0,2)})`),this}csharp(e,t){let r=mr(new we({outputDir:R.join(this.options.outputDir,"/types"),staticSourceDir:t??void 0,targetNamespace:e,logger:new _({prefix:"C#",timestamp:true,verbose:true,suppressLoggingLevel:[]})}));return this.generators.set("C#",r),this.logger.debug("Configured C# generator"),this}onProgress(e){return this.progressCallback=e,this}outputTo(e){this.logger.debug(`Setting output directory: ${e}`),this.options.outputDir=e;for(let t of this.generators.values())t.setOutputDir&&t.setOutputDir(e);return this}verbose(e=true){return this.options.verbose=e,this.logger?.configure({verbose:e}),this}throwException(e=true){return this.options.throwException=e,this}cleanOutput(e=true){return this.options.cleanOutput=e,this}writeTypeTree(e){return this.options.exportTypeTree=e,this}treeShake(e){return this.options.treeShake=e,this}writeTypeSchemas(e){return this.options.typeSchemaOutputDir=e,this}async generate(){let e=performance.now(),t={success:false,outputDir:this.options.outputDir,filesGenerated:[],errors:[],warnings:[],duration:0};this.logger.debug(`Starting generation with ${this.generators.size} generators`);try{this.options.cleanOutput&&mi(this.options,this.logger),this.logger.info("Initialize Canonical Manager");let r=CanonicalManager({packages:this.packages,workingDir:".codegen-cache/canonical-manager-cache"}),i=await r.init(),s=Object.values(i),o=await ee(r,{logger:this.logger,focusedPackages:s}),a=await _t(o,this.logger);await bi(a,this.options,this.logger);let c=ct(a,this.logger);this.options.treeShake&&(c=rr(c,this.options.treeShake,this.logger)),this.options.exportTypeTree&&await c.exportTree(this.options.exportTypeTree),this.logger.debug(`Executing ${this.generators.size} generators`),await this.executeGenerators(t,{schemas:a,index:c}),this.logger.info("Generation completed successfully"),t.success=t.errors.length===0,this.logger.debug(`Generation completed: ${t.filesGenerated.length} files`);}catch(r){if(this.logger.error("Code generation failed",r instanceof Error?r:new Error(String(r))),t.errors.push(r instanceof Error?r.message:String(r)),this.options.throwException)throw r}return {...t,success:t.errors.length===0,duration:performance.now()-e}}async build(){let e={};for(let[t,r]of this.generators.entries())r.build&&(e[t]=await r.build(this.schemas));return e}reset(){return this.schemas=[],this.generators.clear(),this.progressCallback=void 0,this}getSchemas(){return [...this.schemas]}getGenerators(){return Array.from(this.generators.keys())}async loadFromFiles(e){this.typeSchemaGenerator||(this.typeSchemaGenerator=new D({verbose:this.options.verbose,logger:this.logger.child("Schema"),treeshake:this.typeSchemaConfig?.treeshake},this.typeSchemaConfig));let r=await new W({format:"auto"}).parseFromFiles(e);this.schemas=[...this.schemas,...r],this.cache&&this.cache.setMany(r);}async executeGenerators(e,t){for(let[r,i]of this.generators.entries()){this.logger.info(`Generating ${r}...`);try{let s=await i.generate(t);e.filesGenerated.push(...s.map(o=>o.path||o.filename)),this.logger.info(`Generating ${r} finished successfully`);}catch(s){if(e.errors.push(`${r} generator failed: ${s instanceof Error?s.message:String(s)}`),this.options.throwException)throw s}}}};var mt={outputDir:"./generated",verbose:false,overwrite:true,validate:true,cache:true,cleanOutput:true,typescript:{moduleFormat:"esm",generateIndex:true,includeDocuments:false,namingConvention:"PascalCase",strictMode:true,includeProfiles:true,includeExtensions:false,includeCodeSystems:false,includeOperations:false,generateValueSets:false,valueSetDirectory:"valuesets",valueSetMode:"required-only",valueSetStrengths:["required"],includeValueSetHelpers:false,fhirVersion:"R4",resourceTypes:[],maxDepth:10,profileOptions:{generateKind:"interface",includeConstraints:true,includeDocumentation:true,strictMode:false,subfolder:"profiles"},generateBuilders:false,builderOptions:{includeValidation:true,includeFactoryMethods:true,includeInterfaces:true,generateNestedBuilders:true,includeHelperMethods:true,supportPartialBuild:true,includeJSDoc:true,generateFactories:true,includeTypeGuards:true,handleChoiceTypes:true,generateArrayHelpers:true},validatorOptions:{includeCardinality:true,includeTypes:true,includeConstraints:true,includeInvariants:false,validateRequired:true,allowAdditional:false,strictValidation:false,collectMetrics:false,generateAssertions:true,generatePartialValidators:true,optimizePerformance:true,includeJSDoc:true,generateCompositeValidators:true},guardOptions:{includeRuntimeValidation:true,includeErrorMessages:true,treeShakeable:true,targetTSVersion:"5.0",strictGuards:false,includeNullChecks:true,verbose:false}},typeSchema:{enablePersistence:true,cacheDir:".typeschema-cache",maxAge:1440*60*1e3,validateCached:true,forceRegenerate:false,shareCache:true,cacheKeyPrefix:"",treeshake:[],singleFile:false,profiles:{autoDetect:true}},packages:[],files:[],$schema:""},Be=["atomic-codegen.config.ts","atomic-codegen.config","atomic-codegen.config.json",".atomic-codegenrc","atomic-codegen.json",".atomic-codegen.json","codegen.config.json","codegen.json"],yt=class{validate(e){let t={valid:true,errors:[],warnings:[]};if(!e||typeof e!="object")return t.valid=false,t.errors.push({path:"root",message:"Configuration must be an object",value:e}),t;let r=e;r.outputDir!==void 0&&typeof r.outputDir!="string"&&t.errors.push({path:"outputDir",message:"outputDir must be a string",value:r.outputDir});let i=["verbose","overwrite","validate","cache"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.errors.push({path:s,message:`${s} must be a boolean`,value:r[s]});if(r.typescript!==void 0){let s=this.validateTypeScriptConfig(r.typescript);t.errors.push(...s);}if(r.typeSchema!==void 0){let s=this.validateTypeSchemaConfig(r.typeSchema);t.errors.push(...s);}return r.packages!==void 0&&(Array.isArray(r.packages)?r.packages.forEach((s,o)=>{typeof s!="string"&&t.errors.push({path:`packages[${o}]`,message:"package name must be a string",value:s});}):t.errors.push({path:"packages",message:"packages must be an array",value:r.packages})),r.files!==void 0&&(Array.isArray(r.files)?r.files.forEach((s,o)=>{typeof s!="string"&&t.errors.push({path:`files[${o}]`,message:"file path must be a string",value:s});}):t.errors.push({path:"files",message:"files must be an array",value:r.files})),t.valid=t.errors.length===0,t.valid&&(t.config=r),t}validateTypeScriptConfig(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript",message:"typescript config must be an object",value:e}),t;let r=e;r.moduleFormat!==void 0&&(["esm","cjs"].includes(r.moduleFormat)||t.push({path:"typescript.moduleFormat",message:'moduleFormat must be "esm" or "cjs"',value:r.moduleFormat})),r.namingConvention!==void 0&&(["PascalCase","camelCase"].includes(r.namingConvention)||t.push({path:"typescript.namingConvention",message:'namingConvention must be "PascalCase" or "camelCase"',value:r.namingConvention}));let i=["generateIndex","includeDocuments","strictMode","includeProfiles","includeExtensions","includeCodeSystems","includeOperations","generateValueSets","includeValueSetHelpers"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typescript.${s}`,message:`${s} must be a boolean`,value:r[s]});if(r.validatorOptions!==void 0){let s=this.validateValidatorOptions(r.validatorOptions);t.push(...s);}if(r.guardOptions!==void 0){let s=this.validateGuardOptions(r.guardOptions);t.push(...s);}if(r.profileOptions!==void 0){let s=this.validateProfileOptions(r.profileOptions);t.push(...s);}return t}validateValidatorOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.validatorOptions",message:"validatorOptions must be an object",value:e}),t;let r=e,i=["includeCardinality","includeTypes","includeConstraints","includeInvariants","validateRequired","allowAdditional","strictValidation","collectMetrics","generateAssertions","generatePartialValidators","optimizePerformance","includeJSDoc","generateCompositeValidators"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typescript.validatorOptions.${s}`,message:`${s} must be a boolean`,value:r[s]});return t}validateGuardOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.guardOptions",message:"guardOptions must be an object",value:e}),t;let r=e;r.targetTSVersion!==void 0&&(["3.8","4.0","4.5","5.0"].includes(r.targetTSVersion)||t.push({path:"typescript.guardOptions.targetTSVersion",message:'targetTSVersion must be one of: "3.8", "4.0", "4.5", "5.0"',value:r.targetTSVersion}));let i=["includeRuntimeValidation","includeErrorMessages","treeShakeable","strictGuards","includeNullChecks","verbose"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typescript.guardOptions.${s}`,message:`${s} must be a boolean`,value:r[s]});return t}validateProfileOptions(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typescript.profileOptions",message:"profileOptions must be an object",value:e}),t;let r=e;r.generateKind!==void 0&&(["interface","type","both"].includes(r.generateKind)||t.push({path:"typescript.profileOptions.generateKind",message:'generateKind must be "interface", "type", or "both"',value:r.generateKind})),r.subfolder!==void 0&&typeof r.subfolder!="string"&&t.push({path:"typescript.profileOptions.subfolder",message:"subfolder must be a string",value:r.subfolder});let i=["includeConstraints","includeDocumentation","strictMode"];for(let s of i)r[s]!==void 0&&typeof r[s]!="boolean"&&t.push({path:`typescript.profileOptions.${s}`,message:`${s} must be a boolean`,value:r[s]});return t}validateTypeSchemaConfig(e){let t=[];if(typeof e!="object"||e===null)return t.push({path:"typeSchema",message:"typeSchema config must be an object",value:e}),t;let r=e,i=["enablePersistence","validateCached","forceRegenerate","shareCache"];for(let o of i)r[o]!==void 0&&typeof r[o]!="boolean"&&t.push({path:`typeSchema.${o}`,message:`${o} must be a boolean`,value:r[o]});let s=["cacheDir","cacheKeyPrefix"];for(let o of s)r[o]!==void 0&&typeof r[o]!="string"&&t.push({path:`typeSchema.${o}`,message:`${o} must be a string`,value:r[o]});if(r.maxAge!==void 0&&(typeof r.maxAge!="number"||r.maxAge<=0)&&t.push({path:"typeSchema.maxAge",message:"maxAge must be a positive number",value:r.maxAge}),r.profiles!==void 0)if(typeof r.profiles!="object"||r.profiles===null)t.push({path:"typeSchema.profiles",message:"profiles must be an object",value:r.profiles});else {let o=r.profiles;o.autoDetect!==void 0&&typeof o.autoDetect!="boolean"&&t.push({path:"typeSchema.profiles.autoDetect",message:"autoDetect must be a boolean",value:o.autoDetect});}return t}},St=class{validator=new yt;async autoload(e=process.cwd()){let t=await this.findConfigFile(e);return t?this.loadFromFile(t):{...mt}}async loadFromFile(e){try{let t;if(e.endsWith(".ts")||e.endsWith("")){let s=await import(resolve(e));t=s.default||s;}else {let i=await readFile(e,"utf-8");t=JSON.parse(i);}let r=this.validator.validate(t);if(!r.valid){let i=r.errors.map(s=>`${s.path}: ${s.message}`).join(`
|
|
43
43
|
`);throw new Error(`Configuration validation failed:
|
|
44
|
-
${i}`)}return this.mergeWithDefaults(r.config)}catch(t){throw t instanceof Error?new Error(`Failed to load config from ${e}: ${t.message}`):t}}async findConfigFile(e){for(let t of Be){let r=resolve(e,t);if(existsSync(r))return r}return null}mergeWithDefaults(e){return {...mt,...e,typescript:{...mt.typescript,...e.typescript}}}},
|
|
44
|
+
${i}`)}return this.mergeWithDefaults(r.config)}catch(t){throw t instanceof Error?new Error(`Failed to load config from ${e}: ${t.message}`):t}}async findConfigFile(e){for(let t of Be){let r=resolve(e,t);if(existsSync(r))return r}return null}mergeWithDefaults(e){return {...mt,...e,typescript:{...mt.typescript,...e.typescript}}}},xi=new St;async function Le(n){return xi.autoload(n)}var br={command:"generate",describe:"Generate code based on configuration file settings",builder:n=>n.option("verbose",{alias:"v",type:"boolean",default:false,description:"Enable verbose output"}).example("$0 generate","Generate code using settings from config file").example("$0 generate --verbose","Generate with verbose output"),handler:async n=>{if(n._.length>1){let o=n._.slice(1).join(" ");C(`Invalid syntax: 'atomic-codegen generate ${o}'
|
|
45
45
|
|
|
46
46
|
The CLI has been simplified and no longer uses subcommands.
|
|
47
47
|
|
|
@@ -49,7 +49,7 @@ The CLI has been simplified and no longer uses subcommands.
|
|
|
49
49
|
\u274C Old: atomic-codegen generate typescript
|
|
50
50
|
|
|
51
51
|
All generation settings are now configured in your config file.
|
|
52
|
-
Create an atomic-codegen.config.ts file to get started.`),process.exit(1);}let e=process.cwd(),t=await
|
|
52
|
+
Create an atomic-codegen.config.ts file to get started.`),process.exit(1);}let e=process.cwd(),t=await Ci(e);if(!t){let o=Be.map(a=>` - ${a}`).join(`
|
|
53
53
|
`);C(`No configuration file found. Please create one of the following files in your project root:
|
|
54
54
|
${o}
|
|
55
55
|
|
|
@@ -62,13 +62,13 @@ export default defineConfig({
|
|
|
62
62
|
typescript: {
|
|
63
63
|
generateIndex: true
|
|
64
64
|
}
|
|
65
|
-
});`),process.exit(1);}let r=await Le(e),i=n.verbose??r.verbose??false,s=I({verbose:i,prefix:"Generate"});try{Ue("Starting generation from config"),i&&(s.info(`Config file: ${t}`),s.info(`Output directory: ${r.outputDir||"./generated"}`),s.info(`Packages: ${r.packages?.length||0}`),s.info(`Files: ${r.files?.length||0}`),s.info(`TypeScript generation: ${r.typescript?"enabled":"disabled"}`));let o=new oe({outputDir:r.outputDir||"./generated",verbose:i,overwrite:r.overwrite??!0,cache:r.cache??!0,typeSchemaConfig:r.typeSchema,logger:s});if(r.packages&&r.packages.length>0){s.info(`Loading packages from config: ${r.packages.join(", ")}`);for(let c of r.packages){let[l,p]=c.includes("@")?c.split("@"):[c,void 0];o.fromPackage(l,p);}}else if(r.files&&r.files.length>0){s.info(`Loading files from config: ${r.files.join(", ")}`);for(let c of r.files)o.fromFiles(c);}else throw new Error("No data source specified in config. Please configure 'packages' or 'files' in your config file.");if(r.typescript&&(i&&(s.info("Configuring TypeScript generation from config"),s.debug(`Module format: ${r.typescript.moduleFormat||"esm"}`),s.debug(`Generate index: ${r.typescript.generateIndex??!0}`),s.debug(`Include docs: ${r.typescript.includeDocuments??!1}`),s.debug(`Naming convention: ${r.typescript.namingConvention||"PascalCase"}`)),o.typescriptDepricated(r.typescript)),!r.typescript)throw new Error("No generators configured. Please enable 'typescript' in your config file.");i&&o.onProgress((c,l,p,d)=>{let m=Math.round(l/p*100);s.progress(`[${c}] ${m}% - ${d||"Processing..."}`);}),s.step("Executing generation...");let a=await o.generate();if(a.success){if(pe(`Generated ${a.filesGenerated.length} files in ${a.duration.toFixed(2)}ms`),s.dim(`Output directory: ${a.outputDir}`),a.warnings.length>0)for(let c of a.warnings)Ge(c);}else {C(`Generation failed with ${a.errors.length} errors`);for(let c of a.errors)s.dim(` ${c}`);process.exit(1);}}catch(o){C("Generation failed with unexpected error",o instanceof Error?o:new Error(String(o))),process.exit(1);}}};async function
|
|
65
|
+
});`),process.exit(1);}let r=await Le(e),i=n.verbose??r.verbose??false,s=I({verbose:i,prefix:"Generate"});try{Ue("Starting generation from config"),i&&(s.info(`Config file: ${t}`),s.info(`Output directory: ${r.outputDir||"./generated"}`),s.info(`Packages: ${r.packages?.length||0}`),s.info(`Files: ${r.files?.length||0}`),s.info(`TypeScript generation: ${r.typescript?"enabled":"disabled"}`));let o=new oe({outputDir:r.outputDir||"./generated",verbose:i,overwrite:r.overwrite??!0,cache:r.cache??!0,typeSchemaConfig:r.typeSchema,logger:s});if(r.packages&&r.packages.length>0){s.info(`Loading packages from config: ${r.packages.join(", ")}`);for(let c of r.packages){let[l,p]=c.includes("@")?c.split("@"):[c,void 0];o.fromPackage(l,p);}}else if(r.files&&r.files.length>0){s.info(`Loading files from config: ${r.files.join(", ")}`);for(let c of r.files)o.fromFiles(c);}else throw new Error("No data source specified in config. Please configure 'packages' or 'files' in your config file.");if(r.typescript&&(i&&(s.info("Configuring TypeScript generation from config"),s.debug(`Module format: ${r.typescript.moduleFormat||"esm"}`),s.debug(`Generate index: ${r.typescript.generateIndex??!0}`),s.debug(`Include docs: ${r.typescript.includeDocuments??!1}`),s.debug(`Naming convention: ${r.typescript.namingConvention||"PascalCase"}`)),o.typescriptDepricated(r.typescript)),!r.typescript)throw new Error("No generators configured. Please enable 'typescript' in your config file.");i&&o.onProgress((c,l,p,d)=>{let m=Math.round(l/p*100);s.progress(`[${c}] ${m}% - ${d||"Processing..."}`);}),s.step("Executing generation...");let a=await o.generate();if(a.success){if(pe(`Generated ${a.filesGenerated.length} files in ${a.duration.toFixed(2)}ms`),s.dim(`Output directory: ${a.outputDir}`),a.warnings.length>0)for(let c of a.warnings)Ge(c);}else {C(`Generation failed with ${a.errors.length} errors`);for(let c of a.errors)s.dim(` ${c}`);process.exit(1);}}catch(o){C("Generation failed with unexpected error",o instanceof Error?o:new Error(String(o))),process.exit(1);}}};async function Ci(n){for(let e of Be){let t=resolve(n,e);if(existsSync(t))return t}return null}var vr={command:"generate <packages..>",describe:"Generate TypeSchema files from FHIR packages",builder:{packages:{type:"string",array:true,demandOption:true,describe:"FHIR packages to process (e.g., hl7.fhir.r4.core@4.0.1)"},output:{alias:"o",type:"string",describe:"Output file or directory",default:"./schemas.ndjson"},format:{alias:"f",type:"string",choices:["ndjson","json"],default:"ndjson",describe:"Output format for TypeSchema files"},treeshake:{alias:"t",type:"string",array:true,describe:"Only generate TypeSchemas for specific ResourceTypes (treeshaking)"},singleFile:{alias:"s",type:"boolean",default:false,describe:"Generate single TypeSchema file instead of multiple files (NDJSON format)"},verbose:{alias:"v",type:"boolean",default:false,describe:"Enable verbose output"}},handler:async n=>{let e=I({verbose:n.verbose,prefix:"TypeSchema"});try{let t=await Le(process.cwd());e.step("Generating TypeSchema from FHIR packages"),e.info(`Packages: ${n.packages.join(", ")}`),e.info(`Output: ${n.output}`);let r=n.singleFile!==void 0?n.singleFile:t.typeSchema?.singleFile??!1,i=r?"ndjson":n.format;e.debug(`Format: ${i}${r&&n.format==="json"?" (forced from json due to singleFile)":""}`);let s=n.treeshake&&n.treeshake.length>0?n.treeshake:t.typeSchema?.treeshake;s&&s.length>0&&e.info(`Treeshaking enabled for ResourceTypes: ${s.join(", ")}`),r&&e.info("Single file output enabled (NDJSON format)");let o=Date.now(),a=new D({verbose:n.verbose,treeshake:s}),c=[];for(let m of n.packages){let[g,h]=m.includes("@")?m.split("@"):[m,void 0];e.progress(`Processing package: ${g}${h?`@${h}`:""}`);let u=await a.generateFromPackage(g,h,e);c.push(...u);}if(c.length===0)throw new Error("No schemas were generated from the specified packages");let l=n.output;await mkdir(dirname(l),{recursive:!0});let p;i==="json"?p=JSON.stringify(c,null,2):p=c.map(m=>JSON.stringify(m)).join(`
|
|
66
66
|
`),await writeFile(l,p,"utf-8");let d=Date.now()-o;if(ze(`Generated ${c.length} TypeSchema definitions`,d,{schemas:c.length}),e.dim(`Output: ${l}`),n.verbose){e.debug("Generated schemas:");let m=c.map(g=>`${g.identifier?.name||"Unknown"} (${g.identifier?.kind||"unknown"})`);j(m);}}catch(t){e.error("Failed to generate TypeSchema",t instanceof Error?t:new Error(String(t))),process.exit(1);}}};var Tr={command:"typeschema [subcommand]",describe:"TypeSchema operations - generate, validate and merge schemas",builder:n=>n.command(vr).help().example("$0 typeschema generate hl7.fhir.r4.core@4.0.1","Generate TypeSchema from FHIR R4 core package"),handler:n=>{if(!n.subcommand&&n._.length===1){ue("Available typeschema subcommands:"),j(["generate Generate TypeSchema files from FHIR packages"]),console.log(`
|
|
67
67
|
Use 'atomic-codegen typeschema <subcommand> --help' for more information about a subcommand.`),console.log(`
|
|
68
68
|
Examples:`),j(["atomic-codegen typeschema generate hl7.fhir.r4.core@4.0.1 -o schemas.ndjson","atomic-codegen typeschema validate schemas.ndjson","atomic-codegen typeschema merge schema1.ndjson schema2.ndjson -o merged.ndjson"]);return}n.subcommand&&!["generate","validate","merge"].includes(n.subcommand)&&(C(`Unknown typeschema subcommand: ${n.subcommand}
|
|
69
69
|
`),ue("Available typeschema subcommands:"),j(["generate Generate TypeSchema files from FHIR packages","validate Validate TypeSchema files for correctness and consistency","merge Merge multiple TypeSchema files into a single file"]),console.log(`
|
|
70
|
-
Use 'atomic-codegen typeschema <subcommand> --help' for more information about a subcommand.`),process.exit(1));}};async function
|
|
70
|
+
Use 'atomic-codegen typeschema <subcommand> --help' for more information about a subcommand.`),process.exit(1));}};async function Pi(n){He({verbose:n.verbose||n.debug,timestamp:n.debug});}function Mi(){return Ii(hideBin(process.argv)).scriptName("atomic-codegen").usage("$0 <command> [options]").middleware(Pi).command(Tr).command(br).option("verbose",{alias:"v",type:"boolean",description:"Enable verbose output",default:false,global:true}).option("debug",{alias:"d",type:"boolean",description:"Enable debug output with detailed logging",default:false,global:true}).option("config",{alias:"c",type:"string",description:"Path to configuration file (.atomic-codegen.json by default)",global:true}).demandCommand(0).middleware(n=>{n._.length===0&&(_e("Welcome to Atomic Codegen!"),console.log("Available commands:"),console.log(" typeschema Generate, validate and merge TypeSchema files"),console.log(" generate Generate code based on configuration file"),console.log(`
|
|
71
71
|
Use 'atomic-codegen <command> --help' for more information about a command.`),console.log(`
|
|
72
72
|
Quick examples:`),console.log(" atomic-codegen typeschema generate hl7.fhir.r4.core@4.0.1 -o schemas.ndjson"),console.log(" atomic-codegen generate # Uses atomic-codegen.config.ts"),console.log(`
|
|
73
73
|
Use 'atomic-codegen --help' to see all options.`),process.exit(0));}).help().version("0.1.0").example("$0 generate","Generate code using atomic-codegen.config.ts").example("$0 generate --verbose","Generate with detailed progress output").example("$0 --config custom-config.ts generate","Use custom configuration file").example("$0 typeschema generate hl7.fhir.r4.core@4.0.1 -o schemas.ndjson","Generate TypeSchemas from FHIR package").fail((n,e,t)=>{e?C(e.message,e):C(n),C(`
|
|
74
|
-
Use --help for usage information`),process.exit(1);}).wrap(Math.min(120,process.stdout.columns||80))}async function bt(){await
|
|
74
|
+
Use --help for usage information`),process.exit(1);}).wrap(Math.min(120,process.stdout.columns||80))}async function bt(){await Mi().parseAsync();}import.meta.main&&bt().catch(n=>{n("Unexpected error:",n),process.exit(1);});bt().catch(n=>{console.error("CLI Error:",n instanceof Error?n.message:n),process.exit(1);});
|
package/dist/index.d.ts
CHANGED
|
@@ -794,6 +794,15 @@ type WriterOptions = {
|
|
|
794
794
|
logger?: CodegenLogger;
|
|
795
795
|
};
|
|
796
796
|
|
|
797
|
+
type TypeScriptOptions = {
|
|
798
|
+
/** openResourceTypeSet -- for resource families (Resource, DomainResource) use open set for resourceType field.
|
|
799
|
+
*
|
|
800
|
+
* - when openResourceTypeSet is false: `type Resource = { resourceType: "Resource" | "DomainResource" | "Patient" }`
|
|
801
|
+
* - when openResourceTypeSet is true: `type Resource = { resourceType: "Resource" | "DomainResource" | "Patient" | string }`
|
|
802
|
+
*/
|
|
803
|
+
openResourceTypeSet: boolean;
|
|
804
|
+
} & WriterOptions;
|
|
805
|
+
|
|
797
806
|
/**
|
|
798
807
|
* High-Level API Builder
|
|
799
808
|
*
|
|
@@ -868,7 +877,7 @@ declare class APIBuilder {
|
|
|
868
877
|
valueSetMode?: "all" | "required-only" | "custom";
|
|
869
878
|
valueSetDirectory?: string;
|
|
870
879
|
}): APIBuilder;
|
|
871
|
-
typescript(
|
|
880
|
+
typescript(userOpts: Partial<TypeScriptOptions>): this;
|
|
872
881
|
csharp(namespace: string, staticSourceDir?: string | undefined): APIBuilder;
|
|
873
882
|
/**
|
|
874
883
|
* Set a progress callback for monitoring generation
|
package/dist/index.js
CHANGED
|
@@ -5905,8 +5905,9 @@ var TypeScript = class extends Writer {
|
|
|
5905
5905
|
if (isResourceTypeSchema(schema)) {
|
|
5906
5906
|
const possibleResourceTypes = [schema.identifier];
|
|
5907
5907
|
possibleResourceTypes.push(...tsIndex.resourceChildren(schema.identifier));
|
|
5908
|
+
const openSetSuffix = this.opts.openResourceTypeSet && possibleResourceTypes.length > 1 ? " | string" : "";
|
|
5908
5909
|
this.lineSM(
|
|
5909
|
-
`resourceType: ${possibleResourceTypes.sort((a, b) => a.name.localeCompare(b.name)).map((e) => `"${e.name}"`).join(" | ")}`
|
|
5910
|
+
`resourceType: ${possibleResourceTypes.sort((a, b) => a.name.localeCompare(b.name)).map((e) => `"${e.name}"`).join(" | ")}${openSetSuffix}`
|
|
5910
5911
|
);
|
|
5911
5912
|
this.line();
|
|
5912
5913
|
}
|
|
@@ -6336,19 +6337,26 @@ var APIBuilder = class {
|
|
|
6336
6337
|
this.logger.debug(`Configured TypeScript generator (${options.moduleFormat || "esm"})`);
|
|
6337
6338
|
return this;
|
|
6338
6339
|
}
|
|
6339
|
-
typescript(
|
|
6340
|
-
const
|
|
6340
|
+
typescript(userOpts) {
|
|
6341
|
+
const defaultWriterOpts = {
|
|
6342
|
+
logger: this.logger,
|
|
6341
6343
|
outputDir: Path4.join(this.options.outputDir, "/types"),
|
|
6342
6344
|
tabSize: 4,
|
|
6343
6345
|
withDebugComment: false,
|
|
6344
6346
|
commentLinePrefix: "//",
|
|
6345
|
-
generateProfile: true
|
|
6346
|
-
|
|
6347
|
+
generateProfile: true
|
|
6348
|
+
};
|
|
6349
|
+
const defaultTsOpts = {
|
|
6350
|
+
...defaultWriterOpts,
|
|
6351
|
+
openResourceTypeSet: false
|
|
6352
|
+
};
|
|
6353
|
+
const opts = {
|
|
6354
|
+
...defaultTsOpts,
|
|
6355
|
+
...Object.fromEntries(Object.entries(userOpts).filter(([_, v]) => v !== void 0))
|
|
6347
6356
|
};
|
|
6348
|
-
const
|
|
6349
|
-
const generator = writerToGenerator(new TypeScript(effectiveOpts));
|
|
6357
|
+
const generator = writerToGenerator(new TypeScript(opts));
|
|
6350
6358
|
this.generators.set("typescript", generator);
|
|
6351
|
-
this.logger.debug(`Configured TypeScript generator (${JSON.stringify(
|
|
6359
|
+
this.logger.debug(`Configured TypeScript generator (${JSON.stringify(opts, void 0, 2)})`);
|
|
6352
6360
|
return this;
|
|
6353
6361
|
}
|
|
6354
6362
|
csharp(namespace, staticSourceDir) {
|