refactor(cli-run): remove redundant opencode bin path shim
This commit is contained in:
parent
101dadbce2
commit
ada8c127aa
@ -1,69 +0,0 @@
|
|||||||
/// <reference types="bun-types" />
|
|
||||||
|
|
||||||
import { describe, expect, it } from "bun:test"
|
|
||||||
import { prependResolvedOpencodeBinToPath } from "./opencode-bin-path"
|
|
||||||
|
|
||||||
describe("prependResolvedOpencodeBinToPath", () => {
|
|
||||||
it("prepends resolved opencode-ai bin path to PATH", () => {
|
|
||||||
//#given
|
|
||||||
const env: Record<string, string | undefined> = {
|
|
||||||
PATH: "/Users/yeongyu/node_modules/.bin:/usr/bin",
|
|
||||||
}
|
|
||||||
const resolver = () => "/tmp/bunx-123/node_modules/opencode-ai/bin/opencode"
|
|
||||||
const pathExists = () => true
|
|
||||||
|
|
||||||
//#when
|
|
||||||
prependResolvedOpencodeBinToPath(env, resolver, pathExists)
|
|
||||||
|
|
||||||
//#then
|
|
||||||
expect(env.PATH).toBe(
|
|
||||||
"/tmp/bunx-123/node_modules/opencode-ai/bin:/Users/yeongyu/node_modules/.bin:/usr/bin",
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("does not duplicate an existing opencode-ai bin path", () => {
|
|
||||||
//#given
|
|
||||||
const env: Record<string, string | undefined> = {
|
|
||||||
PATH: "/tmp/bunx-123/node_modules/opencode-ai/bin:/usr/bin",
|
|
||||||
}
|
|
||||||
const resolver = () => "/tmp/bunx-123/node_modules/opencode-ai/bin/opencode"
|
|
||||||
const pathExists = () => true
|
|
||||||
|
|
||||||
//#when
|
|
||||||
prependResolvedOpencodeBinToPath(env, resolver, pathExists)
|
|
||||||
|
|
||||||
//#then
|
|
||||||
expect(env.PATH).toBe("/tmp/bunx-123/node_modules/opencode-ai/bin:/usr/bin")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("keeps PATH unchanged when opencode-ai cannot be resolved", () => {
|
|
||||||
//#given
|
|
||||||
const env: Record<string, string | undefined> = {
|
|
||||||
PATH: "/Users/yeongyu/node_modules/.bin:/usr/bin",
|
|
||||||
}
|
|
||||||
const resolver = () => {
|
|
||||||
throw new Error("module not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
//#when
|
|
||||||
prependResolvedOpencodeBinToPath(env, resolver)
|
|
||||||
|
|
||||||
//#then
|
|
||||||
expect(env.PATH).toBe("/Users/yeongyu/node_modules/.bin:/usr/bin")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("keeps PATH unchanged when resolved binary path does not exist", () => {
|
|
||||||
//#given
|
|
||||||
const env: Record<string, string | undefined> = {
|
|
||||||
PATH: "/Users/yeongyu/node_modules/.bin:/usr/bin",
|
|
||||||
}
|
|
||||||
const resolver = () => "/Users/yeongyu/node_modules/opencode-ai/bin/opencode"
|
|
||||||
const pathExists = () => false
|
|
||||||
|
|
||||||
//#when
|
|
||||||
prependResolvedOpencodeBinToPath(env, resolver, pathExists)
|
|
||||||
|
|
||||||
//#then
|
|
||||||
expect(env.PATH).toBe("/Users/yeongyu/node_modules/.bin:/usr/bin")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@ -1,40 +0,0 @@
|
|||||||
import { delimiter, dirname } from "node:path"
|
|
||||||
import { createRequire } from "node:module"
|
|
||||||
import { existsSync } from "node:fs"
|
|
||||||
|
|
||||||
type EnvLike = Record<string, string | undefined>
|
|
||||||
|
|
||||||
const resolveFromCurrentModule = createRequire(import.meta.url).resolve
|
|
||||||
|
|
||||||
export function prependResolvedOpencodeBinToPath(
|
|
||||||
env: EnvLike = process.env,
|
|
||||||
resolve: (id: string) => string = resolveFromCurrentModule,
|
|
||||||
pathExists: (path: string) => boolean = existsSync,
|
|
||||||
): void {
|
|
||||||
let resolvedPath: string
|
|
||||||
try {
|
|
||||||
resolvedPath = resolve("opencode-ai/bin/opencode")
|
|
||||||
} catch {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!pathExists(resolvedPath)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const opencodeBinDir = dirname(resolvedPath)
|
|
||||||
if (!pathExists(opencodeBinDir)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentPath = env.PATH ?? ""
|
|
||||||
const pathSegments = currentPath ? currentPath.split(delimiter) : []
|
|
||||||
|
|
||||||
if (pathSegments.includes(opencodeBinDir)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
env.PATH = currentPath
|
|
||||||
? `${opencodeBinDir}${delimiter}${currentPath}`
|
|
||||||
: opencodeBinDir
|
|
||||||
}
|
|
||||||
@ -3,7 +3,6 @@ import pc from "picocolors"
|
|||||||
import type { ServerConnection } from "./types"
|
import type { ServerConnection } from "./types"
|
||||||
import { getAvailableServerPort, isPortAvailable, DEFAULT_SERVER_PORT } from "../../shared/port-utils"
|
import { getAvailableServerPort, isPortAvailable, DEFAULT_SERVER_PORT } from "../../shared/port-utils"
|
||||||
import { withWorkingOpencodePath } from "./opencode-binary-resolver"
|
import { withWorkingOpencodePath } from "./opencode-binary-resolver"
|
||||||
import { prependResolvedOpencodeBinToPath } from "./opencode-bin-path"
|
|
||||||
|
|
||||||
function isPortStartFailure(error: unknown, port: number): boolean {
|
function isPortStartFailure(error: unknown, port: number): boolean {
|
||||||
if (!(error instanceof Error)) {
|
if (!(error instanceof Error)) {
|
||||||
@ -28,8 +27,6 @@ export async function createServerConnection(options: {
|
|||||||
attach?: string
|
attach?: string
|
||||||
signal: AbortSignal
|
signal: AbortSignal
|
||||||
}): Promise<ServerConnection> {
|
}): Promise<ServerConnection> {
|
||||||
prependResolvedOpencodeBinToPath()
|
|
||||||
|
|
||||||
const { port, attach, signal } = options
|
const { port, attach, signal } = options
|
||||||
|
|
||||||
if (attach !== undefined) {
|
if (attach !== undefined) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user