-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
303 lines (264 loc) · 12.9 KB
/
run.js
File metadata and controls
303 lines (264 loc) · 12.9 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
// SPDX-License-Identifier: PMPL-1.0-or-later
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell (hyperpolymath)
//
// run.js — Homoiconic, fault-tolerant, platform-independent run script
// for Game Server Admin (GSA)
//
// Usage:
// deno run --allow-all run.js # auto-detect and launch
// deno run --allow-all run.js --help # show usage
// deno run --allow-all run.js --reflect
// ─────────────────────────────────────────────────────────────────────────────
// REGISTRY — homoiconic data; the script reads this at runtime via reflect()
// ─────────────────────────────────────────────────────────────────────────────
const REGISTRY = {
identity: {
name: "game-server-admin",
display: "Game Server Admin (GSA)",
version: "0.3.0",
license: "PMPL-1.0-or-later",
repo: "https://github.com/hyperpolymath/game-server-admin",
},
launchers: {
bash: "play.sh", // existing homoiconic bash launcher
just: "Justfile",
},
binary: {
zig: "src/interface/ffi/zig-out/bin/gsa",
},
git: {
remote: "origin",
branch: "main",
mirrors: [],
},
capabilities: [
"reflect", // reads own source (homoiconic)
"detectPlatform", // OS, arch, display server
"checkGitSync", // fetch + ahead/behind + dirty check
"launchBash", // delegates to play.sh (existing homoiconic launcher)
"launchJust", // delegates to just run
"launchBinary", // runs zig-out/bin/gsa directly
"gitCycle", // add, commit, push, mirror
],
};
// ─────────────────────────────────────────────────────────────────────────────
// REFLECTION
// ─────────────────────────────────────────────────────────────────────────────
async function reflect() {
const path = new URL(import.meta.url).pathname;
const src = await Deno.readTextFile(path);
return { path, lines: src.split("\n").length, capabilities: REGISTRY.capabilities };
}
// ─────────────────────────────────────────────────────────────────────────────
// PLATFORM DETECTION
// ─────────────────────────────────────────────────────────────────────────────
async function detectPlatform() {
const os = Deno.build.os;
const arch = Deno.build.arch;
let display = "unknown";
if (os === "linux") {
if (Deno.env.get("WAYLAND_DISPLAY")) display = "wayland";
else if (Deno.env.get("DISPLAY")) display = "x11";
else display = "headless";
} else if (os === "darwin") display = "quartz";
else if (os === "windows") display = "win32";
const has = async (cmd) => {
try {
const p = new Deno.Command("which", { args: [cmd], stdout: "null", stderr: "null" });
return (await p.output()).success;
} catch { return false; }
};
return {
os, arch, display,
hasBash: os !== "windows",
hasJust: await has("just"),
hasDeno: await has("deno"),
};
}
// ─────────────────────────────────────────────────────────────────────────────
// SHARED HELPERS
// ─────────────────────────────────────────────────────────────────────────────
async function run(cmd, args) {
try {
const p = new Deno.Command(cmd, { args, stdout: "piped", stderr: "piped" });
const { code, stdout, stderr } = await p.output();
return {
ok: code === 0,
out: new TextDecoder().decode(stdout).trim(),
err: new TextDecoder().decode(stderr).trim(),
};
} catch (e) {
return { ok: false, out: "", err: e.message };
}
}
const c = { reset:"\x1b[0m", bold:"\x1b[1m", green:"\x1b[32m", yellow:"\x1b[33m", cyan:"\x1b[36m" };
const log = (m) => console.log(`${c.green}▶${c.reset} ${m}`);
const warn = (m) => console.warn(`${c.yellow}⚠${c.reset} ${m}`);
const head = (m) => console.log(`\n${c.bold}${c.cyan}${m}${c.reset}`);
// ─────────────────────────────────────────────────────────────────────────────
// GIT SYNC
// ─────────────────────────────────────────────────────────────────────────────
async function checkGitSync() {
const sync = { dirty: false, ahead: 0, behind: 0, branch: "main", fetchError: false };
const branch = await run("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
if (branch.ok) sync.branch = branch.out;
const fetch = await run("git", ["fetch", "--quiet", REGISTRY.git.remote]);
if (!fetch.ok) { sync.fetchError = true; warn("git fetch failed — offline"); }
const rl = await run("git", ["rev-list", "--left-right", "--count",
`${REGISTRY.git.remote}/${sync.branch}...HEAD`]);
if (rl.ok) {
const [b, a] = rl.out.split(/\s+/);
sync.behind = parseInt(b, 10) || 0;
sync.ahead = parseInt(a, 10) || 0;
}
const diff = await run("git", ["status", "--porcelain"]);
if (diff.ok && diff.out) sync.dirty = true;
const remotes = await run("git", ["remote", "-v"]);
if (remotes.ok) {
REGISTRY.git.mirrors = [...new Set(
remotes.out.split("\n")
.filter(l => l.includes("(push)") && !l.startsWith(REGISTRY.git.remote + "\t"))
.map(l => l.split("\t")[0])
)];
}
return sync;
}
// ─────────────────────────────────────────────────────────────────────────────
// LAUNCH
// ─────────────────────────────────────────────────────────────────────────────
async function launchBash() {
try {
await Deno.stat(REGISTRY.launchers.bash);
log(`Delegating to ${REGISTRY.launchers.bash} (homoiconic bash launcher)...`);
const p = new Deno.Command("bash", {
args: [REGISTRY.launchers.bash, "run"],
stdin: "inherit", stdout: "inherit", stderr: "inherit",
});
const child = p.spawn();
await child.status;
return true;
} catch { return false; }
}
async function launchJust(platform) {
if (!platform.hasJust) return false;
try {
await Deno.stat(REGISTRY.launchers.just);
log("Launching via: just run");
const p = new Deno.Command("just", {
args: ["run"], stdin: "inherit", stdout: "inherit", stderr: "inherit",
});
const child = p.spawn();
await child.status;
return true;
} catch { return false; }
}
async function launchBinary() {
try {
await Deno.stat(REGISTRY.binary.zig);
log(`Running binary: ${REGISTRY.binary.zig}`);
const p = new Deno.Command(REGISTRY.binary.zig, {
args: ["status"], stdin: "inherit", stdout: "inherit", stderr: "inherit",
});
const child = p.spawn();
await child.status;
return true;
} catch { return false; }
}
// ─────────────────────────────────────────────────────────────────────────────
// GIT CYCLE
// ─────────────────────────────────────────────────────────────────────────────
async function gitCycle(sync) {
head("── Git cycle ──");
const add = await run("git", ["add", "-A"]);
if (!add.ok) { warn("git add failed: " + add.err); return; }
const staged = await run("git", ["diff", "--cached", "--stat"]);
if (staged.out) {
const commit = await run("git", ["commit", "-m",
"chore: run.js launch cycle — auto-detected platform, git cycle"]);
if (commit.ok) log("Committed outstanding changes");
else { warn("Commit failed: " + commit.err); return; }
} else {
log("Nothing to commit");
}
// Push current branch
const pushBranch = await run("git", ["push", REGISTRY.git.remote, sync.branch]);
if (pushBranch.ok) log(`Pushed ${sync.branch} → ${REGISTRY.git.remote}`);
else warn("Push failed: " + pushBranch.err);
// Merge to main if not already there
const mainBranch = "main";
if (sync.branch !== mainBranch) {
log(`Merging ${sync.branch} → ${mainBranch}...`);
const checkout = await run("git", ["checkout", mainBranch]);
if (!checkout.ok) { warn("Could not switch to main: " + checkout.err); return; }
const merge = await run("git", ["merge", "--ff-only", sync.branch]);
if (merge.ok) {
log(`Fast-forward merged ${sync.branch} → ${mainBranch}`);
} else {
const mergeRegular = await run("git", ["merge", sync.branch,
"-m", `chore: merge ${sync.branch} → main`]);
if (!mergeRegular.ok) { warn("Merge failed: " + mergeRegular.err); return; }
log(`Merged ${sync.branch} → ${mainBranch}`);
}
const pushMain = await run("git", ["push", REGISTRY.git.remote, mainBranch]);
if (pushMain.ok) log(`Pushed ${mainBranch} → ${REGISTRY.git.remote}`);
else warn("Main push failed: " + pushMain.err);
}
// Push to mirrors
for (const mirror of REGISTRY.git.mirrors) {
const mp = await run("git", ["push", mirror, mainBranch]);
if (mp.ok) log(`Pushed to mirror: ${mirror}`);
else warn(`Mirror push failed (${mirror}): ${mp.err}`);
}
}
// ─────────────────────────────────────────────────────────────────────────────
// MAIN
// ─────────────────────────────────────────────────────────────────────────────
if (import.meta.main) {
const args = Deno.args;
if (args.includes("--help") || args.includes("-h")) {
console.log(`
${c.bold}${REGISTRY.identity.display} — run.js${c.reset}
${REGISTRY.identity.license} | ${REGISTRY.identity.repo}
Usage: deno run --allow-all run.js [OPTIONS]
Options:
--help, -h Show this help
--no-git Skip git sync check and post-launch git cycle
--no-launch Git cycle only
--reflect Print reflection data and exit
`);
Deno.exit(0);
}
if (args.includes("--reflect")) {
const r = await reflect();
console.log(JSON.stringify({ registry: REGISTRY, reflection: r }, null, 2));
Deno.exit(0);
}
const skipGit = args.includes("--no-git");
const doLaunch = !args.includes("--no-launch");
head(`${REGISTRY.identity.display} v${REGISTRY.identity.version}`);
const r = await reflect();
log(`Reflected: ${r.lines} lines, ${r.capabilities.length} capabilities`);
head("Platform");
const platform = await detectPlatform();
log(`OS: ${platform.os}/${platform.arch} / display: ${platform.display}`);
log(`just: ${platform.hasJust ? "available" : "not found"} | bash: ${platform.hasBash ? "yes" : "no"}`);
let sync = { dirty: false, ahead: 0, behind: 0, branch: "main", fetchError: false };
if (!skipGit) {
head("Git sync");
sync = await checkGitSync();
log(`Branch: ${sync.branch} | ahead: ${sync.ahead} | behind: ${sync.behind}`);
if (sync.dirty) warn("Working tree has uncommitted changes");
if (sync.behind > 0) warn(`${sync.behind} commit(s) behind remote`);
}
if (doLaunch) {
head("Launch");
let launched = false;
// play.sh is the preferred launcher — it's already homoiconic bash
launched = launched || await launchBash();
launched = launched || await launchJust(platform);
launched = launched || await launchBinary();
if (!launched) warn("No launch method available — check build / install tools");
}
if (!skipGit) await gitCycle(sync);
head("Done");
}