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