support OPENCODE_CONFIG_CONTENT

This commit is contained in:
Dax Raad
2025-09-02 15:15:34 -04:00
parent d6350a7fa6
commit 8952b3d246
3 changed files with 22 additions and 10 deletions

View File

@@ -34,6 +34,11 @@ export namespace Config {
log.debug("loaded custom config", { path: Flag.OPENCODE_CONFIG }) log.debug("loaded custom config", { path: Flag.OPENCODE_CONFIG })
} }
if (Flag.OPENCODE_CONFIG_CONTENT) {
result = mergeDeep(result, JSON.parse(Flag.OPENCODE_CONFIG_CONTENT))
log.debug("loaded custom config from OPENCODE_CONFIG_CONTENT")
}
for (const [key, value] of Object.entries(auth)) { for (const [key, value] of Object.entries(auth)) {
if (value.type === "wellknown") { if (value.type === "wellknown") {
process.env[value.key] = value.token process.env[value.key] = value.token

View File

@@ -2,6 +2,7 @@ export namespace Flag {
export const OPENCODE_AUTO_SHARE = truthy("OPENCODE_AUTO_SHARE") export const OPENCODE_AUTO_SHARE = truthy("OPENCODE_AUTO_SHARE")
export const OPENCODE_DISABLE_WATCHER = truthy("OPENCODE_DISABLE_WATCHER") export const OPENCODE_DISABLE_WATCHER = truthy("OPENCODE_DISABLE_WATCHER")
export const OPENCODE_CONFIG = process.env["OPENCODE_CONFIG"] export const OPENCODE_CONFIG = process.env["OPENCODE_CONFIG"]
export const OPENCODE_CONFIG_CONTENT = process.env["OPENCODE_CONFIG_CONTENT"]
export const OPENCODE_DISABLE_AUTOUPDATE = truthy("OPENCODE_DISABLE_AUTOUPDATE") export const OPENCODE_DISABLE_AUTOUPDATE = truthy("OPENCODE_DISABLE_AUTOUPDATE")
export const OPENCODE_PERMISSION = process.env["OPENCODE_PERMISSION"] export const OPENCODE_PERMISSION = process.env["OPENCODE_PERMISSION"]
export const OPENCODE_DISABLE_DEFAULT_PLUGINS = truthy("OPENCODE_DISABLE_DEFAULT_PLUGINS") export const OPENCODE_DISABLE_DEFAULT_PLUGINS = truthy("OPENCODE_DISABLE_DEFAULT_PLUGINS")

View File

@@ -1,30 +1,36 @@
import { spawn } from "node:child_process" import { spawn } from "node:child_process"
import { Config } from "./gen/types.gen.js"
export type ServerConfig = { export type ServerOptions = {
hostname?: string hostname?: string
port?: number port?: number
signal?: AbortSignal signal?: AbortSignal
timeout?: number timeout?: number
config?: Config
} }
export async function createOpencodeServer(config?: ServerConfig) { export async function createOpencodeServer(options?: ServerOptions) {
config = Object.assign( options = Object.assign(
{ {
hostname: "127.0.0.1", hostname: "127.0.0.1",
port: 4096, port: 4096,
timeout: 5000, timeout: 5000,
}, },
config ?? {}, options ?? {},
) )
const proc = spawn(`opencode`, [`serve`, `--hostname=${config.hostname}`, `--port=${config.port}`], { const proc = spawn(`opencode`, [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`], {
signal: config.signal, signal: options.signal,
env: {
...process.env,
OPENCODE_CONFIG_CONTENT: JSON.stringify(options.config ?? {}),
},
}) })
const url = await new Promise<string>((resolve, reject) => { const url = await new Promise<string>((resolve, reject) => {
const id = setTimeout(() => { const id = setTimeout(() => {
reject(new Error(`Timeout waiting for server to start after ${config.timeout}ms`)) reject(new Error(`Timeout waiting for server to start after ${options.timeout}ms`))
}, config.timeout) }, options.timeout)
let output = "" let output = ""
proc.stdout?.on("data", (chunk) => { proc.stdout?.on("data", (chunk) => {
output += chunk.toString() output += chunk.toString()
@@ -56,8 +62,8 @@ export async function createOpencodeServer(config?: ServerConfig) {
clearTimeout(id) clearTimeout(id)
reject(error) reject(error)
}) })
if (config.signal) { if (options.signal) {
config.signal.addEventListener("abort", () => { options.signal.addEventListener("abort", () => {
clearTimeout(id) clearTimeout(id)
reject(new Error("Aborted")) reject(new Error("Aborted"))
}) })