add: elo data model and static rankings page
All checks were successful
Deploy on push / deploy (push) Has been skipped

This commit is contained in:
2026-03-22 20:18:28 -07:00
parent 8f9a7eda2f
commit 26adbe617f
11 changed files with 1071 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ const sharp = require("sharp");
const { buildPages } = require("./build");
const { buildThumbnails } = require("./generate-thumbnails");
const { eloPath } = require("./lib/elo");
const {
getNextMealId,
loadMeals,
@@ -15,6 +16,7 @@ const {
const fullsDir = path.join(repoRoot, "images", "fulls");
const thumbsDir = path.join(repoRoot, "images", "thumbs");
const indexPath = path.join(repoRoot, "index.html");
const rankingsPath = path.join(repoRoot, "rankings.html");
const manifestPath = path.join(thumbsDir, ".thumbs-manifest.json");
const FULL_IMAGE_QUALITY = 90;
@@ -123,28 +125,45 @@ async function writeFullImage(sourcePath, destinationPath) {
.toFile(destinationPath);
}
function readOptionalFile(filePath) {
return fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8") : null;
}
async function restoreOptionalFile(filePath, previousContents) {
if (previousContents === null) {
if (fs.existsSync(filePath)) {
await fs.promises.unlink(filePath);
}
return;
}
if (previousContents !== undefined) {
fs.writeFileSync(filePath, previousContents);
}
}
async function rollback({
createdFullPath,
createdThumbPath,
previousElo,
previousIndex,
previousManifest,
previousMeals,
previousRankings,
}) {
if (previousMeals !== undefined) {
fs.writeFileSync(mealsPath, previousMeals);
}
await restoreOptionalFile(eloPath, previousElo);
if (previousIndex !== undefined) {
fs.writeFileSync(indexPath, previousIndex);
}
if (previousManifest === null) {
if (fs.existsSync(manifestPath)) {
await fs.promises.unlink(manifestPath);
}
} else if (previousManifest !== undefined) {
fs.writeFileSync(manifestPath, previousManifest);
}
await restoreOptionalFile(rankingsPath, previousRankings);
await restoreOptionalFile(manifestPath, previousManifest);
if (createdThumbPath && fs.existsSync(createdThumbPath)) {
await fs.promises.unlink(createdThumbPath);
@@ -169,10 +188,10 @@ async function ingestMeal(options) {
}
const previousMeals = fs.readFileSync(mealsPath, "utf8");
const previousElo = readOptionalFile(eloPath);
const previousIndex = fs.readFileSync(indexPath, "utf8");
const previousManifest = fs.existsSync(manifestPath)
? fs.readFileSync(manifestPath, "utf8")
: null;
const previousRankings = readOptionalFile(rankingsPath);
const previousManifest = readOptionalFile(manifestPath);
await fs.promises.mkdir(fullsDir, { recursive: true });
@@ -185,9 +204,11 @@ async function ingestMeal(options) {
await rollback({
createdFullPath: fullPath,
createdThumbPath: thumbPath,
previousElo,
previousIndex,
previousManifest,
previousMeals,
previousRankings,
});
throw error;
}