npm.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. export * as Npm from "./npm"
  2. import path from "path"
  3. import { fileURLToPath } from "url"
  4. import npa from "npm-package-arg"
  5. import semver from "semver"
  6. // @ts-expect-error npm does not publish types for this internal config API.
  7. import Config from "@npmcli/config"
  8. // @ts-expect-error npm does not publish types for this internal config API.
  9. import { definitions, flatten, nerfDarts, shorthands } from "@npmcli/config/lib/definitions/index.js"
  10. import { Effect, Schema, Context, Layer, Option, FileSystem, Stream } from "effect"
  11. import { NodeFileSystem } from "@effect/platform-node"
  12. import { AppFileSystem } from "./filesystem"
  13. import { Global } from "./global"
  14. import { EffectFlock } from "./util/effect-flock"
  15. import { makeRuntime } from "./effect/runtime"
  16. import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
  17. import { CrossSpawnSpawner } from "./cross-spawn-spawner"
  18. export class InstallFailedError extends Schema.TaggedErrorClass<InstallFailedError>()("NpmInstallFailedError", {
  19. add: Schema.Array(Schema.String).pipe(Schema.optional),
  20. dir: Schema.String,
  21. cause: Schema.optional(Schema.Defect),
  22. }) {}
  23. export interface EntryPoint {
  24. readonly directory: string
  25. readonly entrypoint: Option.Option<string>
  26. }
  27. export interface Interface {
  28. readonly add: (pkg: string) => Effect.Effect<EntryPoint, InstallFailedError | EffectFlock.LockError>
  29. readonly install: (
  30. dir: string,
  31. input?: {
  32. add: {
  33. name: string
  34. version?: string
  35. }[]
  36. },
  37. ) => Effect.Effect<void, EffectFlock.LockError | InstallFailedError>
  38. readonly outdated: (pkg: string, cachedVersion: string) => Effect.Effect<boolean>
  39. readonly which: (pkg: string, bin?: string) => Effect.Effect<Option.Option<string>>
  40. }
  41. export class Service extends Context.Service<Service, Interface>()("@opencode/Npm") {}
  42. const illegal = process.platform === "win32" ? new Set(["<", ">", ":", '"', "|", "?", "*"]) : undefined
  43. const npmPath = fileURLToPath(new URL("..", import.meta.url))
  44. export function sanitize(pkg: string) {
  45. if (!illegal) return pkg
  46. return Array.from(pkg, (char) => (illegal.has(char) || char.charCodeAt(0) < 32 ? "_" : char)).join("")
  47. }
  48. const loadOptions = (dir: string) =>
  49. Effect.tryPromise({
  50. try: async () => {
  51. const config = new Config({
  52. npmPath,
  53. cwd: dir,
  54. env: { ...process.env },
  55. argv: [process.execPath, process.execPath],
  56. execPath: process.execPath,
  57. platform: process.platform,
  58. definitions,
  59. flatten,
  60. nerfDarts,
  61. shorthands,
  62. warn: false,
  63. })
  64. await config.load()
  65. return config.flat
  66. },
  67. catch: (cause) =>
  68. new InstallFailedError({
  69. cause,
  70. dir,
  71. }),
  72. })
  73. const resolveEntryPoint = (name: string, dir: string): EntryPoint => {
  74. let entrypoint: Option.Option<string>
  75. try {
  76. const resolved = typeof Bun !== "undefined" ? import.meta.resolve(name, dir) : import.meta.resolve(dir)
  77. entrypoint = Option.some(resolved)
  78. } catch {
  79. entrypoint = Option.none()
  80. }
  81. return {
  82. directory: dir,
  83. entrypoint,
  84. }
  85. }
  86. interface ArboristNode {
  87. name: string
  88. path: string
  89. }
  90. interface ArboristTree {
  91. edgesOut: Map<string, { to?: ArboristNode }>
  92. }
  93. export const layer = Layer.effect(
  94. Service,
  95. Effect.gen(function* () {
  96. const afs = yield* AppFileSystem.Service
  97. const global = yield* Global.Service
  98. const fs = yield* FileSystem.FileSystem
  99. const flock = yield* EffectFlock.Service
  100. const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
  101. const directory = (pkg: string) => path.join(global.cache, "packages", sanitize(pkg))
  102. const runView = Effect.fnUntraced(function* (cmd: string[]) {
  103. const handle = yield* spawner.spawn(
  104. ChildProcess.make(cmd[0], cmd.slice(1), {
  105. extendEnv: true,
  106. }),
  107. )
  108. const [stdout, stderr] = yield* Effect.all(
  109. [Stream.mkString(Stream.decodeText(handle.stdout)), Stream.mkString(Stream.decodeText(handle.stderr))],
  110. { concurrency: 2 },
  111. )
  112. const code = yield* handle.exitCode
  113. if (code !== 0 || !stdout.trim()) {
  114. return yield* Effect.fail(stderr || stdout || `Failed to run ${cmd.join(" ")}`)
  115. }
  116. return yield* Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.String))(stdout)
  117. }, Effect.scoped)
  118. const viewLatestVersion = Effect.fnUntraced(function* (pkg: string) {
  119. return yield* runView(["npm", "view", pkg, "dist-tags.latest", "--json"]).pipe(
  120. Effect.catch(() =>
  121. runView(["pnpm", "view", pkg, "dist-tags.latest", "--json"]).pipe(
  122. Effect.catch(() => runView(["bun", "pm", "view", pkg, "dist-tags.latest", "--json"])),
  123. ),
  124. ),
  125. )
  126. })
  127. const reify = (input: { dir: string; add?: string[] }) =>
  128. Effect.gen(function* () {
  129. yield* flock.acquire(`npm-install:${input.dir}`)
  130. const { Arborist } = yield* Effect.promise(() => import("@npmcli/arborist"))
  131. const add = input.add ?? []
  132. const npmOptions = yield* loadOptions(input.dir)
  133. const arborist = new Arborist({
  134. ...npmOptions,
  135. path: input.dir,
  136. binLinks: true,
  137. progress: false,
  138. savePrefix: "",
  139. ignoreScripts: true,
  140. })
  141. return yield* Effect.tryPromise({
  142. try: () =>
  143. arborist.reify({
  144. ...npmOptions,
  145. add,
  146. save: true,
  147. saveType: "prod",
  148. }),
  149. catch: (cause) =>
  150. new InstallFailedError({
  151. cause,
  152. add,
  153. dir: input.dir,
  154. }),
  155. }) as Effect.Effect<ArboristTree, InstallFailedError>
  156. }).pipe(
  157. Effect.withSpan("Npm.reify", {
  158. attributes: input,
  159. }),
  160. )
  161. const outdated = Effect.fn("Npm.outdated")(function* (pkg: string, cachedVersion: string) {
  162. const latestVersion = yield* viewLatestVersion(pkg).pipe(Effect.option)
  163. if (Option.isNone(latestVersion)) {
  164. return false
  165. }
  166. const range = /[\s^~*xX<>|=]/.test(cachedVersion)
  167. if (range) return !semver.satisfies(latestVersion.value, cachedVersion)
  168. return semver.lt(cachedVersion, latestVersion.value)
  169. })
  170. const add = Effect.fn("Npm.add")(function* (pkg: string) {
  171. const dir = directory(pkg)
  172. const name = (() => {
  173. try {
  174. return npa(pkg).name ?? pkg
  175. } catch {
  176. return pkg
  177. }
  178. })()
  179. if (yield* afs.existsSafe(dir)) {
  180. return resolveEntryPoint(name, path.join(dir, "node_modules", name))
  181. }
  182. const tree = yield* reify({ dir, add: [pkg] })
  183. const first = tree.edgesOut.values().next().value?.to
  184. if (!first) return yield* new InstallFailedError({ add: [pkg], dir })
  185. return resolveEntryPoint(first.name, first.path)
  186. }, Effect.scoped)
  187. const install: Interface["install"] = Effect.fn("Npm.install")(function* (dir, input) {
  188. const canWrite = yield* afs.access(dir, { writable: true }).pipe(
  189. Effect.as(true),
  190. Effect.orElseSucceed(() => false),
  191. )
  192. if (!canWrite) return
  193. const add = input?.add.map((pkg) => [pkg.name, pkg.version].filter(Boolean).join("@")) ?? []
  194. if (
  195. yield* Effect.gen(function* () {
  196. const nodeModulesExists = yield* afs.existsSafe(path.join(dir, "node_modules"))
  197. if (!nodeModulesExists) {
  198. yield* reify({ add, dir })
  199. return true
  200. }
  201. return false
  202. }).pipe(Effect.withSpan("Npm.checkNodeModules"))
  203. )
  204. return
  205. yield* Effect.gen(function* () {
  206. const pkg = yield* afs.readJson(path.join(dir, "package.json")).pipe(Effect.orElseSucceed(() => ({})))
  207. const lock = yield* afs.readJson(path.join(dir, "package-lock.json")).pipe(Effect.orElseSucceed(() => ({})))
  208. const pkgAny = pkg as any
  209. const lockAny = lock as any
  210. const declared = new Set([
  211. ...Object.keys(pkgAny?.dependencies || {}),
  212. ...Object.keys(pkgAny?.devDependencies || {}),
  213. ...Object.keys(pkgAny?.peerDependencies || {}),
  214. ...Object.keys(pkgAny?.optionalDependencies || {}),
  215. ...(input?.add || []).map((pkg) => pkg.name),
  216. ])
  217. const root = lockAny?.packages?.[""] || {}
  218. const locked = new Set([
  219. ...Object.keys(root?.dependencies || {}),
  220. ...Object.keys(root?.devDependencies || {}),
  221. ...Object.keys(root?.peerDependencies || {}),
  222. ...Object.keys(root?.optionalDependencies || {}),
  223. ])
  224. for (const name of declared) {
  225. if (!locked.has(name)) {
  226. yield* reify({ dir, add })
  227. return
  228. }
  229. }
  230. }).pipe(Effect.withSpan("Npm.checkDirty"))
  231. return
  232. }, Effect.scoped)
  233. const which = Effect.fn("Npm.which")(function* (pkg: string, bin?: string) {
  234. const dir = directory(pkg)
  235. const binDir = path.join(dir, "node_modules", ".bin")
  236. const pick = Effect.fnUntraced(function* () {
  237. const files = yield* fs.readDirectory(binDir).pipe(Effect.catch(() => Effect.succeed([] as string[])))
  238. if (files.length === 0) return Option.none<string>()
  239. // Caller picked a specific bin (e.g. pyright exposes both `pyright` and
  240. // `pyright-langserver`); trust the hint if the package provides it.
  241. if (bin) return files.includes(bin) ? Option.some(bin) : Option.none<string>()
  242. if (files.length === 1) return Option.some(files[0])
  243. const pkgJson = yield* afs.readJson(path.join(dir, "node_modules", pkg, "package.json")).pipe(Effect.option)
  244. if (Option.isSome(pkgJson)) {
  245. const parsed = pkgJson.value as { bin?: string | Record<string, string> }
  246. if (parsed?.bin) {
  247. const unscoped = pkg.startsWith("@") ? pkg.split("/")[1] : pkg
  248. const parsedBin = parsed.bin
  249. if (typeof parsedBin === "string") return Option.some(unscoped)
  250. const keys = Object.keys(parsedBin)
  251. if (keys.length === 1) return Option.some(keys[0])
  252. return parsedBin[unscoped] ? Option.some(unscoped) : Option.some(keys[0])
  253. }
  254. }
  255. return Option.some(files[0])
  256. })
  257. return yield* Effect.gen(function* () {
  258. const bin = yield* pick()
  259. if (Option.isSome(bin)) {
  260. return Option.some(path.join(binDir, bin.value))
  261. }
  262. yield* fs.remove(path.join(dir, "package-lock.json")).pipe(Effect.orElseSucceed(() => {}))
  263. yield* add(pkg)
  264. const resolved = yield* pick()
  265. if (Option.isNone(resolved)) return Option.none<string>()
  266. return Option.some(path.join(binDir, resolved.value))
  267. }).pipe(
  268. Effect.scoped,
  269. Effect.orElseSucceed(() => Option.none<string>()),
  270. )
  271. })
  272. return Service.of({
  273. add,
  274. install,
  275. outdated,
  276. which,
  277. })
  278. }),
  279. )
  280. export const defaultLayer = layer.pipe(
  281. Layer.provide(EffectFlock.layer),
  282. Layer.provide(AppFileSystem.layer),
  283. Layer.provide(Global.layer),
  284. Layer.provide(NodeFileSystem.layer),
  285. Layer.provide(CrossSpawnSpawner.defaultLayer),
  286. )
  287. const { runPromise } = makeRuntime(Service, defaultLayer)
  288. export async function install(...args: Parameters<Interface["install"]>) {
  289. return runPromise((svc) => svc.install(...args))
  290. }
  291. export async function add(...args: Parameters<Interface["add"]>) {
  292. const entry = await runPromise((svc) => svc.add(...args))
  293. return {
  294. directory: entry.directory,
  295. entrypoint: Option.getOrUndefined(entry.entrypoint),
  296. }
  297. }
  298. export async function outdated(...args: Parameters<Interface["outdated"]>) {
  299. return runPromise((svc) => svc.outdated(...args))
  300. }
  301. export async function which(...args: Parameters<Interface["which"]>) {
  302. const resolved = await runPromise((svc) => svc.which(...args))
  303. return Option.getOrUndefined(resolved)
  304. }