|
| 1 | +import { log } from "builder-util" |
| 2 | +import * as path from "path" |
| 3 | +import * as resolve from "resolve" |
| 4 | +import { NodeModulesCollector } from "./nodeModulesCollector" |
| 5 | +import { PM } from "./packageManager" |
| 6 | +import { Dependency } from "./types" |
| 7 | + |
| 8 | +interface BunDependency extends Dependency<BunDependency, BunDependency> { |
| 9 | + manifestDependencies: Record<string, string> |
| 10 | + manifestOptionalDependencies: Record<string, string> |
| 11 | +} |
| 12 | + |
| 13 | +export class BunNodeModulesCollector extends NodeModulesCollector<BunDependency, BunDependency> { |
| 14 | + public readonly installOptions = { manager: PM.BUN, lockfile: "bun.lock" } |
| 15 | + |
| 16 | + private readonly dependencyCacheByPath = new Map<string, BunDependency>() |
| 17 | + |
| 18 | + protected async getDependenciesTree(): Promise<BunDependency> { |
| 19 | + const rootManifest = require(path.join(this.rootDir, "package.json")) |
| 20 | + const rootName = rootManifest.name ?? "." |
| 21 | + |
| 22 | + const childMaps = await this.resolveChildren(this.rootDir, rootManifest.dependencies ?? {}, rootManifest.optionalDependencies ?? {}) |
| 23 | + return { |
| 24 | + name: rootName, |
| 25 | + version: rootManifest.version ?? "0.0.0", |
| 26 | + path: this.rootDir, |
| 27 | + manifestDependencies: rootManifest.dependencies ?? {}, |
| 28 | + manifestOptionalDependencies: rootManifest.optionalDependencies ?? {}, |
| 29 | + dependencies: Object.keys(childMaps.dependencies).length > 0 ? childMaps.dependencies : undefined, |
| 30 | + optionalDependencies: Object.keys(childMaps.optionalDependencies).length > 0 ? childMaps.optionalDependencies : undefined, |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + protected getArgs(): string[] { |
| 35 | + return [] |
| 36 | + } |
| 37 | + |
| 38 | + protected collectAllDependencies(tree: BunDependency): void { |
| 39 | + const allDeps = [...Object.values(tree.dependencies || {}), ...Object.values(tree.optionalDependencies || {})] |
| 40 | + |
| 41 | + for (const dependency of allDeps) { |
| 42 | + const key = `${dependency.name}@${dependency.version}` |
| 43 | + if (!this.allDependencies.has(key)) { |
| 44 | + this.allDependencies.set(key, dependency) |
| 45 | + this.collectAllDependencies(dependency) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + protected extractProductionDependencyGraph(tree: BunDependency, dependencyId: string): void { |
| 51 | + if (this.productionGraph[dependencyId]) { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const dependencies: string[] = [] |
| 56 | + |
| 57 | + const processDeps = [ |
| 58 | + { entries: tree.dependencies, manifest: tree.manifestDependencies }, |
| 59 | + { entries: tree.optionalDependencies, manifest: tree.manifestOptionalDependencies }, |
| 60 | + ] |
| 61 | + |
| 62 | + for (const { entries, manifest } of processDeps) { |
| 63 | + if (!entries) continue |
| 64 | + |
| 65 | + for (const [alias, dep] of Object.entries(entries)) { |
| 66 | + if (manifest[alias]) { |
| 67 | + const childId = `${dep.name}@${dep.version}` |
| 68 | + dependencies.push(childId) |
| 69 | + this.extractProductionDependencyGraph(dep, childId) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + this.productionGraph[dependencyId] = { dependencies } |
| 75 | + } |
| 76 | + |
| 77 | + protected parseDependenciesTree(_jsonBlob: string): BunDependency { |
| 78 | + throw new Error("BunNodeModulesCollector does not parse external dependency trees") |
| 79 | + } |
| 80 | + |
| 81 | + private async resolveChildren( |
| 82 | + requesterDir: string, |
| 83 | + manifestDependencies: Record<string, string>, |
| 84 | + manifestOptionalDependencies: Record<string, string> |
| 85 | + ): Promise<{ |
| 86 | + dependencies: Record<string, BunDependency> |
| 87 | + optionalDependencies: Record<string, BunDependency> |
| 88 | + }> { |
| 89 | + const dependencies: Record<string, BunDependency> = {} |
| 90 | + const optionalDependencies: Record<string, BunDependency> = {} |
| 91 | + |
| 92 | + const loadPromises: Promise<void>[] = [] |
| 93 | + |
| 94 | + for (const alias of Object.keys(manifestDependencies)) { |
| 95 | + loadPromises.push( |
| 96 | + this.loadDependency(alias, requesterDir, false).then(dep => { |
| 97 | + if (dep) dependencies[alias] = dep |
| 98 | + }) |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + for (const alias of Object.keys(manifestOptionalDependencies)) { |
| 103 | + loadPromises.push( |
| 104 | + this.loadDependency(alias, requesterDir, true).then(dep => { |
| 105 | + if (dep) optionalDependencies[alias] = dep |
| 106 | + }) |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + await Promise.all(loadPromises) |
| 111 | + return { dependencies, optionalDependencies } |
| 112 | + } |
| 113 | + |
| 114 | + private async loadDependency(alias: string, requesterDir: string, isOptional: boolean): Promise<BunDependency | null> { |
| 115 | + const installedPath = this.findInstalledDependency(requesterDir, alias) |
| 116 | + if (!installedPath) { |
| 117 | + if (!isOptional) { |
| 118 | + log.debug({ alias, requesterDir }, "bun collector could not locate dependency") |
| 119 | + } |
| 120 | + return null |
| 121 | + } |
| 122 | + |
| 123 | + // Use resolved path directly - resolve.sync already handles symlinks with preserveSymlinks: false |
| 124 | + const cached = this.dependencyCacheByPath.get(installedPath) |
| 125 | + if (cached) { |
| 126 | + return cached |
| 127 | + } |
| 128 | + |
| 129 | + const manifest = require(path.join(installedPath, "package.json")) |
| 130 | + const packageName = manifest.name ?? alias |
| 131 | + const manifestDependencies = manifest.dependencies ?? {} |
| 132 | + const manifestOptionalDependencies = manifest.optionalDependencies ?? {} |
| 133 | + |
| 134 | + // Create a temporary placeholder to prevent infinite recursion |
| 135 | + const placeholder: BunDependency = { |
| 136 | + name: packageName, |
| 137 | + version: manifest.version ?? "0.0.0", |
| 138 | + path: installedPath, |
| 139 | + manifestDependencies, |
| 140 | + manifestOptionalDependencies, |
| 141 | + } |
| 142 | + this.dependencyCacheByPath.set(installedPath, placeholder) |
| 143 | + |
| 144 | + const childMaps = await this.resolveChildren(installedPath, manifestDependencies, manifestOptionalDependencies) |
| 145 | + |
| 146 | + const dependency: BunDependency = { |
| 147 | + name: packageName, |
| 148 | + version: manifest.version ?? "0.0.0", |
| 149 | + path: installedPath, |
| 150 | + manifestDependencies, |
| 151 | + manifestOptionalDependencies, |
| 152 | + dependencies: Object.keys(childMaps.dependencies).length > 0 ? childMaps.dependencies : undefined, |
| 153 | + optionalDependencies: Object.keys(childMaps.optionalDependencies).length > 0 ? childMaps.optionalDependencies : undefined, |
| 154 | + } |
| 155 | + |
| 156 | + this.dependencyCacheByPath.set(installedPath, dependency) |
| 157 | + return dependency |
| 158 | + } |
| 159 | + |
| 160 | + private findInstalledDependency(basedir: string, dependencyName: string): string | null { |
| 161 | + try { |
| 162 | + const packageJsonPath = resolve.sync(`${dependencyName}${path.sep}package.json`, { |
| 163 | + basedir, |
| 164 | + preserveSymlinks: false, |
| 165 | + includeCoreModules: false, |
| 166 | + }) |
| 167 | + return path.dirname(packageJsonPath) |
| 168 | + } catch (e: any) { |
| 169 | + if (e && e.code === "MODULE_NOT_FOUND") { |
| 170 | + return null |
| 171 | + } |
| 172 | + throw e |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments