-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.ts
150 lines (123 loc) · 3.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import $ from "jsr:@david/dax@^0.42.0";
import * as fs from "jsr:@std/fs@^0.224.0";
import * as path from "jsr:@std/path@^0.224.0";
$.setPrintCommand(true);
const account = "mono0x";
if (
!(Deno.build.os === "linux" || Deno.build.os === "darwin" ||
Deno.build.os === "windows")
) {
console.error(`Unsupported OS: ${Deno.build.os}`);
Deno.exit(1);
}
const ci = Deno.env.get("GITHUB_ACTIONS") === "true";
const remoteContainers = Deno.env.get("REMOTE_CONTAINERS") === "true";
const wsl = !!Deno.env.get("WSL_DISTRO_NAME");
console.log(`
OS: ${Deno.build.os}
CI: ${ci}
Remote Containers: ${remoteContainers}
WSL: ${wsl}
`);
if (remoteContainers) {
await setupRemoteContainer();
Deno.exit(0);
}
switch (Deno.build.os) {
case "linux":
case "darwin":
await setupUnix(Deno.build.os);
break;
case "windows":
await setupWindows();
break;
}
async function setupRemoteContainer() {
console.log("Installing and initalizing chezmoi");
await $`sh -c "$(curl -fsLS get.chezmoi.io)" -- init --apply --verbose ${account}`;
}
async function setupUnix(os: "linux" | "darwin") {
const homeDir = Deno.env.get("HOME");
if (!homeDir) {
console.error("HOME environment variable not set");
Deno.exit(1);
}
if (os === "linux") {
console.log("Installing dependencies");
await aptInstall([
// https://docs.brew.sh/Homebrew-on-Linux
"build-essential",
"procps",
"curl",
"file",
"git",
"language-pack-en",
"libssl-dev",
"pkg-config",
"zsh",
]);
}
const scoopShimDir = await (async () => {
if (!wsl) return;
const scoop = await $.which("scoop");
return scoop && path.dirname(scoop);
})();
const homebrewDir = os === "linux"
? "/home/linuxbrew/.linuxbrew"
: "/opt/homebrew";
const brew = path.join(homebrewDir, "bin", "brew");
console.log("Installing Homebrew");
if (!await fs.exists(brew)) {
await $`/bin/bash --noprofile --norc`
.stdinText(`
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
`);
}
console.log("Installing and initializing chezmoi");
await $`/bin/bash --noprofile --norc`
.stdinText(`
export PATH=$PATH:${scoopShimDir}
eval $("${brew}" shellenv)
HOMEBREW_NO_AUTO_UPDATE=1 "${brew}" install chezmoi
chezmoi init --verbose --apply ${account}
`)
.clearEnv()
.env({
GITHUB_ACTIONS: Deno.env.get("GITHUB_ACTIONS"),
HOME: homeDir,
});
}
async function setupWindows() {
const homeDir = Deno.env.get("USERPROFILE");
if (!homeDir) {
console.error("USERPROFILE environment variable not set");
Deno.exit(1);
}
console.log("Installing scoop");
const scoopShimDir = path.join(homeDir, "scoop", "shims");
if (!await fs.exists(scoopShimDir)) {
await $`pwsh -noprofile -noninteractive -`
.stdinText(`
Invoke-RestMethod get.scoop.sh | Invoke-Expression
scoop install chezmoi git
`);
}
console.log("Initializing chezmoi");
const chezmoi = path.join(scoopShimDir, "chezmoi.exe");
await $`pwsh -noprofile -noninteractive -`
.stdinText(`
. "${chezmoi}" init --verbose --apply ${account}
`);
}
async function aptInstall(packages: string[]) {
const installed = new Set(
(await $`dpkg --get-selections`.lines())
.map((l) => l.split(/\t+/))
.filter(([, v]) => v === "install")
.map(([k]) => k.includes(":") ? k.split(":")[0] : k),
);
const installs = packages.filter((p) => !installed.has(p));
if (installs.length > 0) {
await $`${Deno.uid() == 0 ? "" : "sudo"} apt-get install -y ${installs}`;
}
}