babelfhir-ts 1.2.2 → 1.2.3

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.
@@ -1,4 +1,4 @@
1
- import { generateIntoPackage, resetFetchFailureTracking, getFetchFailureWarning } from "../generator/index.js";
1
+ import { generateIntoPackageDirect, resetFetchFailureTracking, getFetchFailureWarning } from "../generator/index.js";
2
2
  import { getCacheConfig, clearAllCaches } from "../generator/core/cacheConfig.js";
3
3
  import { buildQualityReport, formatReportSummary, resetDiagnostics } from "../generator/core/sdDiagnostics.js";
4
4
  import fs from 'fs';
@@ -94,10 +94,10 @@ function npmInstall(packagePath) {
94
94
  }
95
95
  export async function handleInstallCommand(opts) {
96
96
  const { packageToInstall, registry, generationFlags, downloadPackage } = opts;
97
- // Default to cleaning cache for install command
98
- if (!generationFlags.noCache) {
99
- generationFlags.noCache = true;
100
- }
97
+ // Keep cache by default dependency packages are large (hl7.fhir.r4.core ~50 MB)
98
+ // and re-downloading them every run is the single biggest bottleneck.
99
+ // Users can opt in to cache cleanup with --no-cache.
100
+ let generationCleanup;
101
101
  try {
102
102
  let downloadedPath;
103
103
  if (fs.existsSync(packageToInstall) && (packageToInstall.endsWith('.tgz') || packageToInstall.endsWith('.zip'))) {
@@ -108,11 +108,11 @@ export async function handleInstallCommand(opts) {
108
108
  downloadedPath = await downloadPackage(packageToInstall, registry);
109
109
  }
110
110
  console.log(`Processing package...`);
111
- const baseName = path.basename(downloadedPath, path.extname(downloadedPath));
112
- const outputArchive = path.join(process.cwd(), `${baseName}.with-generated.tgz`);
113
111
  resetFetchFailureTracking();
114
112
  resetDiagnostics();
115
- await generateIntoPackage(downloadedPath, outputArchive, generationFlags);
113
+ // Generate directly into extracted package — no intermediate archive round-trip
114
+ const { generatedDir, cleanup } = await generateIntoPackageDirect(downloadedPath, generationFlags);
115
+ generationCleanup = cleanup;
116
116
  // Clean up temp directory only if we downloaded from registry (not for local files)
117
117
  if (!fs.existsSync(packageToInstall) || !(packageToInstall.endsWith('.tgz') || packageToInstall.endsWith('.zip'))) {
118
118
  const tempDir = path.join(process.cwd(), '.temp-download');
@@ -120,29 +120,11 @@ export async function handleInstallCommand(opts) {
120
120
  fs.rmSync(tempDir, { recursive: true, force: true });
121
121
  }
122
122
  }
123
- console.log(`✓ Package processed successfully: ${path.basename(outputArchive)}`);
124
- // Extract the archive to access generated folder
125
- console.log(`Extracting generated code...`);
126
- const { extract } = await import('tar');
127
- const extractDir = path.join(process.cwd(), '.temp-install');
128
- if (!fs.existsSync(extractDir)) {
129
- fs.mkdirSync(extractDir, { recursive: true });
130
- }
131
- await extract({ cwd: extractDir, file: outputArchive });
132
- // Look for generated folder - could be at root or under 'package' subfolder
133
- let generatedDir = path.join(extractDir, 'generated');
134
- if (!fs.existsSync(generatedDir)) {
135
- generatedDir = path.join(extractDir, 'package', 'generated');
136
- }
137
- if (!fs.existsSync(generatedDir)) {
138
- throw new Error('Generated folder not found in package archive');
139
- }
140
- // Verify package.json exists
123
+ // Verify package.json exists in generated folder
141
124
  const pkgJsonPath = path.join(generatedDir, 'package.json');
142
125
  if (!fs.existsSync(pkgJsonPath)) {
143
126
  throw new Error(`package.json not found in generated folder: ${generatedDir}`);
144
127
  }
145
- // Create a tarball from the generated folder to avoid symlink issues on Windows
146
128
  const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
147
129
  const packageName = pkgJson.name;
148
130
  console.log(`Creating package tarball for ${packageName}...`);
@@ -163,15 +145,9 @@ export async function handleInstallCommand(opts) {
163
145
  }, ['.']);
164
146
  console.log(`Installing ${packageName}...`);
165
147
  await npmInstall(tarballPath);
166
- // Clean up temporary files (but keep the tarball that npm references in package.json under ./lib)
167
- console.log(`Cleaning up temporary files...`);
168
- if (fs.existsSync(extractDir)) {
169
- fs.rmSync(extractDir, { recursive: true, force: true });
170
- }
171
- if (fs.existsSync(outputArchive)) {
172
- fs.unlinkSync(outputArchive);
173
- console.log(`✓ Removed ${path.basename(outputArchive)}`);
174
- }
148
+ // Clean up extracted package tree (but keep the tarball under ./lib)
149
+ generationCleanup();
150
+ generationCleanup = undefined;
175
151
  console.log(`✓ Package tarball saved to ./lib: ${path.basename(tarballPath)}`);
176
152
  showGenerationWarnings();
177
153
  const report = buildQualityReport();
@@ -184,20 +160,12 @@ export async function handleInstallCommand(opts) {
184
160
  console.log(`\n✓ Package ${packageToInstall} installed and ready to use!`);
185
161
  }
186
162
  catch (error) {
187
- // Clean up temporary files on error
188
- for (const dir of ['.temp-download', '.temp-install']) {
189
- const fullPath = path.join(process.cwd(), dir);
190
- if (fs.existsSync(fullPath)) {
191
- fs.rmSync(fullPath, { recursive: true, force: true });
192
- }
193
- }
194
- // Clean up any leftover archive
195
- const baseName = packageToInstall.includes('@')
196
- ? packageToInstall.replace('@', '-').replace(/\//g, '-')
197
- : path.basename(packageToInstall, path.extname(packageToInstall));
198
- const outputArchive = path.join(process.cwd(), `${baseName}.with-generated.tgz`);
199
- if (fs.existsSync(outputArchive)) {
200
- fs.unlinkSync(outputArchive);
163
+ // Clean up extracted package on error
164
+ generationCleanup?.();
165
+ // Clean up temp download directory
166
+ const tempDir = path.join(process.cwd(), '.temp-download');
167
+ if (fs.existsSync(tempDir)) {
168
+ fs.rmSync(tempDir, { recursive: true, force: true });
201
169
  }
202
170
  if (generationFlags.noCache) {
203
171
  cleanupCache();
@@ -13,7 +13,7 @@
13
13
  import fs from 'fs';
14
14
  import path from 'path';
15
15
  import { getFhirPackagesCacheDir, ensureCacheDir } from '../core/cacheConfig.js';
16
- import { fetchArrayBuffer } from '../core/fetchUtils.js';
16
+ import { downloadFile } from '../core/utils.js';
17
17
  import { logger } from '../../logger.js';
18
18
  const log = logger.withTag('core-resolver');
19
19
  /**
@@ -58,7 +58,7 @@ export async function ensureCorePackage(corePackageSpec) {
58
58
  return candidate;
59
59
  }
60
60
  }
61
- // Not cached — download from registry
61
+ // Not cached — download from registry (streaming to avoid buffering large packages in RAM)
62
62
  ensureCacheDir(configuredCacheDir);
63
63
  const packageDir = path.join(configuredCacheDir, dirName);
64
64
  let downloaded = false;
@@ -67,8 +67,7 @@ export async function ensureCorePackage(corePackageSpec) {
67
67
  const url = `${registry}/${packageName}/${version}`;
68
68
  try {
69
69
  log.info(`Downloading core package ${corePackageSpec} from ${registry}…`);
70
- const response = await fetchArrayBuffer(url);
71
- fs.writeFileSync(tgzPath, Buffer.from(response.data));
70
+ await downloadFile(url, tgzPath);
72
71
  downloaded = true;
73
72
  break;
74
73
  }
@@ -14,7 +14,7 @@
14
14
  import fs from 'fs';
15
15
  import path from 'path';
16
16
  import { getFhirPackagesCacheDir, ensureCacheDir } from '../core/cacheConfig.js';
17
- import { fetchArrayBuffer } from '../core/fetchUtils.js';
17
+ import { downloadFile } from '../core/utils.js';
18
18
  import { logger } from '../../logger.js';
19
19
 
20
20
  const log = logger.withTag('core-resolver');
@@ -88,7 +88,7 @@ export async function ensureCorePackage(corePackageSpec: string): Promise<string
88
88
  }
89
89
  }
90
90
 
91
- // Not cached — download from registry
91
+ // Not cached — download from registry (streaming to avoid buffering large packages in RAM)
92
92
  ensureCacheDir(configuredCacheDir);
93
93
  const packageDir = path.join(configuredCacheDir, dirName);
94
94
 
@@ -98,8 +98,7 @@ export async function ensureCorePackage(corePackageSpec: string): Promise<string
98
98
  const url = `${registry}/${packageName}/${version}`;
99
99
  try {
100
100
  log.info(`Downloading core package ${corePackageSpec} from ${registry}…`);
101
- const response = await fetchArrayBuffer(url);
102
- fs.writeFileSync(tgzPath, Buffer.from(response.data));
101
+ await downloadFile(url, tgzPath);
103
102
  downloaded = true;
104
103
  break;
105
104
  } catch (err) {