@atomic-ehr/codegen 0.0.4-canary.20251222144228.ac2a1d7 → 0.0.4-canary.20251223085924.a5813d1

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/index.js CHANGED
@@ -3,11 +3,11 @@ import * as fs from 'fs';
3
3
  import fs__default, { existsSync } from 'fs';
4
4
  import * as afs2 from 'fs/promises';
5
5
  import { readFile } from 'fs/promises';
6
- import * as Path5 from 'path';
7
- import Path5__default, { resolve } from 'path';
6
+ import * as Path from 'path';
7
+ import Path__default, { resolve } from 'path';
8
8
  import { CanonicalManager } from '@atomic-ehr/fhir-canonical-manager';
9
- import assert3 from 'assert';
10
9
  import { fileURLToPath } from 'url';
10
+ import assert3 from 'assert';
11
11
  import * as YAML from 'yaml';
12
12
  import * as fhirschema from '@atomic-ehr/fhirschema';
13
13
  import { isStructureDefinition } from '@atomic-ehr/fhirschema';
@@ -244,7 +244,7 @@ var FileSystemWriter = class {
244
244
  }
245
245
  cd(path, gen) {
246
246
  const prev = this.currentDir;
247
- this.currentDir = path.startsWith("/") ? Path5.join(this.opts.outputDir, path) : Path5.join(this.currentDir ?? this.opts.outputDir, path);
247
+ this.currentDir = path.startsWith("/") ? Path.join(this.opts.outputDir, path) : Path.join(this.currentDir ?? this.opts.outputDir, path);
248
248
  this.onDiskMkDir(this.currentDir);
249
249
  this.logger()?.debug(`cd '${this.currentDir}'`);
250
250
  gen();
@@ -253,12 +253,16 @@ var FileSystemWriter = class {
253
253
  cat(fn, gen) {
254
254
  if (this.currentFile) throw new Error("Can't open file when another file is open");
255
255
  if (fn.includes("/")) throw new Error(`Change file path separatly: ${fn}`);
256
- const relPath = Path5.normalize(`${this.currentDir}/${fn}`);
256
+ const relPath = Path.normalize(`${this.currentDir}/${fn}`);
257
257
  try {
258
258
  const descriptor = this.onDiskOpenFile(relPath);
259
259
  this.logger()?.debug(`cat > '${relPath}'`);
260
260
  this.currentFile = { descriptor, relPath };
261
- this.writtenFilesBuffer[this.currentFile.relPath] = { relPath, absPath: Path5.resolve(relPath), tokens: [] };
261
+ this.writtenFilesBuffer[this.currentFile.relPath] = {
262
+ relPath,
263
+ absPath: Path.resolve(relPath),
264
+ tokens: []
265
+ };
262
266
  gen();
263
267
  } finally {
264
268
  if (this.currentFile) this.onDiskCloseFile(this.currentFile.descriptor);
@@ -272,9 +276,21 @@ var FileSystemWriter = class {
272
276
  if (!buf) throw new Error("No buffer found");
273
277
  buf.tokens.push(str);
274
278
  }
279
+ cp(source, destination) {
280
+ if (!this.opts.resolveAssets) throw new Error("resolveAssets is not defined");
281
+ source = Path.resolve(this.opts.resolveAssets(source));
282
+ destination = Path.normalize(`${this.currentDir ?? this.opts.outputDir}/${destination}`);
283
+ const content = fs.readFileSync(source, "utf8");
284
+ this.writtenFilesBuffer[destination] = {
285
+ relPath: destination,
286
+ absPath: Path.resolve(destination),
287
+ tokens: [content]
288
+ };
289
+ fs.cpSync(source, destination);
290
+ }
275
291
  writtenFiles() {
276
292
  return Object.values(this.writtenFilesBuffer).map(({ relPath, absPath, tokens }) => {
277
- return { relPath, absPath, content: tokens.join() };
293
+ return { relPath, absPath, content: tokens.join("") };
278
294
  }).sort((a, b) => a.relPath.localeCompare(b.relPath));
279
295
  }
280
296
  };
@@ -488,6 +504,15 @@ function formatName(input) {
488
504
  }
489
505
 
490
506
  // src/api/writer-generator/csharp/csharp.ts
507
+ var resolveCSharpAssets = (fn) => {
508
+ const __filename2 = fileURLToPath(import.meta.url);
509
+ const __dirname = Path__default.dirname(__filename2);
510
+ if (__filename2.endsWith("dist/index.js")) {
511
+ return Path__default.resolve(__dirname, "..", "assets", "api", "writer-generator", "csharp", fn);
512
+ } else {
513
+ return Path__default.resolve(__dirname, "../../../..", "assets", "api", "writer-generator", "csharp", fn);
514
+ }
515
+ };
491
516
  var PRIMITIVE_TYPE_MAP = {
492
517
  boolean: "bool",
493
518
  instant: "string",
@@ -553,6 +578,7 @@ var CSharp = class extends Writer {
553
578
  tabSize: 4,
554
579
  withDebugComment: false,
555
580
  commentLinePrefix: "//",
581
+ resolveAssets: options.resolveAssets ?? resolveCSharpAssets,
556
582
  ...options
557
583
  });
558
584
  }
@@ -658,8 +684,8 @@ var CSharp = class extends Writer {
658
684
  "CSharpSDK",
659
685
  "System.Text.Json",
660
686
  "System.Text.Json.Serialization",
661
- this.opts.targetNamespace,
662
- ...packages.map((pkg) => `${this.opts.targetNamespace}.${pkg}`)
687
+ this.opts.rootNamespace,
688
+ ...packages.map((pkg) => `${this.opts.rootNamespace}.${pkg}`)
663
689
  ];
664
690
  for (const using of globalUsings) this.lineSM("global", "using", using);
665
691
  }
@@ -668,7 +694,7 @@ var CSharp = class extends Writer {
668
694
  this.cat("base.cs", () => {
669
695
  this.generateDisclaimer();
670
696
  this.line();
671
- this.lineSM("namespace", this.opts.targetNamespace);
697
+ this.lineSM("namespace", this.opts.rootNamespace);
672
698
  for (const schema of complexTypes) {
673
699
  const packageName = formatName(schema.identifier.package);
674
700
  this.generateType(schema, packageName);
@@ -685,7 +711,7 @@ var CSharp = class extends Writer {
685
711
  this.cat(`${schema.identifier.name}.cs`, () => {
686
712
  this.generateDisclaimer();
687
713
  this.line();
688
- this.lineSM("namespace", `${this.opts.targetNamespace}.${packageName}`);
714
+ this.lineSM("namespace", `${this.opts.rootNamespace}.${packageName}`);
689
715
  this.line();
690
716
  this.generateType(schema, packageName);
691
717
  });
@@ -709,7 +735,7 @@ var CSharp = class extends Writer {
709
735
  generateEnumFileContent(packageName, enums) {
710
736
  this.lineSM("using", "System.ComponentModel");
711
737
  this.line();
712
- this.lineSM(`namespace ${this.opts.targetNamespace}.${packageName}`);
738
+ this.lineSM(`namespace ${this.opts.rootNamespace}.${packageName}`);
713
739
  for (const [enumName, values] of Object.entries(enums)) {
714
740
  this.generateEnum(enumName, values);
715
741
  }
@@ -731,7 +757,7 @@ var CSharp = class extends Writer {
731
757
  this.cat(`${packageName}ResourceDictionary.cs`, () => {
732
758
  this.generateDisclaimer();
733
759
  this.line();
734
- this.lineSM(`namespace ${this.opts.targetNamespace}`);
760
+ this.lineSM(`namespace ${this.opts.rootNamespace}`);
735
761
  this.generateResourceDictionaryClass(packageName, packageResources);
736
762
  });
737
763
  }
@@ -749,13 +775,13 @@ var CSharp = class extends Writer {
749
775
  });
750
776
  }
751
777
  copyStaticFiles() {
752
- if (!this.opts.staticSourceDir) return;
753
- const sourcePath = Path5__default.resolve(this.opts.staticSourceDir);
754
- fs__default.cpSync(sourcePath, this.opts.outputDir, { recursive: true });
778
+ this.cp("Client.cs", "Client.cs");
779
+ this.cp("Helper.cs", "Helper.cs");
755
780
  }
756
781
  generateHelperFile() {
757
- const sourceFile = "src/api/writer-generator/csharp/Helper.cs";
758
- const destFile = Path5__default.join(this.opts.outputDir, "Helper.cs");
782
+ if (this.opts.inMemoryOnly) return;
783
+ const sourceFile = resolveCSharpAssets("Helper.cs");
784
+ const destFile = Path__default.join(this.opts.outputDir, "Helper.cs");
759
785
  fs__default.copyFileSync(sourceFile, destFile);
760
786
  }
761
787
  };
@@ -978,7 +1004,7 @@ var mkTypeSchemaIndex = (schemas, { resolutionTree, logger }) => {
978
1004
  }
979
1005
  }
980
1006
  const raw = filename.endsWith(".yaml") ? YAML.stringify(tree) : JSON.stringify(tree, void 0, 2);
981
- await afs2.mkdir(Path5.dirname(filename), { recursive: true });
1007
+ await afs2.mkdir(Path.dirname(filename), { recursive: true });
982
1008
  await afs2.writeFile(filename, raw);
983
1009
  };
984
1010
  return {
@@ -1100,18 +1126,18 @@ var deriveResourceName = (id) => {
1100
1126
  return pascalCase(id.name);
1101
1127
  };
1102
1128
  var resolvePyAssets = (fn) => {
1103
- const __dirname = Path5.dirname(fileURLToPath(import.meta.url));
1129
+ const __dirname = Path.dirname(fileURLToPath(import.meta.url));
1104
1130
  if (__filename.endsWith("dist/index.js")) {
1105
- return Path5.resolve(__dirname, "..", "assets", "api", "writer-generator", "python", fn);
1131
+ return Path.resolve(__dirname, "..", "assets", "api", "writer-generator", "python", fn);
1106
1132
  } else {
1107
- return Path5.resolve(__dirname, "../../..", "assets", "api", "writer-generator", "python", fn);
1133
+ return Path.resolve(__dirname, "../../..", "assets", "api", "writer-generator", "python", fn);
1108
1134
  }
1109
1135
  };
1110
1136
  var Python = class extends Writer {
1111
1137
  nameFormatFunction;
1112
1138
  tsIndex;
1113
1139
  constructor(options) {
1114
- super(options);
1140
+ super({ ...options, resolveAssets: options.resolveAssets ?? resolvePyAssets });
1115
1141
  this.nameFormatFunction = this.getFieldFormatFunction(options.fieldFormat);
1116
1142
  }
1117
1143
  async generate(tsIndex) {
@@ -1125,7 +1151,7 @@ var Python = class extends Writer {
1125
1151
  }
1126
1152
  generateRootPackages(groups) {
1127
1153
  this.generateRootInitFile(groups);
1128
- fs__default.cpSync(resolvePyAssets("requirements.txt"), Path5.resolve(this.opts.outputDir, "requirements.txt"));
1154
+ this.cp(resolvePyAssets("requirements.txt"), "requirements.txt");
1129
1155
  }
1130
1156
  generateSDKPackages(groups) {
1131
1157
  this.generateComplexTypesPackages(groups.groupedComplexTypes);
@@ -2875,7 +2901,7 @@ var writeTypeSchemasToSeparateFiles = async (typeSchemas, outputDir, logger) =>
2875
2901
  const pkgPath = normalizeFileName(packageMetaToFhir(pkg));
2876
2902
  const name = normalizeFileName(`${ts.identifier.name}(${extractNameFromCanonical(ts.identifier.url)})`);
2877
2903
  const json = JSON.stringify(ts, null, 2);
2878
- const baseName = Path5.join(outputDir, pkgPath, name);
2904
+ const baseName = Path.join(outputDir, pkgPath, name);
2879
2905
  if (!files[baseName]) files[baseName] = [];
2880
2906
  if (!files[baseName]?.some((e) => e === json)) {
2881
2907
  files[baseName].push(json);
@@ -2890,7 +2916,7 @@ var writeTypeSchemasToSeparateFiles = async (typeSchemas, outputDir, logger) =>
2890
2916
  } else {
2891
2917
  fullName = `${baseName}-${index}.typeschema.json`;
2892
2918
  }
2893
- await afs2.mkdir(Path5.dirname(fullName), { recursive: true });
2919
+ await afs2.mkdir(Path.dirname(fullName), { recursive: true });
2894
2920
  await afs2.writeFile(fullName, json);
2895
2921
  })
2896
2922
  );
@@ -2898,7 +2924,7 @@ var writeTypeSchemasToSeparateFiles = async (typeSchemas, outputDir, logger) =>
2898
2924
  };
2899
2925
  var writeTypeSchemasToSingleFile = async (typeSchemas, outputFile, logger) => {
2900
2926
  logger.info(`Writing TypeSchema files to: ${outputFile}`);
2901
- await afs2.mkdir(Path5.dirname(outputFile), { recursive: true });
2927
+ await afs2.mkdir(Path.dirname(outputFile), { recursive: true });
2902
2928
  logger.info(`Writing TypeSchemas to one file ${outputFile}...`);
2903
2929
  for (const ts of typeSchemas) {
2904
2930
  const json = JSON.stringify(ts, null, 2);
@@ -2909,7 +2935,7 @@ var writeTypeSchemasToSingleFile = async (typeSchemas, outputFile, logger) => {
2909
2935
  var tryWriteTypeSchema = async (typeSchemas, opts, logger) => {
2910
2936
  if (!opts.typeSchemaOutputDir) return;
2911
2937
  try {
2912
- if (Path5.extname(opts.typeSchemaOutputDir) === ".ndjson") {
2938
+ if (Path.extname(opts.typeSchemaOutputDir) === ".ndjson") {
2913
2939
  await writeTypeSchemasToSingleFile(typeSchemas, opts.typeSchemaOutputDir, logger);
2914
2940
  } else {
2915
2941
  await writeTypeSchemasToSeparateFiles(typeSchemas, opts.typeSchemaOutputDir, logger);
@@ -2972,7 +2998,7 @@ var APIBuilder = class {
2972
2998
  return this;
2973
2999
  }
2974
3000
  localTgzPackage(archivePath) {
2975
- this.localTgzArchives.push(Path5.resolve(archivePath));
3001
+ this.localTgzArchives.push(Path.resolve(archivePath));
2976
3002
  return this;
2977
3003
  }
2978
3004
  fromSchemas(schemas) {
@@ -2983,7 +3009,7 @@ var APIBuilder = class {
2983
3009
  typescript(userOpts) {
2984
3010
  const defaultWriterOpts = {
2985
3011
  logger: this.logger,
2986
- outputDir: Path5.join(this.options.outputDir, "/types"),
3012
+ outputDir: Path.join(this.options.outputDir, "/types"),
2987
3013
  tabSize: 4,
2988
3014
  withDebugComment: false,
2989
3015
  commentLinePrefix: "//",
@@ -3025,18 +3051,24 @@ var APIBuilder = class {
3025
3051
  this.logger.debug(`Configured python generator`);
3026
3052
  return this;
3027
3053
  }
3028
- csharp(namespace, staticSourceDir) {
3029
- const generator = new CSharp({
3030
- outputDir: Path5.join(this.options.outputDir, "/types"),
3031
- staticSourceDir: staticSourceDir ?? void 0,
3032
- targetNamespace: namespace,
3033
- logger: new CodegenLogger({
3034
- prefix: "C#",
3035
- timestamp: true,
3036
- suppressLoggingLevel: []
3037
- })
3038
- });
3039
- this.generators.set("C#", generator);
3054
+ csharp(userOptions) {
3055
+ const defaultWriterOpts = {
3056
+ logger: this.logger,
3057
+ outputDir: Path.join(this.options.outputDir, "/types"),
3058
+ tabSize: 4,
3059
+ withDebugComment: false,
3060
+ commentLinePrefix: "//"
3061
+ };
3062
+ const defaultCSharpOpts = {
3063
+ ...defaultWriterOpts,
3064
+ rootNamespace: "Fhir.Types"
3065
+ };
3066
+ const opts = {
3067
+ ...defaultCSharpOpts,
3068
+ ...Object.fromEntries(Object.entries(userOptions).filter(([_, v]) => v !== void 0))
3069
+ };
3070
+ const generator = new CSharp(opts);
3071
+ this.generators.set("csharp", generator);
3040
3072
  this.logger.debug(`Configured C# generator`);
3041
3073
  return this;
3042
3074
  }
@@ -3087,7 +3119,7 @@ var APIBuilder = class {
3087
3119
  const result = {
3088
3120
  success: false,
3089
3121
  outputDir: this.options.outputDir,
3090
- filesGenerated: [],
3122
+ filesGenerated: {},
3091
3123
  errors: [],
3092
3124
  warnings: [],
3093
3125
  duration: 0
@@ -3179,7 +3211,9 @@ var APIBuilder = class {
3179
3211
  try {
3180
3212
  await generator.generate(tsIndex);
3181
3213
  const fileBuffer = generator.writtenFiles();
3182
- result.filesGenerated.push(...fileBuffer.map((e) => e.absPath));
3214
+ fileBuffer.forEach((buf) => {
3215
+ result.filesGenerated[buf.relPath] = buf.content;
3216
+ });
3183
3217
  this.logger.info(`Generating ${type} finished successfully`);
3184
3218
  } catch (error) {
3185
3219
  result.errors.push(