Skip to content

Commit cded68a

Browse files
authored
refactor(npm): use object-based package spec for install API (#23181)
1 parent 0068cce commit cded68a

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

packages/opencode/src/cli/cmd/tui/config/tui.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ export const layer = Layer.effect(
158158
(dir) =>
159159
npm
160160
.install(dir, {
161-
add: ["@opencode-ai/plugin" + (InstallationLocal ? "" : "@" + InstallationVersion)],
161+
add: [
162+
{
163+
name: "@opencode-ai/plugin",
164+
version: InstallationLocal ? undefined : InstallationVersion,
165+
},
166+
],
162167
})
163168
.pipe(Effect.forkScoped),
164169
{

packages/opencode/src/config/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,12 @@ export const layer = Layer.effect(
518518

519519
const dep = yield* npmSvc
520520
.install(dir, {
521-
add: ["@opencode-ai/plugin" + (InstallationLocal ? "" : "@" + InstallationVersion)],
521+
add: [
522+
{
523+
name: "@opencode-ai/plugin",
524+
version: InstallationLocal ? undefined : InstallationVersion,
525+
},
526+
],
522527
})
523528
.pipe(
524529
Effect.exit,

packages/opencode/src/npm/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ export interface Interface {
2525
readonly add: (pkg: string) => Effect.Effect<EntryPoint, InstallFailedError | EffectFlock.LockError>
2626
readonly install: (
2727
dir: string,
28-
input?: { add: string[] },
28+
input?: {
29+
add: {
30+
name: string
31+
version?: string
32+
}[]
33+
},
2934
) => Effect.Effect<void, EffectFlock.LockError | InstallFailedError>
3035
readonly outdated: (pkg: string, cachedVersion: string) => Effect.Effect<boolean>
3136
readonly which: (pkg: string) => Effect.Effect<Option.Option<string>>
@@ -137,17 +142,18 @@ export const layer = Layer.effect(
137142
return resolveEntryPoint(first.name, first.path)
138143
}, Effect.scoped)
139144

140-
const install = Effect.fn("Npm.install")(function* (dir: string, input?: { add: string[] }) {
145+
const install: Interface["install"] = Effect.fn("Npm.install")(function* (dir, input) {
141146
const canWrite = yield* afs.access(dir, { writable: true }).pipe(
142147
Effect.as(true),
143148
Effect.orElseSucceed(() => false),
144149
)
145150
if (!canWrite) return
146151

152+
const add = input?.add.map((pkg) => [pkg.name, pkg.version].filter(Boolean).join("@")) ?? []
147153
yield* Effect.gen(function* () {
148154
const nodeModulesExists = yield* afs.existsSafe(path.join(dir, "node_modules"))
149155
if (!nodeModulesExists) {
150-
yield* reify({ add: input?.add, dir })
156+
yield* reify({ add, dir })
151157
return
152158
}
153159
}).pipe(Effect.withSpan("Npm.checkNodeModules"))
@@ -163,7 +169,7 @@ export const layer = Layer.effect(
163169
...Object.keys(pkgAny?.devDependencies || {}),
164170
...Object.keys(pkgAny?.peerDependencies || {}),
165171
...Object.keys(pkgAny?.optionalDependencies || {}),
166-
...(input?.add || []),
172+
...(input?.add || []).map((pkg) => pkg.name),
167173
])
168174

169175
const root = lockAny?.packages?.[""] || {}
@@ -176,7 +182,7 @@ export const layer = Layer.effect(
176182

177183
for (const name of declared) {
178184
if (!locked.has(name)) {
179-
yield* reify({ dir, add: input?.add })
185+
yield* reify({ dir, add })
180186
return
181187
}
182188
}

0 commit comments

Comments
 (0)