-
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathbootstrap.ts
More file actions
212 lines (201 loc) · 8.04 KB
/
bootstrap.ts
File metadata and controls
212 lines (201 loc) · 8.04 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
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import consola from "consola";
import { TServer } from "./server";
import { version } from "./package.json";
import open from "open";
import { exec } from "child_process";
import AdmZip from "adm-zip";
consola.info("Bootstrapping TerbiumOS [v" + version + "]");
export default async function Bootstrap() {
const args = process.argv;
const nodever = fs.readFileSync(".node_version", "utf-8").trim();
if (process.version < nodever) {
consola.warn("Your version of Node.JS is not supported. Please update node to use Terbium. (Current version: " + process.version + ", Required version: " + nodever + " or higher)");
}
await BuildApps();
await CreateAppsPaths();
if (!fs.existsSync(".env")) await CreateEnv();
await Updater();
consola.success("TerbiumOS bootstrapped successfully");
if (!(args.includes("--apps-only") || args.includes("--dev"))) {
TServer();
}
}
export async function BuildApps() {
consola.start("Building apps...");
const __dirname = path.dirname(fileURLToPath(import.meta.url)),
baseDir = path.join(__dirname, "./public/apps"),
outputDir = path.join(__dirname, "./src"),
outputJsonPath = path.join(outputDir, "apps.json"),
result: { name: string; config: any }[] = [];
function scanDirectory(dir: string) {
fs.readdirSync(dir, { withFileTypes: true }).forEach(i => {
if (i.isDirectory()) {
const indexFilePath = path.join(dir, i.name, "index.json");
if (fs.existsSync(indexFilePath)) {
try {
const data = JSON.parse(fs.readFileSync(indexFilePath, "utf-8"));
if (data.name && data.config) {
if (data.name !== "Browser") {
data.config.src = data.config.src.replace(`/apps/${data.name.toLowerCase()}.tapp/`, `/fs/apps/system/${data.name.toLowerCase()}.tapp/`);
data.config.icon = data.config.icon.replace(`/apps/${data.name.toLowerCase()}.tapp/`, `/fs/apps/system/${data.name.toLowerCase()}.tapp/`);
}
result.push({ name: data.name, config: data.config });
}
} catch (t) {
consola.error(`Error parsing ${indexFilePath}:`, t instanceof Error ? t.message : String(t));
}
}
} else if (i.name.endsWith(".tapp.zip")) {
const zipPath = path.join(dir, i.name);
try {
const zip = new AdmZip(zipPath);
const configEntry = zip.getEntry(".tbconfig");
if (configEntry) {
const configData = JSON.parse(configEntry.getData().toString("utf-8"));
if (configData.title && configData.wmArgs) {
result.push({ name: configData.title, config: configData.wmArgs });
}
}
} catch (t) {
consola.error(`Error reading ${zipPath}:`, t instanceof Error ? t.message : String(t));
}
}
});
}
scanDirectory(baseDir),
fs.existsSync(path.join(__dirname, "./build")) || fs.mkdirSync(path.join(__dirname, "./build")),
fs.existsSync(outputJsonPath) || fs.writeFileSync(outputJsonPath, "[]", "utf-8"),
fs.writeFileSync(outputJsonPath, JSON.stringify(result, null, 2), "utf-8"),
consola.success(`Aggregated JSON saved to ${outputJsonPath}`);
exec("git rev-parse HEAD", (error, stdout, stderr) => {
if (error || stderr) {
consola.error("Failed to get git commit hash");
fs.writeFileSync(path.join(__dirname, "./src/hash.json"), JSON.stringify({ hash: "2b14b5", repository: "terbiumos/web-v2" }, null, 2), "utf-8");
} else {
const hash = stdout.trim();
exec("git remote get-url origin", (remoteError, remoteStdout, remoteStderr) => {
const repoUrl = remoteStdout.trim();
const data = { hash, repository: repoUrl.replace("https://github.com/", "") };
if (remoteError || remoteStderr) {
consola.error("Failed to get repository URL");
fs.writeFileSync(path.join(__dirname, "./src/hash.json"), JSON.stringify({ hash: null, repository: null }, null, 2), "utf-8");
} else {
fs.writeFileSync(path.join(__dirname, "./src/hash.json"), JSON.stringify(data, null, 2), "utf-8");
consola.success(`Git hash and repo saved to ${path.join(__dirname, "./src/hash.json")}`);
}
});
}
});
return true;
}
export async function CreateAppsPaths() {
interface Apps {
[appName: string]: (string | { [path: string]: string[] })[];
}
consola.start("Creating apps paths...");
const __dirname = path.dirname(fileURLToPath(import.meta.url)),
baseDir = path.join(__dirname, "./public/apps"),
outputDir = path.join(__dirname, "./src"),
outputJsonPath = path.join(outputDir, "installer.json"),
output: string[] = [];
function collectPaths(dir: string, base: string = dir): void {
const files: fs.Dirent[] = fs.readdirSync(dir, { withFileTypes: true });
files.forEach((file: fs.Dirent) => {
const fullPath = path.join(dir, file.name);
const relativePath = path.relative(baseDir, fullPath).replace(/\\/g, "/");
if (file.isDirectory()) {
output.push(relativePath + "/");
collectPaths(fullPath, base);
} else {
output.push(relativePath);
}
});
}
const accmp: string[] = [];
fs.readdirSync(baseDir, { withFileTypes: true }).forEach(app => {
if (app.isDirectory() && app.name.toLocaleLowerCase().endsWith(".tapp")) {
const appPath = path.join(baseDir, app.name);
if (app.name.toLowerCase() === "settings.tapp") {
collectPaths(appPath);
if (fs.existsSync(path.join(appPath, "accounts"))) {
collectPaths(path.join(appPath, "accounts"));
accmp.push(...output.splice(output.indexOf("settings.tapp/accounts/")));
}
} else {
collectPaths(appPath);
}
} else if (app.name.toLowerCase().endsWith(".tapp.zip")) {
accmp.push(app.name);
}
});
output.push(...accmp);
fs.writeFileSync(outputJsonPath, JSON.stringify(output, null, 2), "utf-8");
consola.success(`Installer JSON saved to ${outputJsonPath}`);
return true;
}
export async function CreateEnv() {
const port =
(await consola.prompt("Enter a port for the server to run on (3000): ", {
type: "text",
default: "3000",
placeholder: "3000",
cancel: "default",
})) || 3000;
const masqr = await consola.prompt("Enable Masqr? (no): ", {
type: "text",
default: "false",
placeholder: "no",
cancel: "default",
});
if (masqr === "no" || masqr === "false" || masqr === "n") {
fs.writeFileSync(".env", `MASQR=${false}\nPORT=${port}`);
} else {
const licenseServer = (await consola.prompt("Enter the masqr license server URL: ")) || "";
const whitelist = (await consola.prompt("Enter a comma separated array of domains to whitelist (Ex: ['https://balls.com', 'https://tomp.app']): ")) || [];
fs.writeFileSync(".env", `MASQR=${true}\nPORT=${port}\nLICENSE_SERVER_URL=${licenseServer}\nWHITELISTED_DOMAINS=${whitelist}\n`);
}
consola.success("Environment file created");
return true;
}
export async function Updater() {
consola.start("Checking for updates...");
exec("git remote get-url origin", async (remoteError, remoteStdout, remoteStderr) => {
if (remoteError || remoteStderr) {
consola.error("Failed to get local repository URL");
return;
}
const repo = `https://raw.githubusercontent.com/${remoteStdout.trim().replace("https://github.com/", "").replace(".git", "")}/refs/heads/main/package.json` || "https://raw.githubusercontent.com/TerbiumOS/web-v2/refs/heads/main/package.json";
try {
const response = await fetch(repo);
const ver = (await response.json()).version;
if (ver > version) {
const res = await consola.prompt(`A new version of Terbium is available. Would you like to download it? (New Version: ${ver}, Current: ${version})`, {
type: "confirm",
});
if (res) {
consola.info("Downloading new version...");
exec("git pull", async (remoteError, remoteStdout, remoteStderr) => {
if (remoteError || remoteStderr) {
consola.error("Failed to update Terbium, Please update manually");
open(`${remoteStdout.trim()}/releases/latest`);
return;
}
consola.success("Terbium updated successfully");
await BuildApps();
await CreateAppsPaths();
});
return;
} else return;
} else {
consola.success("Terbium is up to date");
}
} catch (e) {
consola.error(`Failed to check for updates, ${e}`);
}
});
return true;
}
Bootstrap();