-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ts
More file actions
371 lines (322 loc) · 12.2 KB
/
Copy pathinstall.ts
File metadata and controls
371 lines (322 loc) · 12.2 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env -S deno run --allow-all
import { dirname, fromFileUrl, join } from "@std/path";
const HOME = Deno.env.get("HOME")!;
const SCRIPT_DIR = dirname(fromFileUrl(import.meta.url));
const extraPaths: string[] = [];
function getEnv(): Record<string, string> {
const e = Deno.env.toObject();
if (extraPaths.length) e.PATH = [...extraPaths, e.PATH].join(":");
return e;
}
async function run(cmd: string, args: string[] = []): Promise<void> {
const { success } = await new Deno.Command(cmd, {
args,
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
env: getEnv(),
}).output();
if (!success) throw new Error(`Command failed: ${[cmd, ...args].join(" ")}`);
}
async function capture(cmd: string, args: string[] = []): Promise<string> {
try {
const { stdout } = await new Deno.Command(cmd, {
args,
stdout: "piped",
stderr: "null",
env: getEnv(),
}).output();
return new TextDecoder().decode(stdout).trim();
} catch {
return "";
}
}
async function installed(cmd: string): Promise<boolean> {
const { success } = await new Deno.Command("which", {
args: [cmd],
stdout: "null",
stderr: "null",
env: getEnv(),
}).output();
return success;
}
async function tryRun(cmd: string, args: string[]): Promise<boolean> {
try {
const { success } = await new Deno.Command(cmd, {
args,
stdout: "null",
stderr: "null",
env: getEnv(),
}).output();
return success;
} catch {
return false;
}
}
async function runScript(
url: string,
args: string[] = [],
shell = "sh",
): Promise<void> {
const script = await fetch(url).then((r) => r.text());
const proc = new Deno.Command(shell, {
args,
stdin: "piped",
stdout: "inherit",
stderr: "inherit",
env: getEnv(),
}).spawn();
const w = proc.stdin.getWriter();
await w.write(new TextEncoder().encode(script));
await w.close();
const { success } = await proc.status;
if (!success) throw new Error(`Script from ${url} failed`);
}
// ── apt packages ─────────────────────────────────────────────────────────────
const needed: string[] = [];
for (const pkg of ["fish", "unzip", "python3"]) {
if (!(await installed(pkg))) needed.push(pkg);
}
if (!(await tryRun("python3", ["-m", "pip", "--version"])))
needed.push("python3-pip");
if (!(await tryRun("python3", ["-m", "venv", "--help"])))
needed.push("python3-venv");
if (needed.length === 0) {
console.log(
"skip: fish, unzip, python3, python3-pip, python3-venv already installed",
);
} else {
console.log(`==> Installing: ${needed.join(" ")}`);
await run("sudo", ["apt-get", "update", "-q"]);
await run("sudo", ["apt-get", "install", "-y", ...needed]);
}
// ── Neovim ───────────────────────────────────────────────────────────────────
await Deno.mkdir(join(HOME, ".local", "bin"), { recursive: true });
if (await installed("nvim")) {
console.log("skip: nvim already installed");
} else {
console.log("==> Installing Neovim (AppImage)");
const release = await fetch(
"https://api.github.com/repos/neovim/neovim/releases/latest",
).then((r) => r.json());
const asset = release.assets.find(
(a: { name: string; browser_download_url: string }) =>
a.name === "nvim-linux-x86_64.appimage",
);
const nvimPath = join(HOME, ".local", "bin", "nvim");
const bytes = await fetch(asset.browser_download_url).then((r) =>
r.arrayBuffer(),
);
await Deno.writeFile(nvimPath, new Uint8Array(bytes));
await Deno.chmod(nvimPath, 0o755);
}
// ── Rust ─────────────────────────────────────────────────────────────────────
if (await installed("cargo")) {
console.log("skip: Rust already installed");
} else {
console.log("==> Installing Rust");
await runScript("https://sh.rustup.rs", ["-s", "--", "-y"]);
}
extraPaths.push(join(HOME, ".cargo", "bin"));
// ── cargo tools ──────────────────────────────────────────────────────────────
const cargoInstallTools: [string, string, string[]][] = [
["cargo-binstall", "cargo-binstall", ["--locked"]],
];
for (const [cmd, pkg, flags] of cargoInstallTools) {
if (await installed(cmd)) {
console.log(`skip: ${cmd} already installed`);
} else {
console.log(`==> Installing ${cmd}`);
await run("cargo", ["install", ...flags, pkg]);
}
}
const cargoBinstallTools: [string, string][] = [
["cargo-update", "cargo-update"],
["rg", "ripgrep"],
["atuin", "atuin"],
["zellij", "zellij"],
["fnm", "fnm"],
["just", "just"],
];
for (const [cmd, pkg] of cargoBinstallTools) {
if (await installed(cmd)) {
console.log(`skip: ${cmd} already installed`);
} else {
console.log(`==> Installing ${cmd}`);
await run("cargo", ["binstall", pkg]);
}
}
// ── Node.js via fnm ──────────────────────────────────────────────────────────
if (await installed("node")) {
console.log("skip: node already installed");
} else {
console.log("==> Installing latest Node.js via fnm");
const fnmEnv = await capture("fnm", ["env", "--shell", "bash"]);
for (const line of fnmEnv.split("\n")) {
const m = line.match(/^export (\w+)="([^"]*)"/);
if (!m) continue;
const [, key, val] = m;
if (key === "PATH") {
extraPaths.unshift(...val.split(":").filter(Boolean));
} else {
Deno.env.set(key, val);
}
}
await run("fnm", ["install", "--lts"]);
await run("fnm", ["default", "lts-latest"]);
}
// ── opencode ─────────────────────────────────────────────────────────────────
if (await installed("opencode")) {
console.log("skip: opencode already installed");
} else {
console.log("==> Installing opencode");
await runScript("https://opencode.ai/install", [], "bash");
}
// ── Nix ──────────────────────────────────────────────────────────────────────
if (await installed("nix")) {
console.log("skip: nix already installed");
} else {
console.log("==> Installing Nix (single-user)");
await runScript("https://nixos.org/nix/install", ["-s", "--", "--no-daemon"]);
}
// ── git config ───────────────────────────────────────────────────────────────
const gitEmail = await capture("git", ["config", "--global", "user.email"]);
const gitName = await capture("git", ["config", "--global", "user.name"]);
if (gitEmail && gitName) {
console.log("skip: git already configured");
} else {
console.log("==> Configuring git");
await run("git", [
"config",
"--global",
"user.email",
"hendrik.hamerlinck@hammernet.be",
]);
await run("git", ["config", "--global", "user.name", "Hendrik Hamerlinck"]);
}
// ── fish as default shell ────────────────────────────────────────────────────
const fishPath = await capture("which", ["fish"]);
if (Deno.env.get("SHELL") === fishPath) {
console.log("skip: fish is already the default shell");
} else {
console.log("==> Setting fish as default shell");
const shells = await Deno.readTextFile("/etc/shells");
if (
!shells
.split("\n")
.map((s) => s.trim())
.includes(fishPath)
) {
const proc = new Deno.Command("sudo", {
args: ["tee", "-a", "/etc/shells"],
stdin: "piped",
stdout: "null",
}).spawn();
const w = proc.stdin.getWriter();
await w.write(new TextEncoder().encode(fishPath + "\n"));
await w.close();
await proc.status;
}
await run("chsh", ["-s", fishPath]);
}
// ── link dotfiles ────────────────────────────────────────────────────────────
console.log("\n==> Linking dotfiles");
const dotfilesConfig = join(SCRIPT_DIR, ".config");
const targetConfig = join(HOME, ".config");
await Deno.mkdir(targetConfig, { recursive: true });
for await (const entry of Deno.readDir(dotfilesConfig)) {
const src = join(dotfilesConfig, entry.name);
const dest = join(targetConfig, entry.name);
let destExists = false;
try {
await Deno.lstat(dest);
destExists = true;
} catch {
// dest does not exist
}
if (destExists) {
const answer = prompt(`already exists: ${dest} — delete and relink? [y/N]`);
if (answer?.toLowerCase() === "y") {
await Deno.remove(dest, { recursive: true });
await Deno.symlink(src, dest);
console.log(`linked: ${dest} -> ${src}`);
} else {
console.log(`skipped: ${dest}`);
}
} else {
await Deno.symlink(src, dest);
console.log(`linked: ${dest} -> ${src}`);
}
}
// ── link agents skills ───────────────────────────────────────────────────────
const dotfilesSkills = join(SCRIPT_DIR, ".agents", "skills");
const targetAgentsSkills = join(HOME, ".agents", "skills");
await Deno.mkdir(join(HOME, ".agents"), { recursive: true });
let agentsSkillsExists = false;
try {
await Deno.lstat(targetAgentsSkills);
agentsSkillsExists = true;
} catch {
// does not exist
}
if (agentsSkillsExists) {
const answer = prompt(
`already exists: ${targetAgentsSkills} — delete and relink? [y/N]`,
);
if (answer?.toLowerCase() === "y") {
await Deno.remove(targetAgentsSkills, { recursive: true });
await Deno.symlink(dotfilesSkills, targetAgentsSkills);
console.log(`linked: ${targetAgentsSkills} -> ${dotfilesSkills}`);
} else {
console.log(`skipped: ${targetAgentsSkills}`);
}
} else {
await Deno.symlink(dotfilesSkills, targetAgentsSkills);
console.log(`linked: ${targetAgentsSkills} -> ${dotfilesSkills}`);
}
// ── opencode plugins ─────────────────────────────────────────────────
const opencodeTarget = join(targetConfig, "opencode");
const opencodePkg = join(opencodeTarget, "package.json");
let opencodeNeedsInstall = false;
try {
await Deno.lstat(opencodePkg);
opencodeNeedsInstall = true;
} catch {
// no package.json, skip
}
if (opencodeNeedsInstall) {
const nodeModules = join(opencodeTarget, "node_modules");
let modulesExist = false;
try {
await Deno.lstat(nodeModules);
modulesExist = true;
} catch {
// does not exist
}
if (modulesExist) {
console.log("skip: opencode plugins already installed");
} else {
console.log("==> Installing opencode plugins");
const { success } = await new Deno.Command("npm", {
args: ["install"],
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
env: getEnv(),
cwd: opencodeTarget,
}).output();
if (!success) throw new Error("npm install failed for opencode plugins");
}
}
// ── TLS certificate ──────────────────────────────────────────────────────────
const certSrc = join(SCRIPT_DIR, "certs", "hammerserver.crt");
const certDest = "/usr/local/share/ca-certificates/hammerserver.crt";
try {
await Deno.lstat(certSrc);
console.log("==> Installing TLS certificate");
await run("sudo", ["cp", certSrc, certDest]);
await run("sudo", ["update-ca-certificates"]);
} catch {
console.log("skip: no certificate found");
}
console.log("\nDone. Make sure ~/.local/bin is in your PATH for nvim.");