nyc-bookstores/index.js
David Ashby 7f4987d8d5
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
correct build script appending dates onto the CSS forever
2023-03-07 19:06:13 -05:00

50 lines
1.3 KiB
JavaScript

const cheerio = require("cheerio");
const fs = require("fs");
const stores = require("./stores.json");
function TableViewTemplate(rows) {
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>`;
}
fs.readFile("./index.html", function (err, data) {
if (err) {
throw err;
}
const $ = cheerio.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",
})
);
const cssurl = $("link[type='text/css']").attr("href").split("?")[0];
$("link[type='text/css']").attr("href", cssurl + "?" + new Date().getTime());
fs.writeFile("./index.html", $.html(), (err) => {
if (err) throw err;
console.log("Default view updated.");
});
});