From 1c32ed7e1a8568eac0fa5fe61576c293ca9171db Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 21 Nov 2025 16:08:21 +0100 Subject: [PATCH] Nix: emit revision file, bundle: use revision file, fix git dir resolution on logo --- flake.nix | 10 ++++++- src/bundle/start.ts | 64 +++++++++++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/flake.nix b/flake.nix index 461b49bb..49cb44e1 100644 --- a/flake.nix +++ b/flake.nix @@ -53,7 +53,14 @@ nativeBuildInputs = with pkgs; [ python3 ]; - installPhase = '' + installPhase = + let + revsFile = pkgs.writeText "spacebar-server-rev.json" (builtins.toJSON { + rev = self.sourceInfo.rev or self.sourceInfo.dirtyRev; + shortRev = self.sourceInfo.shortRev or self.sourceInfo.dirtyShortRev; + lastModified = self.sourceInfo.lastModified; + }); + in '' runHook preInstall # set -x @@ -65,6 +72,7 @@ echo "Installing package into $out" mkdir -p $out cp -r assets dist node_modules package.json $out/ + cp ${revsFile} $out/.rev # Create wrappers for start scripts echo "Creating wrappers for start scripts" diff --git a/src/bundle/start.ts b/src/bundle/start.ts index 26d62bdd..37c5b24c 100644 --- a/src/bundle/start.ts +++ b/src/bundle/start.ts @@ -30,42 +30,58 @@ import { config } from "dotenv"; config({ quiet: true }); import { execSync } from "child_process"; import { centerString, Logo } from "@spacebar/util"; +import fs from "fs"; +import path from "path"; const cores = process.env.THREADS ? parseInt(process.env.THREADS) : 1; -function getCommitOrFail() { +function getRevInfoOrFail(): { rev: string | null; lastModified: number } { + const rootDir = path.join(__dirname, "../../"); + // sanity check + if (!fs.existsSync(path.join(rootDir, "package.json"))) { + console.log(red("Error: Cannot find package.json in root directory. Are you running from the correct location?")); + } + + // use .rev file if it exists + if (fs.existsSync(path.join(__dirname, "../../.rev"))) { + return JSON.parse(fs.readFileSync(path.join(rootDir, ".rev"), "utf-8")); + } + + // fall back to invoking git try { - return execSync("git rev-parse HEAD").toString().trim(); + const rev = execSync(`git -C "${rootDir}" rev-parse HEAD`).toString().trim(); + const lastModified = Number(execSync(`git -C "${rootDir}" log -1 --format=%cd --date=unix`).toString().trim()); + return { + rev, + lastModified, + }; } catch (e) { - return null; + return { rev: null, lastModified: 0 }; } } if (cluster.isPrimary) { - const commit = getCommitOrFail(); - Logo.printLogo().then(()=>{ + const revInfo = getRevInfoOrFail(); + Logo.printLogo().then(() => { const unformatted = `spacebar-server | !! Pre-release build !!`; const formatted = `${blueBright("spacebar-server")} | ${redBright("⚠️ Pre-release build ⚠️")}`; - console.log( - bold(centerString(unformatted, 86).replace(unformatted, formatted)), - ); + console.log(bold(centerString(unformatted, 86).replace(unformatted, formatted))); - const unformattedGitHeader = `Commit Hash: ${commit !== null ? `${commit} (${commit.slice(0, 7)})` : "Unknown (Git cannot be found)"}`; - const formattedGitHeader = `Commit Hash: ${commit !== null ? `${cyan(commit)} (${yellow(commit.slice(0, 7))})` : "Unknown (Git cannot be found)"}`; - console.log( - bold( - centerString(unformattedGitHeader, 86).replace( - unformattedGitHeader, - formattedGitHeader, - ), - ), - ); - console.log(`Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)`); + const shortRev = revInfo.rev ? revInfo.rev.slice(0, 7) : "unknown"; + const unformattedRevisionHeader = `Commit Hash: ${revInfo.rev !== null ? `${revInfo.rev} (${shortRev})` : "Unknown"}`; + const formattedRevisionHeader = `Commit Hash: ${revInfo.rev !== null ? `${cyan(revInfo.rev)} (${yellow(shortRev)})` : "Unknown"}`; + console.log(bold(centerString(unformattedRevisionHeader, 86).replace(unformattedRevisionHeader, formattedRevisionHeader))); - if (commit == null) { - console.log(yellow(`Warning: Git is not installed or not in PATH.`)); + const modifiedTime = new Date(revInfo.lastModified * 1000); + const unformattedLastModified = `Last Updated: ${revInfo.lastModified !== 0 ? `${modifiedTime.toUTCString()}` : "Unknown"}`; + const formattedLastModified = `Last Updated: ${revInfo.lastModified !== 0 ? `${cyan(modifiedTime.toUTCString())}` : "Unknown"}`; + console.log(bold(centerString(unformattedLastModified, 86).replace(unformattedLastModified, formattedLastModified))); + + if (revInfo.rev == null) { + console.log(yellow(`Warning: Git is not installed or not in PATH, or the server is not running from a Git repository.`)); } + console.log(`Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)`); initStats(); console.log(`[Process] Starting with ${cores} threads`); @@ -94,11 +110,7 @@ if (cluster.isPrimary) { }); cluster.on("exit", (worker) => { - console.log( - `[Worker] ${red( - `PID ${worker.process.pid} died, restarting ...`, - )}`, - ); + console.log(`[Worker] ${red(`PID ${worker.process.pid} died, restarting ...`)}`); cluster.fork(); }); }