78 lines
1.9 kB
1
// Janky script to get around pnpm link issues
2
// Probably don't use this. Probably
3
/* eslint-disable no-console */
4
const fs = require("fs");
5
const path = require("path");
6
const child_process = require("child_process");
7
8
const cwd = process.cwd();
9
const onDisk = {
10
//"@moonlight-mod/lunast": "../lunast",
11
//"@moonlight-mod/moonmap": "../moonmap",
12
"@moonlight-mod/mappings": "../mappings"
13
};
14
15
function exec(cmd, dir) {
16
child_process.execSync(cmd, { cwd: dir, stdio: "inherit" });
17
}
18
19
function getDeps(packageJSON) {
20
const ret = {};
21
Object.assign(ret, packageJSON.dependencies || {});
22
Object.assign(ret, packageJSON.devDependencies || {});
23
Object.assign(ret, packageJSON.peerDependencies || {});
24
return ret;
25
}
26
27
function link(dir) {
28
const packageJSONPath = path.join(dir, "package.json");
29
if (!fs.existsSync(packageJSONPath)) return;
30
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, "utf8"));
31
const deps = getDeps(packageJSON);
32
33
for (const [dep, relativePath] of Object.entries(onDisk)) {
34
const fullPath = path.join(cwd, relativePath);
35
if (deps[dep]) {
36
exec(`pnpm link ${fullPath}`, dir);
37
}
38
}
39
}
40
41
function undo(dir) {
42
exec("pnpm unlink", dir);
43
try {
44
if (fs.existsSync(path.join(dir, "pnpm-lock.yaml"))) {
45
exec("git restore pnpm-lock.yaml", dir);
46
}
47
} catch {
48
// ignored
49
}
50
}
51
52
const shouldUndo = process.argv.includes("--undo");
53
const packages = fs.readdirSync("./packages");
54
55
for (const path of Object.values(onDisk)) {
56
console.log(path);
57
if (shouldUndo) {
58
undo(path);
59
} else {
60
link(path);
61
}
62
}
63
64
if (shouldUndo) {
65
console.log(cwd);
66
undo(cwd);
67
for (const pkg of packages) {
68
const dir = path.join(cwd, "packages", pkg);
69
console.log(dir);
70
undo(dir);
71
}
72
} else {
73
for (const pkg of packages) {
74
const dir = path.join(cwd, "packages", pkg);
75
console.log(dir);
76
link(dir);
77
}
78
}
79