@atomic-ehr/codegen 0.0.2-canary.20251121161730.cea2c3d → 0.0.2-canary.20251121164115.c134ba7

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
@@ -44,48 +44,71 @@ var typeSchemaInfo = (schema) => {
44
44
  var FileSystemWriter = class {
45
45
  opts;
46
46
  currentDir;
47
- currentFileDescriptor;
48
- writtenFilesSet = /* @__PURE__ */ new Set();
47
+ currentFile;
48
+ writtenFilesBuffer = {};
49
49
  constructor(opts) {
50
50
  this.opts = opts;
51
- this.currentDir = opts.outputDir;
51
+ }
52
+ setOutputDir(path) {
53
+ if (this.currentDir) throw new Error("Can't change output dir while writing");
54
+ this.opts.outputDir = path;
52
55
  }
53
56
  logger() {
54
57
  return this.opts.logger;
55
58
  }
59
+ onDiskMkDir(path) {
60
+ if (this.opts.inMemoryOnly) return;
61
+ if (!fs.existsSync(path)) {
62
+ fs.mkdirSync(path, { recursive: true });
63
+ }
64
+ }
65
+ onDiskOpenFile(relPath) {
66
+ if (this.opts.inMemoryOnly) return -1;
67
+ return fs.openSync(relPath, "w");
68
+ }
69
+ onDiskCloseFile(descriptor) {
70
+ if (this.opts.inMemoryOnly) return;
71
+ fs.fsyncSync(descriptor);
72
+ fs.closeSync(descriptor);
73
+ }
74
+ onDiskWrite(descriptor, token) {
75
+ if (this.opts.inMemoryOnly) return;
76
+ fs.writeSync(descriptor, token);
77
+ }
56
78
  cd(path, gen) {
57
79
  const prev = this.currentDir;
58
- this.currentDir = path.startsWith("/") ? Path4.join(this.opts.outputDir, path) : Path4.join(this.currentDir, path);
59
- if (!fs.existsSync(this.currentDir)) {
60
- fs.mkdirSync(this.currentDir, { recursive: true });
61
- }
80
+ this.currentDir = path.startsWith("/") ? Path4.join(this.opts.outputDir, path) : Path4.join(this.currentDir ?? this.opts.outputDir, path);
81
+ this.onDiskMkDir(this.currentDir);
62
82
  this.logger()?.debug(`cd '${this.currentDir}'`);
63
83
  gen();
64
84
  this.currentDir = prev;
65
85
  }
66
86
  cat(fn, gen) {
67
- if (this.currentFileDescriptor) throw new Error("Can't open file in file");
87
+ if (this.currentFile) throw new Error("Can't open file when another file is open");
68
88
  if (fn.includes("/")) throw new Error(`Change file path separatly: ${fn}`);
69
- const fullFn = `${this.currentDir}/${fn}`;
89
+ const relPath = Path4.normalize(`${this.currentDir}/${fn}`);
70
90
  try {
71
- this.currentFileDescriptor = fs.openSync(fullFn, "w");
72
- this.writtenFilesSet.add(fn);
73
- this.logger()?.debug(`cat > '${fullFn}'`);
91
+ const descriptor = this.onDiskOpenFile(relPath);
92
+ this.logger()?.debug(`cat > '${relPath}'`);
93
+ this.currentFile = { descriptor, relPath };
94
+ this.writtenFilesBuffer[this.currentFile.relPath] = { relPath, absPath: Path4.resolve(relPath), tokens: [] };
74
95
  gen();
75
96
  } finally {
76
- if (this.currentFileDescriptor) {
77
- fs.fsyncSync(this.currentFileDescriptor);
78
- fs.closeSync(this.currentFileDescriptor);
79
- }
80
- this.currentFileDescriptor = void 0;
97
+ if (this.currentFile) this.onDiskCloseFile(this.currentFile.descriptor);
98
+ this.currentFile = void 0;
81
99
  }
82
100
  }
83
101
  write(str) {
84
- if (!this.currentFileDescriptor) throw new Error("No file opened");
85
- fs.writeSync(this.currentFileDescriptor, str);
102
+ if (!this.currentFile) throw new Error("No file opened");
103
+ this.onDiskWrite(this.currentFile.descriptor, str);
104
+ const buf = this.writtenFilesBuffer[this.currentFile.relPath];
105
+ if (!buf) throw new Error("No buffer found");
106
+ buf.tokens.push(str);
86
107
  }
87
108
  writtenFiles() {
88
- return Array.from(this.writtenFilesSet);
109
+ return Object.values(this.writtenFilesBuffer).map(({ relPath, absPath, tokens }) => {
110
+ return { relPath, absPath, content: tokens.join() };
111
+ }).sort((a, b) => a.relPath.localeCompare(b.relPath));
89
112
  }
90
113
  };
91
114
  var Writer = class extends FileSystemWriter {
@@ -1357,7 +1380,7 @@ var mkTypeSchemaIndex = (schemas, logger) => {
1357
1380
  append(schema);
1358
1381
  }
1359
1382
  const relations = resourceRelatives(schemas);
1360
- const resolve2 = (id) => index[id.url]?.[id.package];
1383
+ const resolve3 = (id) => index[id.url]?.[id.package];
1361
1384
  const resolveByUrl = (pkgName, url) => index[url]?.[pkgName];
1362
1385
  const resourceChildren = (id) => {
1363
1386
  return relations.filter((relative) => relative.parent.name === id.name).map((relative) => relative.child);
@@ -1369,7 +1392,7 @@ var mkTypeSchemaIndex = (schemas, logger) => {
1369
1392
  res.push(cur);
1370
1393
  const base = cur.base;
1371
1394
  if (base === void 0) break;
1372
- const resolved = resolve2(base);
1395
+ const resolved = resolve3(base);
1373
1396
  if (!resolved) {
1374
1397
  logger?.warn(
1375
1398
  `Failed to resolve base type: ${res.map((e) => `${e.identifier.url} (${e.identifier.kind})`).join(", ")}`
@@ -1395,7 +1418,7 @@ var mkTypeSchemaIndex = (schemas, logger) => {
1395
1418
  return nonConstraintSchema;
1396
1419
  };
1397
1420
  const findLastSpecializationByIdentifier = (id) => {
1398
- const schema = resolve2(id);
1421
+ const schema = resolve3(id);
1399
1422
  if (!schema) return id;
1400
1423
  return findLastSpecialization(schema).identifier;
1401
1424
  };
@@ -1467,7 +1490,7 @@ var mkTypeSchemaIndex = (schemas, logger) => {
1467
1490
  collectResources: () => schemas.filter(isResourceTypeSchema),
1468
1491
  collectLogicalModels: () => schemas.filter(isLogicalTypeSchema),
1469
1492
  collectProfiles: () => schemas.filter(isProfileTypeSchema),
1470
- resolve: resolve2,
1493
+ resolve: resolve3,
1471
1494
  resolveByUrl,
1472
1495
  resourceChildren,
1473
1496
  tryHierarchy,
@@ -2051,25 +2074,6 @@ var TypeScript = class extends Writer {
2051
2074
  };
2052
2075
 
2053
2076
  // src/api/builder.ts
2054
- var writerToGenerator = (writerGen) => {
2055
- const getGeneratedFiles = () => {
2056
- return writerGen.writtenFiles().map((fn) => {
2057
- return {
2058
- path: Path4.normalize(Path4.join(writerGen.opts.outputDir, fn)),
2059
- filename: fn.replace(/^.*[\\/]/, ""),
2060
- timestamp: /* @__PURE__ */ new Date()
2061
- };
2062
- });
2063
- };
2064
- return {
2065
- generate: async ({ index: tsIndex }) => {
2066
- await writerGen.generate(tsIndex);
2067
- return getGeneratedFiles();
2068
- },
2069
- setOutputDir: (outputDir) => writerGen.opts.outputDir = outputDir,
2070
- build: async (_input) => getGeneratedFiles()
2071
- };
2072
- };
2073
2077
  var normalizeFileName = (str) => {
2074
2078
  const res = str.replace(/[^a-zA-Z0-9\-_.@#()]/g, "");
2075
2079
  if (res.length === 0) return "unknown";
@@ -2214,25 +2218,23 @@ var APIBuilder = class {
2214
2218
  ...defaultTsOpts,
2215
2219
  ...Object.fromEntries(Object.entries(userOpts).filter(([_, v]) => v !== void 0))
2216
2220
  };
2217
- const generator = writerToGenerator(new TypeScript(opts));
2221
+ const generator = new TypeScript(opts);
2218
2222
  this.generators.set("typescript", generator);
2219
2223
  this.logger.debug(`Configured TypeScript generator (${JSON.stringify(opts, void 0, 2)})`);
2220
2224
  return this;
2221
2225
  }
2222
2226
  csharp(namespace, staticSourceDir) {
2223
- const generator = writerToGenerator(
2224
- new CSharp({
2225
- outputDir: Path4.join(this.options.outputDir, "/types"),
2226
- staticSourceDir: staticSourceDir ?? void 0,
2227
- targetNamespace: namespace,
2228
- logger: new CodegenLogger({
2229
- prefix: "C#",
2230
- timestamp: true,
2231
- verbose: true,
2232
- suppressLoggingLevel: []
2233
- })
2227
+ const generator = new CSharp({
2228
+ outputDir: Path4.join(this.options.outputDir, "/types"),
2229
+ staticSourceDir: staticSourceDir ?? void 0,
2230
+ targetNamespace: namespace,
2231
+ logger: new CodegenLogger({
2232
+ prefix: "C#",
2233
+ timestamp: true,
2234
+ verbose: true,
2235
+ suppressLoggingLevel: []
2234
2236
  })
2235
- );
2237
+ });
2236
2238
  this.generators.set("C#", generator);
2237
2239
  this.logger.debug(`Configured C# generator`);
2238
2240
  return this;
@@ -2251,9 +2253,7 @@ var APIBuilder = class {
2251
2253
  this.logger.debug(`Setting output directory: ${directory}`);
2252
2254
  this.options.outputDir = directory;
2253
2255
  for (const generator of this.generators.values()) {
2254
- if (generator.setOutputDir) {
2255
- generator.setOutputDir(directory);
2256
- }
2256
+ generator.setOutputDir(directory);
2257
2257
  }
2258
2258
  return this;
2259
2259
  }
@@ -2312,10 +2312,7 @@ var APIBuilder = class {
2312
2312
  if (this.options.treeShake) tsIndex = treeShake(tsIndex, this.options.treeShake, this.logger);
2313
2313
  if (this.options.exportTypeTree) await tsIndex.exportTree(this.options.exportTypeTree);
2314
2314
  this.logger.debug(`Executing ${this.generators.size} generators`);
2315
- await this.executeGenerators(result, {
2316
- schemas: typeSchemas,
2317
- index: tsIndex
2318
- });
2315
+ await this.executeGenerators(result, tsIndex);
2319
2316
  this.logger.info("Generation completed successfully");
2320
2317
  result.success = result.errors.length === 0;
2321
2318
  this.logger.debug(`Generation completed: ${result.filesGenerated.length} files`);
@@ -2330,18 +2327,6 @@ var APIBuilder = class {
2330
2327
  duration: performance.now() - startTime
2331
2328
  };
2332
2329
  }
2333
- /**
2334
- * Generate and return the results without writing to files
2335
- */
2336
- async build() {
2337
- const results = {};
2338
- for (const [type, generator] of this.generators.entries()) {
2339
- if (generator.build) {
2340
- results[type] = await generator.build(this.schemas);
2341
- }
2342
- }
2343
- return results;
2344
- }
2345
2330
  /**
2346
2331
  * Clear all configuration and start fresh
2347
2332
  */
@@ -2363,12 +2348,13 @@ var APIBuilder = class {
2363
2348
  getGenerators() {
2364
2349
  return Array.from(this.generators.keys());
2365
2350
  }
2366
- async executeGenerators(result, input) {
2351
+ async executeGenerators(result, tsIndex) {
2367
2352
  for (const [type, generator] of this.generators.entries()) {
2368
2353
  this.logger.info(`Generating ${type}...`);
2369
2354
  try {
2370
- const files = await generator.generate(input);
2371
- result.filesGenerated.push(...files.map((f) => f.path || f.filename));
2355
+ await generator.generate(tsIndex);
2356
+ const fileBuffer = generator.writtenFiles();
2357
+ result.filesGenerated.push(...fileBuffer.map((e) => e.absPath));
2372
2358
  this.logger.info(`Generating ${type} finished successfully`);
2373
2359
  } catch (error) {
2374
2360
  result.errors.push(