canonicalize-node-modules.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { lstat, mkdir, readdir, rm, symlink } from "fs/promises"
  2. import { join, relative } from "path"
  3. type SemverLike = {
  4. valid: (value: string) => string | null
  5. rcompare: (left: string, right: string) => number
  6. }
  7. type Entry = {
  8. dir: string
  9. version: string
  10. label: string
  11. }
  12. const root = process.cwd()
  13. const bunRoot = join(root, "node_modules/.bun")
  14. const linkRoot = join(bunRoot, "node_modules")
  15. const directories = (await readdir(bunRoot)).sort()
  16. const versions = new Map<string, Entry[]>()
  17. for (const entry of directories) {
  18. const full = join(bunRoot, entry)
  19. const info = await lstat(full)
  20. if (!info.isDirectory()) {
  21. continue
  22. }
  23. const marker = entry.lastIndexOf("@")
  24. if (marker <= 0) {
  25. continue
  26. }
  27. const slug = entry.slice(0, marker).replace(/\+/g, "/")
  28. const version = entry.slice(marker + 1)
  29. const list = versions.get(slug) ?? []
  30. list.push({ dir: full, version, label: entry })
  31. versions.set(slug, list)
  32. }
  33. const semverModule = (await import(join(bunRoot, "node_modules/semver"))) as SemverLike | {
  34. default: SemverLike
  35. }
  36. const semver = "default" in semverModule ? semverModule.default : semverModule
  37. const selections = new Map<string, Entry>()
  38. for (const [slug, list] of versions) {
  39. list.sort((a, b) => {
  40. const left = semver.valid(a.version)
  41. const right = semver.valid(b.version)
  42. if (left && right) {
  43. const delta = semver.rcompare(left, right)
  44. if (delta !== 0) {
  45. return delta
  46. }
  47. }
  48. if (left && !right) {
  49. return -1
  50. }
  51. if (!left && right) {
  52. return 1
  53. }
  54. return b.version.localeCompare(a.version)
  55. })
  56. selections.set(slug, list[0])
  57. }
  58. await rm(linkRoot, { recursive: true, force: true })
  59. await mkdir(linkRoot, { recursive: true })
  60. const rewrites: string[] = []
  61. for (const [slug, entry] of Array.from(selections.entries()).sort((a, b) => a[0].localeCompare(b[0]))) {
  62. const parts = slug.split("/")
  63. const leaf = parts.pop()
  64. if (!leaf) {
  65. continue
  66. }
  67. const parent = join(linkRoot, ...parts)
  68. await mkdir(parent, { recursive: true })
  69. const linkPath = join(parent, leaf)
  70. const desired = join(entry.dir, "node_modules", slug)
  71. const relativeTarget = relative(parent, desired)
  72. const resolved = relativeTarget.length === 0 ? "." : relativeTarget
  73. await rm(linkPath, { recursive: true, force: true })
  74. await symlink(resolved, linkPath)
  75. rewrites.push(slug + " -> " + resolved)
  76. }
  77. rewrites.sort()
  78. console.log("[canonicalize-node-modules] rebuilt", rewrites.length, "links")
  79. for (const line of rewrites.slice(0, 20)) {
  80. console.log(" ", line)
  81. }
  82. if (rewrites.length > 20) {
  83. console.log(" ...")
  84. }