|
| 1 | +import fs from "node:fs/promises" |
| 2 | +import os from "node:os" |
| 3 | +import path from "node:path" |
| 4 | +import process from "node:process" |
| 5 | +import { spawn } from "node:child_process" |
| 6 | +import { fileURLToPath } from "node:url" |
| 7 | + |
| 8 | +const here = path.dirname(fileURLToPath(import.meta.url)) |
| 9 | +const root = path.resolve(here, "..") |
| 10 | +const localPlanner = `file://${path.join(root, "index.js")}` |
| 11 | +const globalConfigPath = path.join(os.homedir(), ".config", "opencode", "opencode.json") |
| 12 | + |
| 13 | +function usage() { |
| 14 | + console.log(`Usage: node scripts/run-opencode-sandbox.js [--without-plannotator] [opencode args...] |
| 15 | +
|
| 16 | +Examples: |
| 17 | + npm run opencode:no-plannotator |
| 18 | + npm run opencode:no-plannotator -- debug config |
| 19 | + OPENCODE_EXPERIMENTAL_PLAN_MODE=1 OPENCODE_CLIENT=cli npm run opencode:no-plannotator -- debug agent plan`) |
| 20 | +} |
| 21 | + |
| 22 | +function normalizePlugin(entry) { |
| 23 | + return Array.isArray(entry) ? entry[0] : entry |
| 24 | +} |
| 25 | + |
| 26 | +function parseLooseJSON(text) { |
| 27 | + return JSON.parse( |
| 28 | + text |
| 29 | + .replace(/^\uFEFF/, "") |
| 30 | + .replace(/,\s*([}\]])/g, "$1"), |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +function keepWithoutPlannotator(entry) { |
| 35 | + const id = normalizePlugin(entry) |
| 36 | + return id !== "@plannotator/opencode@latest" && id !== "@plannotator/opencode" |
| 37 | +} |
| 38 | + |
| 39 | +const rawArgs = process.argv.slice(2) |
| 40 | +if (rawArgs.includes("-h") || rawArgs.includes("--help")) { |
| 41 | + usage() |
| 42 | + process.exit(0) |
| 43 | +} |
| 44 | + |
| 45 | +const stripPlannotator = rawArgs.includes("--without-plannotator") |
| 46 | +const opencodeArgs = rawArgs.filter((arg) => arg !== "--without-plannotator") |
| 47 | + |
| 48 | +const baseConfig = parseLooseJSON(await fs.readFile(globalConfigPath, "utf8")) |
| 49 | +const plugin = (baseConfig.plugin ?? []).filter((entry) => !stripPlannotator || keepWithoutPlannotator(entry)) |
| 50 | + |
| 51 | +const filtered = plugin.filter((entry) => normalizePlugin(entry) !== localPlanner) |
| 52 | +filtered.push(localPlanner) |
| 53 | + |
| 54 | +const sandboxConfig = { |
| 55 | + ...baseConfig, |
| 56 | + plugin: filtered, |
| 57 | +} |
| 58 | + |
| 59 | +const sandboxRoot = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-planner-")) |
| 60 | +const configDir = path.join(sandboxRoot, ".config", "opencode") |
| 61 | +const dataDir = path.join(sandboxRoot, ".local", "share") |
| 62 | +const stateDir = path.join(sandboxRoot, ".local", "state") |
| 63 | +const cacheDir = path.join(sandboxRoot, ".cache") |
| 64 | + |
| 65 | +await fs.mkdir(configDir, { recursive: true }) |
| 66 | +await fs.mkdir(dataDir, { recursive: true }) |
| 67 | +await fs.mkdir(stateDir, { recursive: true }) |
| 68 | +await fs.mkdir(cacheDir, { recursive: true }) |
| 69 | + |
| 70 | +const sandboxConfigPath = path.join(sandboxRoot, "opencode.sandbox.json") |
| 71 | +await fs.writeFile(sandboxConfigPath, `${JSON.stringify(sandboxConfig, null, 2)}\n`) |
| 72 | + |
| 73 | +console.log(`Sandbox root: ${sandboxRoot}`) |
| 74 | +console.log(`Sandbox config: ${sandboxConfigPath}`) |
| 75 | +console.log("Active plugins:") |
| 76 | +for (const entry of filtered) { |
| 77 | + console.log(`- ${normalizePlugin(entry)}`) |
| 78 | +} |
| 79 | + |
| 80 | +const child = spawn("opencode", opencodeArgs, { |
| 81 | + cwd: root, |
| 82 | + stdio: "inherit", |
| 83 | + env: { |
| 84 | + ...process.env, |
| 85 | + HOME: sandboxRoot, |
| 86 | + XDG_CONFIG_HOME: path.join(sandboxRoot, ".config"), |
| 87 | + XDG_DATA_HOME: dataDir, |
| 88 | + XDG_STATE_HOME: stateDir, |
| 89 | + XDG_CACHE_HOME: cacheDir, |
| 90 | + OPENCODE_CONFIG: sandboxConfigPath, |
| 91 | + }, |
| 92 | +}) |
| 93 | + |
| 94 | +child.on("exit", (code, signal) => { |
| 95 | + if (signal) { |
| 96 | + process.kill(process.pid, signal) |
| 97 | + return |
| 98 | + } |
| 99 | + process.exit(code ?? 0) |
| 100 | +}) |
0 commit comments