diff --git a/packages/opencode/script/build-node.ts b/packages/opencode/script/build-node.ts index c004c7096e..6c1c0e03ec 100755 --- a/packages/opencode/script/build-node.ts +++ b/packages/opencode/script/build-node.ts @@ -8,6 +8,44 @@ import { fileURLToPath } from "url" const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) const dir = path.resolve(__dirname, "..") +const root = path.resolve(dir, "../..") + +type Pkg = { + dependencies?: Record + devDependencies?: Record + workspaces?: unknown +} + +type Lock = { + configVersion?: number +} + +function json(file: string) { + return JSON.parse(fs.readFileSync(file, "utf8")) as T +} + +function mod(base: string, name: string) { + return path.join(base, "node_modules", ...name.split("/")) +} + +function linker(): "hoisted" | "isolated" { + const rootPkg = json(path.join(root, "package.json")) + const pkg = json(path.join(dir, "package.json")) + + for (const [name, ver] of Object.entries(pkg.dependencies ?? {})) { + if (ver.startsWith("workspace:")) continue + if (rootPkg.dependencies?.[name] || rootPkg.devDependencies?.[name]) continue + + const inRoot = fs.existsSync(mod(root, name)) + const inDir = fs.existsSync(mod(dir, name)) + + if (inRoot !== inDir) return inRoot ? "hoisted" : "isolated" + } + + const lock = json(path.join(root, "bun.lock")) + if (lock.configVersion === 0) return "hoisted" + return rootPkg.workspaces ? "isolated" : "hoisted" +} process.chdir(dir) @@ -41,7 +79,9 @@ const migrations = await Promise.all( ) console.log(`Loaded ${migrations.length} migrations`) -await $`bun install --os="*" --cpu="*" @lydell/node-pty@1.2.0-beta.10` +const link = linker() + +await $`bun install --linker=${link} --os="*" --cpu="*" @lydell/node-pty@1.2.0-beta.10` await Bun.build({ target: "node",