-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ts
More file actions
127 lines (107 loc) · 4.09 KB
/
build.ts
File metadata and controls
127 lines (107 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env bun
/**
* Optimized root build script with parallel execution
* Builds packages in dependency order:
* 1. core (required by others)
* 2. cli, mcp-server, vscode-extension (parallel)
*/
interface BuildResult {
package: string;
success: boolean;
duration: number;
}
async function buildPackage(pkg: string): Promise<BuildResult> {
const startTime = performance.now();
console.log(`📦 Building @ecuabyte/cortex-${pkg}...`);
// Special handling for VS Code extension bundling
if (pkg === 'vscode-extension') {
try {
// 1. Compile with tsc for type checking (optional but good)
// 2. Bundle with Bun
const result = await Bun.build({
entrypoints: ['./packages/vscode-extension/src/extension.ts'],
outdir: './packages/vscode-extension/dist',
target: 'node',
external: ['vscode', 'sql.js'], // vscode is provided by host, sql.js needs wasm handling
minify: true,
sourcemap: 'none',
});
if (!result.success) {
console.error(result.logs);
return { package: pkg, success: false, duration: 0 };
}
// Copy sql-wasm.wasm to dist directory for runtime access
const wasmSource = './node_modules/sql.js/dist/sql-wasm.wasm';
const wasmDest = './packages/vscode-extension/dist/sql-wasm.wasm';
try {
const wasmFile = Bun.file(wasmSource);
if (await wasmFile.exists()) {
await Bun.write(wasmDest, wasmFile);
console.log(' ✓ Copied sql-wasm.wasm to dist');
} else {
console.warn(' ⚠ sql-wasm.wasm not found, database may not work');
}
} catch (wasmError) {
console.warn(' ⚠ Failed to copy sql-wasm.wasm:', wasmError);
}
} catch (e) {
console.error(e);
return { package: pkg, success: false, duration: 0 };
}
} else {
// Standard build for other packages
const proc = Bun.spawn(['bun', 'run', 'build'], {
cwd: `./packages/${pkg}`,
stdout: 'inherit',
stderr: 'inherit',
});
const exitCode = await proc.exited;
if (exitCode !== 0) {
const duration = Math.round(performance.now() - startTime);
console.error(`❌ Failed to build ${pkg} (${duration}ms)`);
return { package: pkg, success: false, duration };
}
}
const duration = Math.round(performance.now() - startTime);
console.log(`✅ ${pkg} built in ${duration}ms\n`);
return { package: pkg, success: true, duration };
}
console.log('🏗️ Building Cortex monorepo (optimized)...\n');
const totalStart = performance.now();
try {
// Step 0: Build shared package (required by all others)
console.log('📍 Phase 0: Building shared package...');
const sharedResult = await buildPackage('shared');
if (!sharedResult.success) {
process.exit(1);
}
// Step 1: Build core (required by all others)
console.log('📍 Phase 1: Building core package...');
const coreResult = await buildPackage('core');
if (!coreResult.success) {
process.exit(1);
}
// Step 2: Build dependent packages in parallel
console.log('📍 Phase 2: Building dependent packages in parallel...');
const dependentPackages = ['cli', 'mcp-server', 'vscode-extension'];
const results = await Promise.all(dependentPackages.map((pkg) => buildPackage(pkg)));
// Check if all builds succeeded
const failed = results.filter((r) => !r.success);
if (failed.length > 0) {
console.error(`\n❌ Build failed for: ${failed.map((r) => r.package).join(', ')}`);
process.exit(1);
}
const totalDuration = Math.round(performance.now() - totalStart);
const totalPackageTime = [coreResult, ...results].reduce((sum, r) => sum + r.duration, 0);
const timeSaved = totalPackageTime - totalDuration;
console.log(`\n${'='.repeat(60)}`);
console.log('🎉 All packages built successfully!');
console.log(`⏱️ Total time: ${totalDuration}ms`);
console.log(`💾 Time saved by parallelization: ~${timeSaved}ms`);
console.log('='.repeat(60));
} catch (error) {
console.error('\n❌ Unexpected error during build:', error);
process.exit(1);
}
// Export to make this a module
export {};