npm.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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?: 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<string | undefined>
  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: string | undefined
  43. try {
  44. entrypoint = typeof Bun !== "undefined" ? import.meta.resolve(name, dir) : import.meta.resolve(dir)
  45. } catch {
  46. entrypoint = undefined
  47. }
  48. return {
  49. directory: dir,
  50. entrypoint,
  51. }
  52. }
  53. interface ArboristNode {
  54. name: string
  55. path: string
  56. }
  57. interface ArboristTree {
  58. edgesOut: Map<string, { to?: ArboristNode }>
  59. }
  60. export const layer = Layer.effect(
  61. Service,
  62. Effect.gen(function* () {
  63. const afs = yield* FSUtil.Service
  64. const global = yield* Global.Service
  65. const fs = yield* FileSystem.FileSystem
  66. const flock = yield* EffectFlock.Service
  67. const directory = (pkg: string) => path.join(global.cache, "packages", sanitize(pkg))
  68. const reify = (input: { dir: string; add?: string[] }) =>
  69. Effect.gen(function* () {
  70. yield* flock.acquire(`npm-install:${input.dir}`)
  71. const { Arborist } = yield* Effect.promise(() => import("@npmcli/arborist"))
  72. const add = input.add ?? []
  73. const npmOptions = yield* NpmConfig.load(input.dir)
  74. const arborist = new Arborist({
  75. ...npmOptions,
  76. path: input.dir,
  77. binLinks: true,
  78. progress: false,
  79. savePrefix: "",
  80. ignoreScripts: true,
  81. })
  82. return yield* Effect.tryPromise({
  83. try: () =>
  84. arborist.reify({
  85. ...npmOptions,
  86. add,
  87. save: true,
  88. saveType: "prod",
  89. }),
  90. catch: (cause) =>
  91. new InstallFailedError({
  92. cause,
  93. add,
  94. dir: input.dir,
  95. }),
  96. }) as Effect.Effect<ArboristTree, InstallFailedError>
  97. }).pipe(
  98. Effect.withSpan("Npm.reify", {
  99. attributes: input,
  100. }),
  101. )
  102. const add = Effect.fn("Npm.add")(function* (pkg: string) {
  103. const dir = directory(pkg)
  104. const name = (() => {
  105. try {
  106. return npa(pkg).name ?? pkg
  107. } catch {
  108. return pkg
  109. }
  110. })()
  111. if (yield* afs.existsSafe(path.join(dir, "node_modules", name))) {
  112. return resolveEntryPoint(name, path.join(dir, "node_modules", name))
  113. }
  114. const tree = yield* reify({ dir, add: [pkg] })
  115. const first = tree.edgesOut.values().next().value?.to
  116. if (!first) {
  117. const result = resolveEntryPoint(name, path.join(dir, "node_modules", name))
  118. if (result.entrypoint) return result
  119. return yield* new InstallFailedError({ add: [pkg], dir })
  120. }
  121. return resolveEntryPoint(first.name, first.path)
  122. }, Effect.scoped)
  123. const install: Interface["install"] = Effect.fn("Npm.install")(function* (dir, input) {
  124. const canWrite = yield* afs.access(dir, { writable: true }).pipe(
  125. Effect.as(true),
  126. Effect.orElseSucceed(() => false),
  127. )
  128. if (!canWrite) return
  129. const add = input?.add.map((pkg) => [pkg.name, pkg.version].filter(Boolean).join("@")) ?? []
  130. if (
  131. yield* Effect.gen(function* () {
  132. const nodeModulesExists = yield* afs.existsSafe(path.join(dir, "node_modules"))
  133. if (!nodeModulesExists) {
  134. yield* reify({ add, dir })
  135. return true
  136. }
  137. return false
  138. }).pipe(Effect.withSpan("Npm.checkNodeModules"))
  139. )
  140. return
  141. yield* Effect.gen(function* () {
  142. const pkg = yield* afs.readJson(path.join(dir, "package.json")).pipe(Effect.orElseSucceed(() => ({})))
  143. const lock = yield* afs.readJson(path.join(dir, "package-lock.json")).pipe(Effect.orElseSucceed(() => ({})))
  144. const pkgAny = pkg as any
  145. const lockAny = lock as any
  146. const declared = new Set([
  147. ...Object.keys(pkgAny?.dependencies || {}),
  148. ...Object.keys(pkgAny?.devDependencies || {}),
  149. ...Object.keys(pkgAny?.peerDependencies || {}),
  150. ...Object.keys(pkgAny?.optionalDependencies || {}),
  151. ...(input?.add || []).map((pkg) => pkg.name),
  152. ])
  153. const root = lockAny?.packages?.[""] || {}
  154. const locked = new Set([
  155. ...Object.keys(root?.dependencies || {}),
  156. ...Object.keys(root?.devDependencies || {}),
  157. ...Object.keys(root?.peerDependencies || {}),
  158. ...Object.keys(root?.optionalDependencies || {}),
  159. ])
  160. for (const name of declared) {
  161. if (!locked.has(name)) {
  162. yield* reify({ dir, add })
  163. return
  164. }
  165. }
  166. }).pipe(Effect.withSpan("Npm.checkDirty"))
  167. return
  168. }, Effect.scoped)
  169. const which = Effect.fn("Npm.which")(function* (pkg: string, bin?: string) {
  170. const dir = directory(pkg)
  171. const binDir = path.join(dir, "node_modules", ".bin")
  172. const pick = Effect.fnUntraced(function* () {
  173. const files = yield* fs.readDirectory(binDir).pipe(Effect.catch(() => Effect.succeed([] as string[])))
  174. if (files.length === 0) return Option.none<string>()
  175. // Caller picked a specific bin (e.g. pyright exposes both `pyright` and
  176. // `pyright-langserver`); trust the hint if the package provides it.
  177. if (bin) return files.includes(bin) ? Option.some(bin) : Option.none<string>()
  178. if (files.length === 1) return Option.some(files[0])
  179. const pkgJson = yield* afs.readJson(path.join(dir, "node_modules", pkg, "package.json")).pipe(Effect.option)
  180. if (Option.isSome(pkgJson)) {
  181. const parsed = pkgJson.value as { bin?: string | Record<string, string> }
  182. if (parsed?.bin) {
  183. const unscoped = pkg.startsWith("@") ? pkg.split("/")[1] : pkg
  184. const parsedBin = parsed.bin
  185. if (typeof parsedBin === "string") return Option.some(unscoped)
  186. const keys = Object.keys(parsedBin)
  187. if (keys.length === 1) return Option.some(keys[0])
  188. return parsedBin[unscoped] ? Option.some(unscoped) : Option.some(keys[0])
  189. }
  190. }
  191. return Option.some(files[0])
  192. })
  193. return Option.getOrUndefined(
  194. 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. })
  210. return Service.of({
  211. add,
  212. install,
  213. which,
  214. })
  215. }),
  216. )
  217. export const defaultLayer = layer.pipe(
  218. Layer.provide(EffectFlock.layer),
  219. Layer.provide(FSUtil.layer),
  220. Layer.provide(Global.layer),
  221. Layer.provide(NodeFileSystem.layer),
  222. )
  223. export const node = LayerNode.make(layer, [FSUtil.node, Global.node, filesystem, EffectFlock.node])
  224. const { runPromise } = makeRuntime(Service, defaultLayer)
  225. export async function install(...args: Parameters<Interface["install"]>) {
  226. return runPromise((svc) => svc.install(...args))
  227. }
  228. export async function add(...args: Parameters<Interface["add"]>) {
  229. return runPromise((svc) => svc.add(...args))
  230. }
  231. export async function which(...args: Parameters<Interface["which"]>) {
  232. return runPromise((svc) => svc.which(...args))
  233. }