mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(ocm-cli): replace the ocm process with opencode when warping
This commit is contained in:
1 parent
8b428837c3
commit
421387793b
2 files changed
+47
-7
No files matched your search
+15
-1
@@ -23,6 +23,8 @@ export type WarpSpawn = (
|
||||
options: { stdio: 'inherit'; env: NodeJS.ProcessEnv },
|
||||
) => unknown
|
||||
|
||||
export type WarpExec = (binary: string, args: string[], env: NodeJS.ProcessEnv) => void
|
||||
|
||||
const ATTACH_ENV_KEYS = ['OPENCODE_PASSWORD', REMOTE_MANAGER_URL_ENV, REMOTE_REPO_NAME_ENV, REMOTE_REPO_ID_ENV]
|
||||
|
||||
let pending: PendingWarp | undefined
|
||||
@@ -54,13 +56,25 @@ export function buildLocalInvocation(env: NodeJS.ProcessEnv = process.env): Atta
|
||||
}
|
||||
}
|
||||
|
||||
export function runPendingWarp(spawn: WarpSpawn = spawnSync): void {
|
||||
export function runPendingWarp(spawn: WarpSpawn = spawnSync, exec: WarpExec | undefined = defaultExec): void {
|
||||
const warp = takePendingWarp()
|
||||
if (!warp) return
|
||||
try {
|
||||
const { args, env } = warp.kind === 'attach' ? buildAttachInvocation(warp.target) : buildLocalInvocation()
|
||||
try {
|
||||
exec?.('opencode', ['opencode', ...args], env)
|
||||
} catch {
|
||||
void 0
|
||||
}
|
||||
spawn('opencode', args, { stdio: 'inherit', env })
|
||||
} catch {
|
||||
void 0
|
||||
}
|
||||
}
|
||||
|
||||
function defaultExec(command: string, args: string[], env: NodeJS.ProcessEnv): void {
|
||||
if (typeof Bun === 'undefined') return
|
||||
const binary = Bun.which(command)
|
||||
if (!binary || typeof process.execve !== 'function') return
|
||||
process.execve(binary, args, env)
|
||||
}
|
||||
@@ -78,10 +78,14 @@ describe('runPendingWarp', () => {
|
||||
takePendingWarp()
|
||||
})
|
||||
|
||||
const noExec = () => undefined
|
||||
|
||||
it('is a no-op when nothing is pending', () => {
|
||||
const spawn = vi.fn()
|
||||
runPendingWarp(spawn)
|
||||
const exec = vi.fn(noExec)
|
||||
runPendingWarp(spawn, exec)
|
||||
expect(spawn).not.toHaveBeenCalled()
|
||||
expect(exec).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('calls spawn once with the correct args and env', () => {
|
||||
@@ -89,7 +93,7 @@ describe('runPendingWarp', () => {
|
||||
setPendingWarp(sampleWarp)
|
||||
const invocation = buildAttachInvocation(sampleTarget)
|
||||
|
||||
runPendingWarp(spawn)
|
||||
runPendingWarp(spawn, noExec)
|
||||
|
||||
expect(spawn).toHaveBeenCalledOnce()
|
||||
expect(spawn).toHaveBeenCalledWith(
|
||||
@@ -106,7 +110,7 @@ describe('runPendingWarp', () => {
|
||||
const spawn = vi.fn()
|
||||
setPendingWarp({ kind: 'local' })
|
||||
|
||||
runPendingWarp(spawn)
|
||||
runPendingWarp(spawn, noExec)
|
||||
|
||||
expect(spawn).toHaveBeenCalledWith('opencode', [], { stdio: 'inherit', env: buildLocalInvocation().env })
|
||||
})
|
||||
@@ -115,8 +119,8 @@ describe('runPendingWarp', () => {
|
||||
const spawn = vi.fn()
|
||||
setPendingWarp(sampleWarp)
|
||||
|
||||
runPendingWarp(spawn)
|
||||
runPendingWarp(spawn)
|
||||
runPendingWarp(spawn, noExec)
|
||||
runPendingWarp(spawn, noExec)
|
||||
|
||||
expect(spawn).toHaveBeenCalledOnce()
|
||||
})
|
||||
@@ -125,7 +129,29 @@ describe('runPendingWarp', () => {
|
||||
const spawn = vi.fn(() => { throw new Error('spawn failed') })
|
||||
setPendingWarp(sampleWarp)
|
||||
|
||||
expect(() => runPendingWarp(spawn)).not.toThrow()
|
||||
expect(() => runPendingWarp(spawn, noExec)).not.toThrow()
|
||||
expect(spawn).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('tries to exec opencode with the attach invocation before spawning', () => {
|
||||
const spawn = vi.fn()
|
||||
const exec = vi.fn(noExec)
|
||||
setPendingWarp(sampleWarp)
|
||||
const invocation = buildAttachInvocation(sampleTarget)
|
||||
|
||||
runPendingWarp(spawn, exec)
|
||||
|
||||
expect(exec).toHaveBeenCalledOnce()
|
||||
expect(exec).toHaveBeenCalledWith('opencode', ['opencode', ...invocation.args], invocation.env)
|
||||
})
|
||||
|
||||
it('falls back to spawn when exec throws', () => {
|
||||
const spawn = vi.fn()
|
||||
const exec = vi.fn(() => { throw new Error('exec failed') })
|
||||
setPendingWarp(sampleWarp)
|
||||
const invocation = buildAttachInvocation(sampleTarget)
|
||||
|
||||
expect(() => runPendingWarp(spawn, exec)).not.toThrow()
|
||||
expect(spawn).toHaveBeenCalledWith('opencode', invocation.args, { stdio: 'inherit', env: invocation.env })
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user