Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ export namespace Config {
}
}

const exists = existsSync(path.join(dir, "node_modules"))
const installing = installDependencies(dir)
if (!exists) await installing
await installDependencies(dir).catch(() => {})

result.command = mergeDeep(result.command ?? {}, await loadCommand(dir))
result.agent = mergeDeep(result.agent, await loadAgent(dir))
Expand Down Expand Up @@ -228,25 +226,31 @@ export namespace Config {

export async function installDependencies(dir: string) {
const pkg = path.join(dir, "package.json")
const pkgjson = Bun.file(pkg)

if (!(await Bun.file(pkg).exists())) {
await Bun.write(pkg, "{}")
if (!(await pkgjson.exists())) {
return
}

const gitignore = path.join(dir, ".gitignore")
const hasGitIgnore = await Bun.file(gitignore).exists()
if (!hasGitIgnore) await Bun.write(gitignore, ["node_modules", "package.json", "bun.lock", ".gitignore"].join("\n"))

await BunProc.run(
["add", "@opencode-ai/plugin@" + (Installation.isLocal() ? "latest" : Installation.VERSION), "--exact"],
{
cwd: dir,
},
).catch(() => {})
const pkgContent = await pkgjson.json().catch(() => ({}))
const deps = pkgContent.dependencies ?? {}
const devDeps = pkgContent.devDependencies ?? {}
const allDeps = { ...deps, ...devDeps }

// Install any additional dependencies defined in the package.json
// This allows local plugins and custom tools to use external packages
await BunProc.run(["install"], { cwd: dir }).catch(() => {})
if (Object.keys(allDeps).length > 0) {
await BunProc.run(["install"], { cwd: dir }).catch(() => {})
}

await BunProc.install("@opencode-ai/plugin", "latest").catch(() => {})

const gitignore = path.join(dir, ".gitignore")
const gitignoreFile = Bun.file(gitignore)
const hasGitIgnore = await gitignoreFile.exists()
if (!hasGitIgnore) {
await Bun.write(gitignore, ["node_modules", "package.json", "bun.lock", ".gitignore"].join("\n"))
}
}

function rel(item: string, patterns: string[]) {
Expand Down
Loading