Skip to content

Commit 828cc1e

Browse files
feat: add installer for code
Signed-off-by: GitHub <[email protected]>
1 parent 8c0797e commit 828cc1e

File tree

5 files changed

+95
-8
lines changed

5 files changed

+95
-8
lines changed

bundle-installers.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs from "fs/promises";
2+
import path from "path";
3+
4+
const files = await fs.readdir(path.join(process.cwd(), "src/installers"));
5+
const installers = files.filter((file) => file.endsWith(".sh"));
6+
const map = new Map<string, string>();
7+
8+
for (const installer of installers) {
9+
const content = await fs.readFile(`src/installers/${installer}`, "utf-8");
10+
map.set(installer.replace(/\.sh$/, ""), content);
11+
}
12+
13+
const json = JSON.stringify([...map.entries()], null, 4);
14+
await fs.writeFile("src/installers/bundle.json", json);

src/features/installer/config/apps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const topApps = [
1010
image: "https://upload.wikimedia.org/wikipedia/commons/a/a0/Firefox_logo%2C_2019.svg",
1111
},
1212
{
13-
id: "vscode",
13+
id: "code",
1414
name: "Visual Studio Code",
1515
url: "https://code.visualstudio.com/",
1616
description:

src/features/script/actions/generator.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"use server";
22

3+
import installerMap from "@/installers/bundle.json";
34
import "server-only";
45

6+
const installers = new Map<string, string>(installerMap as [string, string][]);
7+
58
function getBanner() {
69
return `
710
____ _
@@ -25,6 +28,25 @@ export async function generateInstallScript(apps: string[]): Promise<string> {
2528
script += " exit 1;\n";
2629
script += "fi\n\n";
2730

31+
script += `native_install() {\n`;
32+
script += ` if command -v apt-get &> /dev/null; then\n`;
33+
script += ` apt-get install -y $1;\n`;
34+
script += ` return 0;\n`;
35+
script += ` elif command -v dnf &> /dev/null; then\n`;
36+
script += ` dnf install -y $1;\n`;
37+
script += ` return 0;\n`;
38+
script += ` elif command -v yum &> /dev/null; then\n`;
39+
script += ` yum install -y $1;\n`;
40+
script += ` return 0;\n`;
41+
script += ` elif command -v pacman &> /dev/null; then\n`;
42+
script += ` pacman -S --noconfirm $1;\n`;
43+
script += ` return 0;\n`;
44+
script += ` else\n`;
45+
script += ` echo "$me: No supported package manager found to install: $1";\n`;
46+
script += ` return 1;\n`;
47+
script += ` fi\n`;
48+
script += `}\n\n`;
49+
2850
script += "cat <<EOF\n";
2951
script += getBanner();
3052
script += "\nEOF\n\n";
@@ -47,12 +69,16 @@ export async function generateInstallScript(apps: string[]): Promise<string> {
4769
}
4870

4971
function getAppInstallCode(app: string): string | null {
50-
switch (app) {
51-
case "node":
52-
return "curl -fsSL https://deb.nodesource.com/setup_22.x | bash - && apt-get install -y nodejs";
53-
case "docker":
54-
return "curl -fsSL https://get.docker.com | bash -";
55-
default:
56-
return null;
72+
if (!installers.has(app)) {
73+
let script = `echo "$me: No installer found for app: ${app}";\n`;
74+
script += `echo "$me: Using native package manager to install: ${app}";\n`;
75+
script += `native_install "${app}";\n`;
76+
script += `if [ $? -ne 0 ]; then\n`;
77+
script += ` echo "$me: Failed to install: ${app}";\n`;
78+
script += ` exit 1;\n`;
79+
script += `fi\n`;
80+
return script;
5781
}
82+
83+
return installers.get(app) ?? null;
5884
}

src/installers/bundle.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
[
3+
"code",
4+
"#!/bin/sh\n\ninstall_apt() {\n apt-get install wget gpg\n wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/packages.microsoft.gpg\n install -D -o root -g root -m 644 /tmp/packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg\n echo \"deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main\" | tee /etc/apt/sources.list.d/vscode.list > /dev/null\n rm -f /tmp/packages.microsoft.gpg\n apt install apt-transport-https\n apt update\n apt install code\n}\n\nimport_yum() {\n rpm --import https://packages.microsoft.com/keys/microsoft.asc\n printf \"%s\" \"[code]\\nname=Visual Studio Code\\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\\nenabled=1\\ngpgcheck=1\\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc\" | tee /etc/yum.repos.d/vscode.repo > /dev/null\n}\n\ninstall_yum() {\n import_yum\n yum check-update\n yum install code\n}\n\ninstall_dnf() {\n import_yum\n dnf check-update\n dnf install code\n}\n\nif [ -x \"$(command -v apt-get)\" ]; then\n install_apt\nelif [ -x \"$(command -v yum)\" ]; then\n install_yum\nelif [ -x \"$(command -v dnf)\" ]; then\n install_dnf\nelse\n # shellcheck disable=SC2154\n echo \"$me: No supported package manager found to install: code\" >&2\n exit 1\nfi"
5+
]
6+
]

src/installers/code.sh

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/sh
2+
3+
code_install_install_apt() {
4+
apt-get install wget gpg
5+
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/packages.microsoft.gpg
6+
install -D -o root -g root -m 644 /tmp/packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg
7+
echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | tee /etc/apt/sources.list.d/vscode.list > /dev/null
8+
rm -f /tmp/packages.microsoft.gpg
9+
apt install apt-transport-https
10+
apt update
11+
apt install code
12+
}
13+
14+
code_install_import_yum() {
15+
rpm --import https://packages.microsoft.com/keys/microsoft.asc
16+
printf "%s" "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" | tee /etc/yum.repos.d/vscode.repo > /dev/null
17+
}
18+
19+
code_install_install_yum() {
20+
import_yum
21+
yum check-update
22+
yum install code
23+
}
24+
25+
code_install_install_dnf() {
26+
import_yum
27+
dnf check-update
28+
dnf install code
29+
}
30+
31+
if [ -x "$(command -v apt-get)" ]; then
32+
code_install_install_apt
33+
elif [ -x "$(command -v yum)" ]; then
34+
code_install_install_yum
35+
elif [ -x "$(command -v dnf)" ]; then
36+
code_install_install_dnf
37+
else
38+
# shellcheck disable=SC2154
39+
echo "$me: No supported package manager found to install: code" >&2
40+
exit 1
41+
fi

0 commit comments

Comments
 (0)