|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { spawn, execSync } = require("child_process") |
| 4 | +const path = require("path") |
| 5 | +const fs = require("fs") |
| 6 | + |
| 7 | +// Package configuration - Add new packages here as needed. |
| 8 | +const config = { |
| 9 | + packages: [ |
| 10 | + { |
| 11 | + name: "@roo-code/cloud", |
| 12 | + sourcePath: "../Roo-Code-Cloud/packages/sdk", |
| 13 | + targetPath: "src/node_modules/@roo-code/cloud", |
| 14 | + npmPath: "npm", |
| 15 | + watchCommand: "pnpm build:development:watch", |
| 16 | + }, |
| 17 | + ], |
| 18 | +} |
| 19 | + |
| 20 | +const args = process.argv.slice(2) |
| 21 | +const packageName = args.find((arg) => !arg.startsWith("--")) |
| 22 | +const watch = !args.includes("--no-watch") |
| 23 | +const unlink = args.includes("--unlink") |
| 24 | + |
| 25 | +const packages = packageName ? config.packages.filter((p) => p.name === packageName) : config.packages |
| 26 | + |
| 27 | +if (!packages.length) { |
| 28 | + console.error(`Package '${packageName}' not found`) |
| 29 | + process.exit(1) |
| 30 | +} |
| 31 | + |
| 32 | +packages.forEach(unlink ? unlinkPackage : linkPackage) |
| 33 | + |
| 34 | +// After unlinking, restore npm packages with a single pnpm install. |
| 35 | +if (unlink && packages.length > 0) { |
| 36 | + const srcPath = path.resolve(__dirname, "..", "src") |
| 37 | + console.log("\nRestoring npm packages...") |
| 38 | + |
| 39 | + try { |
| 40 | + execSync("pnpm install", { cwd: srcPath, stdio: "inherit" }) |
| 41 | + console.log("Successfully restored npm packages") |
| 42 | + } catch (error) { |
| 43 | + console.error(`Failed to restore packages: ${error.message}`) |
| 44 | + console.log("You may need to run 'pnpm install' manually in the src directory") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +if (!unlink && watch) { |
| 49 | + const watchers = packages.filter((pkg) => pkg.watchCommand).map(startWatch) |
| 50 | + |
| 51 | + if (watchers.length) { |
| 52 | + process.on("SIGINT", () => { |
| 53 | + console.log("\nStopping...") |
| 54 | + watchers.forEach((w) => w.kill()) |
| 55 | + process.exit(0) |
| 56 | + }) |
| 57 | + console.log("\nWatching for changes. Press Ctrl+C to stop.\n") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function linkPackage(pkg) { |
| 62 | + const sourcePath = path.resolve(__dirname, "..", pkg.sourcePath) |
| 63 | + const targetPath = path.resolve(__dirname, "..", pkg.targetPath) |
| 64 | + |
| 65 | + if (!fs.existsSync(sourcePath)) { |
| 66 | + console.error(`Source not found: ${sourcePath}`) |
| 67 | + process.exit(1) |
| 68 | + } |
| 69 | + |
| 70 | + // Install dependencies if needed. |
| 71 | + if (!fs.existsSync(path.join(sourcePath, "node_modules"))) { |
| 72 | + console.log(`Installing dependencies for ${pkg.name}...`) |
| 73 | + |
| 74 | + try { |
| 75 | + execSync("pnpm install", { cwd: sourcePath, stdio: "inherit" }) |
| 76 | + } catch (e) { |
| 77 | + execSync("pnpm install --no-frozen-lockfile", { cwd: sourcePath, stdio: "inherit" }) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Create symlink. |
| 82 | + fs.rmSync(targetPath, { recursive: true, force: true }) |
| 83 | + fs.mkdirSync(path.dirname(targetPath), { recursive: true }) |
| 84 | + const linkSource = pkg.npmPath ? path.join(sourcePath, pkg.npmPath) : sourcePath |
| 85 | + fs.symlinkSync(linkSource, targetPath, "dir") |
| 86 | + console.log(`Linked ${pkg.name}`) |
| 87 | +} |
| 88 | + |
| 89 | +function unlinkPackage(pkg) { |
| 90 | + const targetPath = path.resolve(__dirname, "..", pkg.targetPath) |
| 91 | + if (fs.existsSync(targetPath)) { |
| 92 | + fs.rmSync(targetPath, { recursive: true, force: true }) |
| 93 | + console.log(`Unlinked ${pkg.name}`) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function startWatch(pkg) { |
| 98 | + console.log(`Watching ${pkg.name}...`) |
| 99 | + const [cmd, ...args] = pkg.watchCommand.split(" ") |
| 100 | + return spawn(cmd, args, { |
| 101 | + cwd: path.resolve(__dirname, "..", pkg.sourcePath), |
| 102 | + stdio: "inherit", |
| 103 | + shell: true, |
| 104 | + }) |
| 105 | +} |
0 commit comments