|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +const scriptDir = path.dirname(fileURLToPath(import.meta.url)); |
| 6 | +const repoRoot = path.resolve(scriptDir, ".."); |
| 7 | +const effectRoot = path.join(repoRoot, "node_modules", "effect"); |
| 8 | +const effectScopeDir = path.join(repoRoot, "node_modules", "@effect"); |
| 9 | + |
| 10 | +async function exists(targetPath) { |
| 11 | + try { |
| 12 | + await fs.lstat(targetPath); |
| 13 | + return true; |
| 14 | + } catch { |
| 15 | + return false; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +async function dedupeNestedEffect(packageDir) { |
| 20 | + const nestedEffectDir = path.join(packageDir, "node_modules", "effect"); |
| 21 | + if (!(await exists(nestedEffectDir))) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + const nestedStat = await fs.lstat(nestedEffectDir); |
| 26 | + if (nestedStat.isSymbolicLink()) { |
| 27 | + const linkTarget = await fs.readlink(nestedEffectDir); |
| 28 | + const resolvedTarget = path.resolve(path.dirname(nestedEffectDir), linkTarget); |
| 29 | + if (resolvedTarget === effectRoot) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + await fs.rm(nestedEffectDir, { recursive: true, force: true }); |
| 35 | + const relativeTarget = path.relative(path.dirname(nestedEffectDir), effectRoot); |
| 36 | + await fs.symlink(relativeTarget, nestedEffectDir, "dir"); |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +async function main() { |
| 41 | + if (!(await exists(effectRoot)) || !(await exists(effectScopeDir))) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const entries = await fs.readdir(effectScopeDir, { withFileTypes: true }); |
| 46 | + let dedupedCount = 0; |
| 47 | + for (const entry of entries) { |
| 48 | + if (!entry.isDirectory()) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + const changed = await dedupeNestedEffect(path.join(effectScopeDir, entry.name)); |
| 52 | + if (changed) { |
| 53 | + dedupedCount += 1; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (dedupedCount > 0) { |
| 58 | + console.log(`[dedupe-effect-node-modules] linked root effect into ${dedupedCount} package(s)`); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +await main(); |
0 commit comments