nyc-bookstores/index.js
David Ashby 18d7a3e7b1
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
add this fancy recent changes module
2023-10-11 21:37:28 -04:00

74 lines
1.9 KiB
JavaScript

import { load } from "cheerio";
import { readFile, writeFile } from "fs";
import process from "child_process";
import stores from "./stores.json" assert { type: "json" };
function GetRecentChanges() {
const res = process
.execSync('git log -4 --pretty=format:"%ct %s"')
.toString();
return res.split("\n");
}
function ChangeLog(logs) {
let res = "\n";
logs.forEach((l, i) => {
const s = l.split(" ");
const date = new Date(s[0] * 1000).toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
res = res + `<li>${date} - ${s.slice(1).join(" ")}</li>\n`;
});
return res;
}
function TableViewTemplate(rows) {
let table = "<table>";
rows.forEach((row, key) => {
row.rowNumber = key;
table = table + TableRowTemplate(row);
});
return table + "</table>";
}
function TableRowTemplate({ rowNumber, name, address, city }) {
return `
<tr id="${rowNumber}" class="spotRow">
<td class="name">${name}</td>
<td>${address}, ${city}</td>
</tr>`;
}
readFile("./index.html", function (err, data) {
const changeList = GetRecentChanges();
if (err) {
throw err;
}
const $ = load(data);
stores.sort(function (a, b) {
var aname = a.name.toLowerCase();
var bname = b.name.toLowerCase();
return aname === bname ? 0 : +(aname > bname) || -1;
});
$("#Stores").html(TableViewTemplate(stores));
$("#storeCount").html(stores.length);
$("#updatedOn").html(
new Date().toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
})
);
$("#changesList").html(ChangeLog(changeList));
const cssurl = $("link[type='text/css']").attr("href").split("?")[0];
$("link[type='text/css']").attr("href", cssurl + "?" + new Date().getTime());
writeFile("./index.html", $.html(), (err) => {
if (err) throw err;
console.log("Default view updated.");
});
});