-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymlinks-to-hardlinks.js
64 lines (55 loc) · 1.88 KB
/
symlinks-to-hardlinks.js
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
63
64
import {existsSync} from "node:fs";
import {readdir, readlink, mkdir, link} from "node:fs/promises";
import { parseArgs } from "node:util";
import { join, dirname, basename, relative, resolve } from "node:path";
const options = {
"symlink-dir": {type: "string"},
"output-dir": {type: "string"},
"execute": { type: "boolean", default: false }
};
const { values } = parseArgs({ options });
const symlinkDir = values["symlink-dir"];
const rawDir = values["raw-dir"];
const outputDir = values["output-dir"];
const execute = values["execute"];
console.log('CLI arguments:',{
symlinkDir,
rawDir,
outputDir,
execute
})
async function processSymlinks(dir) {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
await processSymlinks(fullPath);
} else if (entry.isSymbolicLink()) {
await handleSymlink(fullPath);
}
}
}
async function handleSymlink(symlinkPath) {
try {
const targetPath = resolve(dirname(symlinkPath), await readlink(symlinkPath));
const rawFilename = basename(targetPath);
const symlinkRelativeFromSymlinkRoot = relative(symlinkDir, symlinkPath);
const relativeDir = dirname(symlinkRelativeFromSymlinkRoot);
const outputPath = join(outputDir, relativeDir, rawFilename);
if (!existsSync(targetPath)) {
console.error(`Target of symlink ${symlinkPath} does not exist: ${targetPath}`);
return;
}
if (!execute) {
console.log(`[Dry Run] Would hardlink: ${targetPath} -> ${outputPath}`);
return;
}
await mkdir(dirname(outputPath), { recursive: true });
await link(targetPath, outputPath);
console.log(`Hardlinked: ${targetPath} -> ${outputPath}`);
} catch (error) {
console.error(`Error processing symlink ${symlinkPath}:`, error);
}
}
console.log(`Starting in ${execute ? "EXECUTION" : "DRY RUN"} mode.`);
await processSymlinks(symlinkDir);