fix(cli-run): skip unresolved opencode bin path injection
This commit is contained in:
parent
9c5d80af1d
commit
3f16057a4b
@ -10,9 +10,10 @@ describe("prependResolvedOpencodeBinToPath", () => {
|
|||||||
PATH: "/Users/yeongyu/node_modules/.bin:/usr/bin",
|
PATH: "/Users/yeongyu/node_modules/.bin:/usr/bin",
|
||||||
}
|
}
|
||||||
const resolver = () => "/tmp/bunx-123/node_modules/opencode-ai/bin/opencode"
|
const resolver = () => "/tmp/bunx-123/node_modules/opencode-ai/bin/opencode"
|
||||||
|
const pathExists = () => true
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
prependResolvedOpencodeBinToPath(env, resolver)
|
prependResolvedOpencodeBinToPath(env, resolver, pathExists)
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(env.PATH).toBe(
|
expect(env.PATH).toBe(
|
||||||
@ -26,9 +27,10 @@ describe("prependResolvedOpencodeBinToPath", () => {
|
|||||||
PATH: "/tmp/bunx-123/node_modules/opencode-ai/bin:/usr/bin",
|
PATH: "/tmp/bunx-123/node_modules/opencode-ai/bin:/usr/bin",
|
||||||
}
|
}
|
||||||
const resolver = () => "/tmp/bunx-123/node_modules/opencode-ai/bin/opencode"
|
const resolver = () => "/tmp/bunx-123/node_modules/opencode-ai/bin/opencode"
|
||||||
|
const pathExists = () => true
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
prependResolvedOpencodeBinToPath(env, resolver)
|
prependResolvedOpencodeBinToPath(env, resolver, pathExists)
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(env.PATH).toBe("/tmp/bunx-123/node_modules/opencode-ai/bin:/usr/bin")
|
expect(env.PATH).toBe("/tmp/bunx-123/node_modules/opencode-ai/bin:/usr/bin")
|
||||||
@ -49,4 +51,19 @@ describe("prependResolvedOpencodeBinToPath", () => {
|
|||||||
//#then
|
//#then
|
||||||
expect(env.PATH).toBe("/Users/yeongyu/node_modules/.bin:/usr/bin")
|
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,5 +1,6 @@
|
|||||||
import { delimiter, dirname } from "node:path"
|
import { delimiter, dirname } from "node:path"
|
||||||
import { createRequire } from "node:module"
|
import { createRequire } from "node:module"
|
||||||
|
import { existsSync } from "node:fs"
|
||||||
|
|
||||||
type EnvLike = Record<string, string | undefined>
|
type EnvLike = Record<string, string | undefined>
|
||||||
|
|
||||||
@ -8,6 +9,7 @@ const resolveFromCurrentModule = createRequire(import.meta.url).resolve
|
|||||||
export function prependResolvedOpencodeBinToPath(
|
export function prependResolvedOpencodeBinToPath(
|
||||||
env: EnvLike = process.env,
|
env: EnvLike = process.env,
|
||||||
resolve: (id: string) => string = resolveFromCurrentModule,
|
resolve: (id: string) => string = resolveFromCurrentModule,
|
||||||
|
pathExists: (path: string) => boolean = existsSync,
|
||||||
): void {
|
): void {
|
||||||
let resolvedPath: string
|
let resolvedPath: string
|
||||||
try {
|
try {
|
||||||
@ -16,7 +18,15 @@ export function prependResolvedOpencodeBinToPath(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!pathExists(resolvedPath)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const opencodeBinDir = dirname(resolvedPath)
|
const opencodeBinDir = dirname(resolvedPath)
|
||||||
|
if (!pathExists(opencodeBinDir)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const currentPath = env.PATH ?? ""
|
const currentPath = env.PATH ?? ""
|
||||||
const pathSegments = currentPath ? currentPath.split(delimiter) : []
|
const pathSegments = currentPath ? currentPath.split(delimiter) : []
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user