diff options
| author | Dax Raad <[email protected]> | 2025-09-02 15:15:34 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-09-02 16:38:50 -0400 |
| commit | 8952b3d246a6282e7f2059312d52a69f16432259 (patch) | |
| tree | 80f8325897029e1828521d85a223de6b69f77438 /packages/sdk/js | |
| parent | d6350a7fa61702ac2aa74357e901d59f2c4ee1c7 (diff) | |
| download | opencode-8952b3d246a6282e7f2059312d52a69f16432259.tar.gz opencode-8952b3d246a6282e7f2059312d52a69f16432259.zip | |
support OPENCODE_CONFIG_CONTENT
Diffstat (limited to 'packages/sdk/js')
| -rw-r--r-- | packages/sdk/js/src/server.ts | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/packages/sdk/js/src/server.ts b/packages/sdk/js/src/server.ts index 20217b371..5c8caa43f 100644 --- a/packages/sdk/js/src/server.ts +++ b/packages/sdk/js/src/server.ts @@ -1,30 +1,36 @@ import { spawn } from "node:child_process" +import { Config } from "./gen/types.gen.js" -export type ServerConfig = { +export type ServerOptions = { hostname?: string port?: number signal?: AbortSignal timeout?: number + config?: Config } -export async function createOpencodeServer(config?: ServerConfig) { - config = Object.assign( +export async function createOpencodeServer(options?: ServerOptions) { + options = Object.assign( { hostname: "127.0.0.1", port: 4096, timeout: 5000, }, - config ?? {}, + options ?? {}, ) - const proc = spawn(`opencode`, [`serve`, `--hostname=${config.hostname}`, `--port=${config.port}`], { - signal: config.signal, + const proc = spawn(`opencode`, [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`], { + signal: options.signal, + env: { + ...process.env, + OPENCODE_CONFIG_CONTENT: JSON.stringify(options.config ?? {}), + }, }) const url = await new Promise<string>((resolve, reject) => { const id = setTimeout(() => { - reject(new Error(`Timeout waiting for server to start after ${config.timeout}ms`)) - }, config.timeout) + reject(new Error(`Timeout waiting for server to start after ${options.timeout}ms`)) + }, options.timeout) let output = "" proc.stdout?.on("data", (chunk) => { output += chunk.toString() @@ -56,8 +62,8 @@ export async function createOpencodeServer(config?: ServerConfig) { clearTimeout(id) reject(error) }) - if (config.signal) { - config.signal.addEventListener("abort", () => { + if (options.signal) { + options.signal.addEventListener("abort", () => { clearTimeout(id) reject(new Error("Aborted")) }) |
