| 1 | const { spawn } = require("child_process"); |
| 2 | const path = require("path"); |
| 3 | const fs = require("fs"); |
| 4 | const fsPromises = require("fs/promises"); |
| 5 | |
| 6 | const npmInstallCmd = process.env.CI === "true" ? "npm ci" : "npm install"; |
| 7 | |
| 8 | function runCommand(command, cwd, packageName) { |
| 9 | return new Promise((resolve, reject) => { |
| 10 | console.log(`Starting ${packageName}: ${command}`); |
| 11 | |
| 12 | const [cmd, ...args] = command.split(" "); |
| 13 | const child = spawn(cmd, args, { |
| 14 | cwd, |
| 15 | stdio: "pipe", |
| 16 | shell: true, |
| 17 | }); |
| 18 | |
| 19 | let stdout = ""; |
| 20 | let stderr = ""; |
| 21 | |
| 22 | child.stdout.on("data", (data) => { |
| 23 | stdout += data.toString(); |
| 24 | }); |
| 25 | |
| 26 | child.stderr.on("data", (data) => { |
| 27 | stderr += data.toString(); |
| 28 | }); |
| 29 | |
| 30 | child.on("close", (code) => { |
| 31 | if (code === 0) { |
| 32 | console.log(`✅ ${packageName}: ${command} completed successfully`); |
| 33 | resolve({ packageName, command, stdout, stderr }); |
| 34 | } else { |
| 35 | console.error(`❌ ${packageName}: ${command} failed with code ${code}`); |
| 36 | console.error(`stderr: ${stderr}`); |
| 37 | console.error(`stdout: ${stdout}`); |
| 38 | reject( |
| 39 | new Error(`${packageName} failed: ${command} (exit code ${code})`), |
| 40 | ); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | child.on("error", (error) => { |
| 45 | console.error(`❌ ${packageName}: Failed to start ${command}:`, error); |
| 46 | reject(error); |
| 47 | }); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | // Helper function to build a package (install + build) |
| 52 | async function buildPackage(packageName, cleanNodeModules = false) { |
| 53 | const packagePath = path.join(__dirname, "..", "packages", packageName); |
| 54 | |
| 55 | if (!fs.existsSync(packagePath)) { |
| 56 | throw new Error(`Package directory not found: ${packagePath}`); |
| 57 | } |
| 58 | |
| 59 | if (cleanNodeModules) { |
| 60 | const nodeModulesPath = path.join(packagePath, "node_modules"); |
| 61 | if (fs.existsSync(nodeModulesPath)) { |
| 62 | console.log(`🧹 Cleaning node_modules for ${packageName}`); |
| 63 | await fsPromises.rm(nodeModulesPath, { recursive: true, force: true }); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | await runCommand(npmInstallCmd, packagePath, `${packageName} (install)`); |
| 68 | |
| 69 | return runCommand("npm run build", packagePath, `${packageName} (build)`); |
| 70 | } |
| 71 | |
| 72 | async function buildPackagesInParallel(packages, cleanNodeModules = false) { |
| 73 | const buildPromises = packages.map((pkg) => |
| 74 | buildPackage(pkg, cleanNodeModules), |
| 75 | ); |
| 76 | return Promise.all(buildPromises); |
| 77 | } |
| 78 | |
| 79 | async function main() { |
| 80 | try { |
| 81 | console.log("🚀 Starting package builds...\n"); |
| 82 | |
| 83 | // Phase 1: Build foundation packages (no local dependencies) |
| 84 | await buildPackagesInParallel(["config-types", "terminal-security"]); |
| 85 | |
| 86 | // Phase 2: Build packages that depend on config-types |
| 87 | await buildPackagesInParallel(["fetch", "config-yaml", "llm-info"]); |
| 88 | |
| 89 | // Phase 3: Build packages that depend on other local packages |
| 90 | await buildPackagesInParallel(["openai-adapters", "continue-sdk"]); |
| 91 | |
| 92 | console.log("🎉 All packages built successfully!"); |
| 93 | } catch (error) { |
| 94 | console.error("💥 Build failed:", error.message); |
| 95 | process.exit(1); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (require.main === module) { |
| 100 | main(); |
| 101 | } |