npm.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. export * as Npm from "./npm"
  2. import path from "path"
  3. import npa from "npm-package-arg"
  4. import { Effect, Schema, Context, Layer, Option, FileSystem } from "effect"
  5. import { NodeFileSystem } from "@effect/platform-node"
  6. import { FSUtil } from "./fs-util"
  7. import { Global } from "./global"
  8. import { EffectFlock } from "./util/effect-flock"
  9. import { LayerNode } from "./effect/layer-node"
  10. import { filesystem } from "./effect/layer-node-platform"
  11. import { makeRuntime } from "./effect/runtime"
  12. import { NpmConfig } from "./npm-config"
  13. export class InstallFailedError extends Schema.TaggedErrorClass<InstallFailedError>()("NpmInstallFailedError", {
  14. add: Schema.Array(Schema.String).pipe(Schema.optional),
  15. dir: Schema.String,
  16. cause: Schema.optional(Schema.Defect),
  17. }) {}
  18. export interface EntryPoint {
  19. readonly directory: string
  20. readonly entrypoint: Option.Option<string>
  21. }
  22. export interface Interface {
  23. readonly add: (pkg: string) => Effect.Effect<EntryPoint, InstallFailedError | EffectFlock.LockError>
  24. readonly install: (
  25. dir: string,
  26. input?: {
  27. add: {
  28. name: string
  29. version?: string
  30. }[]
  31. },
  32. ) => Effect.Effect<void, EffectFlock.LockError | InstallFailedError>
  33. readonly which: (pkg: string, bin?: string) => Effect.Effect<Option.Option<string>>
  34. }
  35. export class Service extends Context.Service<Service, Interface>()("@opencode/Npm") {}
  36. const illegal = process.platform === "win32" ? new Set(["<", ">", ":", '"', "|", "?", "*"]) : undefined
  37. export function sanitize(pkg: string) {
  38. if (!illegal) return pkg
  39. return Array.from(pkg, (char) => (illegal.has(char) || char.charCodeAt(0) < 32 ? "_" : char)).join("")
  40. }
  41. const resolveEntryPoint = (name: string, dir: string): EntryPoint => {
  42. let entrypoint: Option.Option<string>
  43. try {
  44. const resolved = typeof Bun !== "undefined" ? import.meta.resolve(name, dir) : import.meta.resolve(dir)
  45. entrypoint = Option.some(resolved)
  46. } catch {
  47. entrypoint = Option.none()
  48. }
  49. return {
  50. directory: dir,
  51. entrypoint,
  52. }
  53. }
  54. interface ArboristNode {
  55. name: string
  56. path: string
  57. }
  58. interface ArboristTree {
  59. edgesOut: Map<string, { to?: ArboristNode }>
  60. }
  61. export const layer = Layer.effect(
  62. Service,
  63. Effect.gen(function* () {
  64. const afs = yield* FSUtil.Service
  65. const global = yield* Global.Service
  66. const fs = yield* FileSystem.FileSystem
  67. const flock = yield* EffectFlock.Service
  68. const directory = (pkg: string) => path.join(global.cache, "packages", sanitize(pkg))
  69. const reify = (input: { dir: string; add?: string[] }) =>
  70. Effect.gen(function* () {
  71. yield* flock.acquire(`npm-install:${input.dir}`)
  72. const { Arborist } = yield* Effect.promise(() => import("@npmcli/arborist"))
  73. const add = input.add ?? []
  74. const npmOptions = yield* NpmConfig.load(input.dir)
  75. const arborist = new Arborist({
  76. ...npmOptions,
  77. path: input.dir,
  78. binLinks: true,
  79. progress: false,
  80. savePrefix: "",
  81. ignoreScripts: true,
  82. })
  83. return yield* Effect.tryPromise({
  84. try: () =>
  85. arborist.reify({
  86. ...npmOptions,
  87. add,
  88. save: true,
  89. saveType: "prod",
  90. }),
  91. catch: (cause) =>
  92. new InstallFailedError({
  93. cause,
  94. add,
  95. dir: input.dir,
  96. }),
  97. }) as Effect.Effect<ArboristTree, InstallFailedError>
  98. }).pipe(
  99. Effect.withSpan("Npm.reify", {
  100. attributes: input,
  101. }),
  102. )
  103. const add = Effect.fn("Npm.add")(function* (pkg: string) {
  104. const dir = directory(pkg)
  105. const name = (() => {
  106. try {
  107. return npa(pkg).name ?? pkg
  108. } catch {
  109. return pkg
  110. }
  111. })()
  112. if (yield* afs.existsSafe(path.join(dir, "node_modules", name))) {
  113. return resolveEntryPoint(name, path.join(dir, "node_modules", name))
  114. }
  115. const tree = yield* reify({ dir, add: [pkg] })
  116. const first = tree.edgesOut.values().next().value?.to
  117. if (!first) {
  118. const result = resolveEntryPoint(name, path.join(dir, "node_modules", name))
  119. if (Option.isSome(result.entrypoint)) return result
  120. return yield* new InstallFailedError({ add: [pkg], dir })
  121. }
  122. return resolveEntryPoint(first.name, first.path)
  123. }, Effect.scoped)
  124. const install: Interface["install"] = Effect.fn("Npm.install")(function* (dir, input) {
  125. const canWrite = yield* afs.access(dir, { writable: true }).pipe(
  126. Effect.as(true),
  127. Effect.orElseSucceed(() => false),
  128. )
  129. if (!canWrite) return
  130. const add = input?.add.map((pkg) => [pkg.name, pkg.version].filter(Boolean).join("@")) ?? []
  131. if (
  132. yield* Effect.gen(function* () {
  133. const nodeModulesExists = yield* afs.existsSafe(path.join(dir, "node_modules"))
  134. if (!nodeModulesExists) {
  135. yield* reify({ add, dir })
  136. return true
  137. }
  138. return false
  139. }).pipe(Effect.withSpan("Npm.checkNodeModules"))
  140. )
  141. return
  142. yield* Effect.gen(function* () {
  143. const pkg = yield* afs.readJson(path.join(dir, "package.json")).pipe(Effect.orElseSucceed(() => ({})))
  144. const lock = yield* afs.readJson(path.join(dir, "package-lock.json")).pipe(Effect.orElseSucceed(() => ({})))
  145. const pkgAny = pkg as any
  146. const lockAny = lock as any
  147. const declared = new Set([
  148. ...Object.keys(pkgAny?.dependencies || {}),
  149. ...Object.keys(pkgAny?.devDependencies || {}),
  150. ...Object.keys(pkgAny?.peerDependencies || {}),
  151. ...Object.keys(pkgAny?.optionalDependencies || {}),
  152. ...(input?.add || []).map((pkg) => pkg.name),
  153. ])
  154. const root = lockAny?.packages?.[""] || {}
  155. const locked = new Set([
  156. ...Object.keys(root?.dependencies || {}),
  157. ...Object.keys(root?.devDependencies || {}),
  158. ...Object.keys(root?.peerDependencies || {}),
  159. ...Object.keys(root?.optionalDependencies || {}),
  160. ])
  161. for (const name of declared) {
  162. if (!locked.has(name)) {
  163. yield* reify({ dir, add })
  164. return
  165. }
  166. }
  167. }).pipe(Effect.withSpan("Npm.checkDirty"))
  168. return
  169. }, Effect.scoped)
  170. const which = Effect.fn("Npm.which")(function* (pkg: string, bin?: string) {
  171. const dir = directory(pkg)
  172. const binDir = path.join(dir, "node_modules", ".bin")
  173. const pick = Effect.fnUntraced(function* () {
  174. const files = yield* fs.readDirectory(binDir).pipe(Effect.catch(() => Effect.succeed([] as string[])))
  175. if (files.length === 0) return Option.none<string>()
  176. // Caller picked a specific bin (e.g. pyright exposes both `pyright` and
  177. // `pyright-langserver`); trust the hint if the package provides it.
  178. if (bin) return files.includes(bin) ? Option.some(bin) : Option.none<string>()
  179. if (files.length === 1) return Option.some(files[0])
  180. const pkgJson = yield* afs.readJson(path.join(dir, "node_modules", pkg, "package.json")).pipe(Effect.option)
  181. if (Option.isSome(pkgJson)) {
  182. const parsed = pkgJson.value as { bin?: string | Record<string, string> }
  183. if (parsed?.bin) {
  184. const unscoped = pkg.startsWith("@") ? pkg.split("/")[1] : pkg
  185. const parsedBin = parsed.bin
  186. if (typeof parsedBin === "string") return Option.some(unscoped)
  187. const keys = Object.keys(parsedBin)
  188. if (keys.length === 1) return Option.some(keys[0])
  189. return parsedBin[unscoped] ? Option.some(unscoped) : Option.some(keys[0])
  190. }
  191. }
  192. return Option.some(files[0])
  193. })
  194. return yield* Effect.gen(function* () {
  195. const bin = yield* pick()
  196. if (Option.isSome(bin)) {
  197. return Option.some(path.join(binDir, bin.value))
  198. }
  199. yield* fs.remove(path.join(dir, "package-lock.json")).pipe(Effect.orElseSucceed(() => {}))
  200. yield* add(pkg)
  201. const resolved = yield* pick()
  202. if (Option.isNone(resolved)) return Option.none<string>()
  203. return Option.some(path.join(binDir, resolved.value))
  204. }).pipe(
  205. Effect.scoped,
  206. Effect.orElseSucceed(() => Option.none<string>()),
  207. )
  208. })
  209. return Service.of({
  210. add,
  211. install,
  212. which,
  213. })
  214. }),
  215. )
  216. export const defaultLayer = layer.pipe(
  217. Layer.provide(EffectFlock.layer),
  218. Layer.provide(FSUtil.layer),
  219. Layer.provide(Global.layer),
  220. Layer.provide(NodeFileSystem.layer),
  221. )
  222. export const node = LayerNode.make(layer, [FSUtil.node, Global.node, filesystem, EffectFlock.node])
  223. const { runPromise } = makeRuntime(Service, defaultLayer)
  224. export async function install(...args: Parameters<Interface["install"]>) {
  225. return runPromise((svc) => svc.install(...args))
  226. }
  227. export async function add(...args: Parameters<Interface["add"]>) {
  228. const entry = await runPromise((svc) => svc.add(...args))
  229. return {
  230. directory: entry.directory,
  231. entrypoint: Option.getOrUndefined(entry.entrypoint),
  232. }
  233. }
  234. export async function which(...args: Parameters<Interface["which"]>) {
  235. const resolved = await runPromise((svc) => svc.which(...args))
  236. return Option.getOrUndefined(resolved)
  237. }