Add in-app update checker and one-click updater

Settings now shows when a newer version is available, with the real
version number read from the remote's package.json (via the git
binary already required for this deployment model, so it works
against any remote, not just GitHub), and an "Uppdatera nu" button
that stops the server, pulls, reinstalls dependencies and restarts
automatically - warning explicitly if a race is currently active.
The existing app-version poller already reloads the page once a
restart is detected, so no new client-side polling was needed.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
larssand
2026-09-09 22:05:13 +02:00
parent 2bbbd6e995
commit 4c463485e0
9 changed files with 335 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const { spawn, spawnSync } = require("child_process");
const ROOT = path.join(__dirname, "..");
const DATA_DIR = path.join(ROOT, "data");
@@ -8,6 +8,7 @@ const LOG_DIR = path.join(ROOT, "logs");
const PID_FILE = path.join(DATA_DIR, "server.pid");
const OUT_LOG = path.join(LOG_DIR, "server.out.log");
const ERR_LOG = path.join(LOG_DIR, "server.err.log");
const UPDATE_LOG = path.join(LOG_DIR, "update.log");
fs.mkdirSync(DATA_DIR, { recursive: true });
fs.mkdirSync(LOG_DIR, { recursive: true });
@@ -22,6 +23,8 @@ if (command === "start") {
restart();
} else if (command === "status") {
status();
} else if (command === "update") {
update();
} else {
console.error(`Unknown command: ${command}`);
process.exit(1);
@@ -97,6 +100,48 @@ async function restart() {
start();
}
async function update() {
const log = (line) => {
const stamped = `[${new Date().toISOString()}] ${line}\n`;
process.stdout.write(stamped);
try {
fs.appendFileSync(UPDATE_LOG, stamped);
} catch {
// best-effort - the console line above is the fallback record
}
};
log("Stopping server for update...");
await stop();
// --ff-only refuses to update rather than creating a merge commit or hitting a conflict a
// trackside user has no way to resolve - if the local branch has diverged, this is meant to
// fail loudly (and still start the server back up with the code that was already there) so a
// human looks at it, not silently rewrite history on a live installation.
log("Pulling latest code (git pull --ff-only)...");
const pull = spawnSync("git", ["pull", "--ff-only"], { cwd: ROOT, encoding: "utf8" });
log(pull.stdout || "");
if (pull.stderr) log(pull.stderr);
if (pull.status !== 0) {
log("git pull failed or was not a fast-forward - starting the server back up with the code already installed, no changes made.");
start();
return;
}
log("Installing dependencies (npm install)...");
const npmCommand = process.platform === "win32" ? "npm.cmd" : "npm";
const install = spawnSync(npmCommand, ["install", "--no-audit", "--no-fund"], { cwd: ROOT, encoding: "utf8" });
log(install.stdout || "");
if (install.stderr) log(install.stderr);
if (install.status !== 0) {
log("npm install failed - starting the server anyway with whatever dependencies are currently installed.");
}
log("Starting server...");
start();
log("Update finished.");
}
function status() {
const pid = readPid();
if (pid && isRunning(pid)) {