-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconvert-symlinks.js
85 lines (79 loc) · 2.64 KB
/
convert-symlinks.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const shell = require("shelljs");
const path = require("path");
const symlinkMode = "120000";
async function getSymlinks(dirPath) {
let symlinks = [];
const relPath = dirPath.replaceAll("../", "/").replaceAll("/", "\\");
const cwd = process.cwd();
const changeDir = path.join(cwd, relPath);
shell.cd(changeDir);
const fileList = await shellExec("git ls-files -s");
if (fileList.stdout.includes(symlinkMode)) {
const output = fileList.stdout.split("\n");
symlinks.push(...extractSymlinks(output, changeDir));
}
shell.cd(cwd);
return symlinks;
}
const shellExec = (cmd) =>
new Promise((resolve) => {
shell.exec(cmd, (code, stdout, stderr) => {
resolve({ code, stdout, stderr });
});
});
function extractSymlinks(output, cwd) {
const extractedSymlinks = [];
for (let file of output) {
// eslint-disable-next-line no-unused-vars
let [mode, id, _, relDir] = file.split(/\s/);
if (mode === symlinkMode) {
relDir = relDir.replaceAll("/", "\\");
const fileName = relDir.split("\\").pop();
const fileDir = path.join(cwd, relDir.replace(fileName, ""));
const linkText = shell
.cat(path.join(fileDir, fileName))
.toString()
.replaceAll("/", "\\");
extractedSymlinks.push({
mode: mode,
id: id,
link: linkText,
fileName: fileName,
target: fileDir,
});
}
}
return extractedSymlinks;
}
async function makeNewSymlink(symlink) {
shell.cd(symlink.target);
const output = await shellExec(
"mklink /d " + symlink.fileName + " " + symlink.link
);
console.log(output, symlink.target);
}
function deleteExistingSymlinks(symlinks) {
for (let symlink of symlinks) {
//if link has already been converted
if (symlink.link === "") continue;
shell.rm(path.join(symlink.target, symlink.fileName));
}
}
async function addSymlinks(symlinks) {
for (let symlink of symlinks) {
//if link has already been converted
if (symlink.link === "") continue;
await makeNewSymlink(symlink);
}
}
async function start() {
if (process.platform === "win32") {
const symlinks = await getSymlinks("../");
console.log("Removing existing symlinks");
deleteExistingSymlinks(symlinks);
console.log("Adding new symLinks");
await addSymlinks(symlinks);
console.log("All set up!");
} else console.log("You are not running windows, you should be A-Okay! ");
}
start();