Nix: emit revision file, bundle: use revision file, fix git dir resolution on logo

This commit is contained in:
Rory& 2025-11-21 16:08:21 +01:00
parent a1f4cf8eca
commit 1c32ed7e1a
2 changed files with 47 additions and 27 deletions

View File

@ -53,7 +53,14 @@
nativeBuildInputs = with pkgs; [ nativeBuildInputs = with pkgs; [
python3 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 runHook preInstall
# set -x # set -x
@ -65,6 +72,7 @@
echo "Installing package into $out" echo "Installing package into $out"
mkdir -p $out mkdir -p $out
cp -r assets dist node_modules package.json $out/ cp -r assets dist node_modules package.json $out/
cp ${revsFile} $out/.rev
# Create wrappers for start scripts # Create wrappers for start scripts
echo "Creating wrappers for start scripts" echo "Creating wrappers for start scripts"

View File

@ -30,42 +30,58 @@ import { config } from "dotenv";
config({ quiet: true }); config({ quiet: true });
import { execSync } from "child_process"; import { execSync } from "child_process";
import { centerString, Logo } from "@spacebar/util"; import { centerString, Logo } from "@spacebar/util";
import fs from "fs";
import path from "path";
const cores = process.env.THREADS ? parseInt(process.env.THREADS) : 1; 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 { 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) { } catch (e) {
return null; return { rev: null, lastModified: 0 };
} }
} }
if (cluster.isPrimary) { if (cluster.isPrimary) {
const commit = getCommitOrFail(); const revInfo = getRevInfoOrFail();
Logo.printLogo().then(()=>{ Logo.printLogo().then(() => {
const unformatted = `spacebar-server | !! Pre-release build !!`; const unformatted = `spacebar-server | !! Pre-release build !!`;
const formatted = `${blueBright("spacebar-server")} | ${redBright("⚠️ Pre-release build ⚠️")}`; const formatted = `${blueBright("spacebar-server")} | ${redBright("⚠️ Pre-release build ⚠️")}`;
console.log( console.log(bold(centerString(unformatted, 86).replace(unformatted, formatted)));
bold(centerString(unformatted, 86).replace(unformatted, formatted)),
);
const unformattedGitHeader = `Commit Hash: ${commit !== null ? `${commit} (${commit.slice(0, 7)})` : "Unknown (Git cannot be found)"}`; const shortRev = revInfo.rev ? revInfo.rev.slice(0, 7) : "unknown";
const formattedGitHeader = `Commit Hash: ${commit !== null ? `${cyan(commit)} (${yellow(commit.slice(0, 7))})` : "Unknown (Git cannot be found)"}`; const unformattedRevisionHeader = `Commit Hash: ${revInfo.rev !== null ? `${revInfo.rev} (${shortRev})` : "Unknown"}`;
console.log( const formattedRevisionHeader = `Commit Hash: ${revInfo.rev !== null ? `${cyan(revInfo.rev)} (${yellow(shortRev)})` : "Unknown"}`;
bold( console.log(bold(centerString(unformattedRevisionHeader, 86).replace(unformattedRevisionHeader, formattedRevisionHeader)));
centerString(unformattedGitHeader, 86).replace(
unformattedGitHeader,
formattedGitHeader,
),
),
);
console.log(`Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)`);
if (commit == null) { const modifiedTime = new Date(revInfo.lastModified * 1000);
console.log(yellow(`Warning: Git is not installed or not in PATH.`)); 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(); initStats();
console.log(`[Process] Starting with ${cores} threads`); console.log(`[Process] Starting with ${cores} threads`);
@ -94,11 +110,7 @@ if (cluster.isPrimary) {
}); });
cluster.on("exit", (worker) => { cluster.on("exit", (worker) => {
console.log( console.log(`[Worker] ${red(`PID ${worker.process.pid} died, restarting ...`)}`);
`[Worker] ${red(
`PID ${worker.process.pid} died, restarting ...`,
)}`,
);
cluster.fork(); cluster.fork();
}); });
} }