-
Notifications
You must be signed in to change notification settings - Fork 15
/
link.mjs
57 lines (53 loc) · 1.92 KB
/
link.mjs
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
import fs from "fs";
import path from "path";
import process from "process";
import prompts from "prompts";
const windowsInstructions = process.platform === "win32" ? ' Start with a drive letter ("C:\\").' : "";
const dataPath = (
await prompts({
type: "text",
name: "value",
format: (v) => v.replace(/\W*$/, "").trim(),
message: `Enter the full path to your Foundry data folder.${windowsInstructions}`,
})
).value;
if (!dataPath || !/\bData$/.test(dataPath)) {
console.error(`"${dataPath}" does not look like a Foundry data folder.`);
process.exit(1);
}
const dataPathStats = fs.lstatSync(dataPath, { throwIfNoEntry: false });
if (!dataPathStats?.isDirectory()) {
console.error(`No folder found at "${dataPath}"`);
process.exit(1);
}
const symlinkPath = path.resolve(dataPath, "modules", "starfinder-field-test-for-pf2e");
const symlinkStats = fs.lstatSync(symlinkPath, { throwIfNoEntry: false });
if (symlinkStats) {
const atPath = symlinkStats.isDirectory() ? "folder" : symlinkStats.isSymbolicLink() ? "symlink" : "file";
const proceed = (
await prompts({
type: "confirm",
name: "value",
initial: false,
message: `A "starfinder-field-test-for-pf2e" ${atPath} already exists in the "systems" subfolder. Replace with new symlink?`,
})
).value;
if (!proceed) {
console.log("Aborting.");
process.exit();
}
}
try {
if (symlinkStats?.isDirectory()) {
fs.rmSync(symlinkPath, { recursive: true, force: true });
} else if (symlinkStats) {
fs.unlinkSync(symlinkPath);
}
fs.symlinkSync(path.resolve(process.cwd(), "build"), symlinkPath);
} catch (error) {
if (error instanceof Error) {
console.error(`An error was encountered trying to create a symlink: ${error.message}`);
process.exit(1);
}
}
console.log(`Symlink successfully created at "${symlinkPath}"!`);