| 61 | ) |
| 62 | |
| 63 | export async function install(pkg: string, version = "latest") { |
| 64 | // Use lock to ensure only one install at a time |
| 65 | using _ = await Lock.write("bun-install") |
| 66 | |
| 67 | const mod = path.join(Global.Path.cache, "node_modules", pkg) |
| 68 | const pkgjson = Bun.file(path.join(Global.Path.cache, "package.json")) |
| 69 | const parsed = await pkgjson.json().catch(async () => { |
| 70 | const result = { dependencies: {} } |
| 71 | await Bun.write(pkgjson.name!, JSON.stringify(result, null, 2)) |
| 72 | return result |
| 73 | }) |
| 74 | if (parsed.dependencies[pkg] === version) return mod |
| 75 | |
| 76 | // Build command arguments |
| 77 | const args = ["add", "--force", "--exact", "--cwd", Global.Path.cache, pkg + "@" + version] |
| 78 | |
| 79 | // Let Bun handle registry resolution: |
| 80 | // - If .npmrc files exist, Bun will use them automatically |
| 81 | // - If no .npmrc files exist, Bun will default to https://registry.npmjs.org |
| 82 | // - No need to pass --registry flag |
| 83 | log.info("installing package using Bun's default registry resolution", { |
| 84 | pkg, |
| 85 | version, |
| 86 | }) |
| 87 | |
| 88 | await BunProc.run(args, { |
| 89 | cwd: Global.Path.cache, |
| 90 | }).catch((e) => { |
| 91 | throw new InstallFailedError( |
| 92 | { pkg, version }, |
| 93 | { |
| 94 | cause: e, |
| 95 | }, |
| 96 | ) |
| 97 | }) |
| 98 | |
| 99 | // Resolve actual version from installed package when using "latest" |
| 100 | // This ensures subsequent starts use the cached version until explicitly updated |
| 101 | let resolvedVersion = version |
| 102 | if (version === "latest") { |
| 103 | const installedPkgJson = Bun.file(path.join(mod, "package.json")) |
| 104 | const installedPkg = await installedPkgJson.json().catch(() => null) |
| 105 | if (installedPkg?.version) { |
| 106 | resolvedVersion = installedPkg.version |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | parsed.dependencies[pkg] = resolvedVersion |
| 111 | await Bun.write(pkgjson.name!, JSON.stringify(parsed, null, 2)) |
| 112 | return mod |
| 113 | } |
| 114 | |
| 115 | export async function resolve(pkg: string) { |
| 116 | const local = workspace(pkg) |