canonicalize-node-modules.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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
  34. | SemverLike
  35. | {
  36. default: SemverLike
  37. }
  38. const semver = "default" in semverModule ? semverModule.default : semverModule
  39. const selections = new Map<string, Entry>()
  40. for (const [slug, list] of versions) {
  41. list.sort((a, b) => {
  42. const left = semver.valid(a.version)
  43. const right = semver.valid(b.version)
  44. if (left && right) {
  45. const delta = semver.rcompare(left, right)
  46. if (delta !== 0) {
  47. return delta
  48. }
  49. }
  50. if (left && !right) {
  51. return -1
  52. }
  53. if (!left && right) {
  54. return 1
  55. }
  56. return b.version.localeCompare(a.version)
  57. })
  58. selections.set(slug, list[0])
  59. }
  60. await rm(linkRoot, { recursive: true, force: true })
  61. await mkdir(linkRoot, { recursive: true })
  62. const rewrites: string[] = []
  63. for (const [slug, entry] of Array.from(selections.entries()).sort((a, b) => a[0].localeCompare(b[0]))) {
  64. const parts = slug.split("/")
  65. const leaf = parts.pop()
  66. if (!leaf) {
  67. continue
  68. }
  69. const parent = join(linkRoot, ...parts)
  70. await mkdir(parent, { recursive: true })
  71. const linkPath = join(parent, leaf)
  72. const desired = join(entry.dir, "node_modules", slug)
  73. const relativeTarget = relative(parent, desired)
  74. const resolved = relativeTarget.length === 0 ? "." : relativeTarget
  75. await rm(linkPath, { recursive: true, force: true })
  76. await symlink(resolved, linkPath)
  77. rewrites.push(slug + " -> " + resolved)
  78. }
  79. rewrites.sort()
  80. console.log("[canonicalize-node-modules] rebuilt", rewrites.length, "links")
  81. for (const line of rewrites.slice(0, 20)) {
  82. console.log(" ", line)
  83. }
  84. if (rewrites.length > 20) {
  85. console.log(" ...")
  86. }