-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ts
executable file
·91 lines (76 loc) · 2.61 KB
/
install.ts
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
86
87
88
89
90
91
#!/usr/bin/env -S deno run -A
import { concat } from "jsr:@std/[email protected]/concat";
import { parseArgs } from "jsr:@std/[email protected]/parse-args";
import { $ } from "jsr:@david/[email protected]";
import symlinksJSON from "./symlinks.json" with { "type": "json" };
const encoder = new TextEncoder();
const gray = encoder.encode("\x1b[90m");
const red = encoder.encode("\x1b[31m");
const reset = encoder.encode("\x1b[0m");
const coloredWriter = (color: Uint8Array) =>
new WritableStream({
write(chunk) {
Deno.stdout.writeSync(concat([color, chunk, reset]));
},
});
if (import.meta.main) {
const args = parseArgs(Deno.args, {
boolean: ["sudo", "install-fonts"],
});
const canUseSudo = args["sudo"];
const canInstallFonts = args["install-fonts"];
$.setPrintCommand(true);
const home = $.path(Deno.env.get("HOME") || Deno.env.get("USERPROFILE")!);
home.join(".config").mkdirSync({ recursive: true });
const root = $.path(Deno.cwd());
const backupDir = root.join(".backup", `${Date.now()}`);
backupDir.join(".gitignore").writeTextSync("*");
const { os } = Deno.build;
const isLinux = os === "linux";
const isWSL2 = isLinux && Deno.env.get("WSL_DISTRO_NAME") !== undefined;
const isMac = os === "darwin";
const isWindows = os === "windows";
const symlinks = {
...symlinksJSON._common,
...isWSL2 ? symlinksJSON.wsl2 : {},
...isMac ? symlinksJSON.darwin : {},
...isWindows ? symlinksJSON.windows : {},
};
for (const [_target, _source] of Object.entries(symlinks)) {
const source = root.join(_source);
const target = $.path(_target.replace(/^~\//, `${home}/`));
const needsSudo = !target.startsWith(home);
if (!canUseSudo && needsSudo) {
$.log(`Symlink skipped (${_source} -> ${_target})`);
continue;
}
if (target.existsSync()) {
needsSudo
? await $`sudo mv ${target} ${backupDir}`
: await $`mv ${target} ${backupDir}`;
}
needsSudo
? await $`sudo ln -sfn ${source} ${target}`
: await $`ln -sfn ${source} ${target}`;
}
const packageManagerTask = ({
"linux": "apt",
"darwin": "brew",
"windows": "winget",
} as Record<string, string>)[Deno.build.os];
const tasks = [
...canUseSudo && packageManagerTask ? [packageManagerTask] : [],
"mise",
...canInstallFonts ? ["font"] : [],
];
$.log(`Tasks ${tasks} will be executed`);
// TODO: remove this
Deno.env.set("__STEP__", "echo");
for (const task of tasks) {
$.log(`Task: ${task}`);
const script = root.join(`_setup/${task}.sh`);
await $`bash ${script}`
.stdout(coloredWriter(gray))
.stderr(coloredWriter(red));
}
}