From a246f40d5de04c1c7fcc7385d36bc593bcee6ce6 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Fri, 1 Mar 2024 22:16:54 +0100 Subject: blog: Add timestamp to posts --- modules/blog/blog.js | 24 +++++------------------- modules/blog/template.js | 18 ++++++++++++++++++ modules/blog/util.js | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 modules/blog/util.js (limited to 'modules/blog') diff --git a/modules/blog/blog.js b/modules/blog/blog.js index a866003..3985fdb 100644 --- a/modules/blog/blog.js +++ b/modules/blog/blog.js @@ -4,6 +4,7 @@ const https = require("https"); const child_process = require("child_process"); const generateTemplate = require("./template.js"); +const blogUtil = require("./util.js"); let moddir = null; let repodir = null; @@ -50,32 +51,17 @@ function fetch(url) { }); } -function runCommand(cmd, args) { - console.log(`blog: ${cmd} ${JSON.stringify(args)}`); - child_process.execFileSync( - cmd, args, - { stdio: "inherit", timeout: 20000 } - ); -} - -function runCommandOutput(cmd, args) { - return child_process.execFileSync( - cmd, args, - { timeout: 20000 } - ); -} - function ensureRepo() { try { const stats = fs.statSync(repodir); if (stats.isDirectory()) return; } catch (e) {} - runCommand("git", ["clone", "https://git.tomsmeding.com/blog", repodir]); + blogUtil.runCommand("git", ["clone", "https://git.tomsmeding.com/blog", repodir]); } function currentCommit() { - return runCommandOutput("git", ["-C", repodir, "rev-parse", "HEAD"]).toString().trim(); + return blogUtil.runCommandOutput("git", ["-C", repodir, "rev-parse", "HEAD"], { silent: true }).toString().trim(); } async function upstreamCommit() { @@ -85,8 +71,8 @@ async function upstreamCommit() { function updateRepo() { try { - runCommand("git", ["-C", repodir, "fetch", "--all"]); - runCommand("git", ["-C", repodir, "reset", "origin/master", "--hard"]); + blogUtil.runCommand("git", ["-C", repodir, "fetch", "--all"]); + blogUtil.runCommand("git", ["-C", repodir, "reset", "origin/master", "--hard"]); // Reset cache _after_ the commands succeeded; if anything failed, we // might at least still have stale cache data templateCache = new Map(); diff --git a/modules/blog/template.js b/modules/blog/template.js index 32d7bb3..a1d4d3d 100644 --- a/modules/blog/template.js +++ b/modules/blog/template.js @@ -1,5 +1,7 @@ const fs = require("fs").promises; +const blogUtil = require("./util.js"); + const pathRoot = "blog"; async function recursiveTree(dir) { @@ -39,6 +41,21 @@ function generateTree(tree, path) { return out; } +// path: file path inside repo +async function generateDatetime(repodir, path) { + if (path == "index.html") return ""; + + const stat = await fs.stat(repodir + "/" + path); + const gitout = await blogUtil.runCommandOutput("git", ["-C", repodir, "log", "--format=format:%as", "--", path]); + const lines = gitout.toString().trim().split("\n"); + const last = lines[0]; + const first = lines[lines.length - 1]; + if (last == first) + return `Last updated: `; + else + return `Created: , last updated: `; +} + async function template(repodir, contentPath) { const tree = await recursiveTree(repodir); tree.delete("$template"); @@ -50,6 +67,7 @@ async function template(repodir, contentPath) { html = html.replace("", generateTree(tree, pathRoot)); html = html.replace("", content); + html = html.replace("", await generateDatetime(repodir, contentPath)) return html; } diff --git a/modules/blog/util.js b/modules/blog/util.js new file mode 100644 index 0000000..933ff30 --- /dev/null +++ b/modules/blog/util.js @@ -0,0 +1,24 @@ +const child_process = require("child_process"); + + +function runCommand(cmd, args) { + console.log(`blog: ${cmd} ${JSON.stringify(args)}`); + child_process.execFileSync( + cmd, args, + { stdio: "inherit", timeout: 20000 } + ); +} + +function runCommandOutput(cmd, args, opts) { + if (opts == null) opts = {}; + if (!opts.silent) console.log(`blog: ${cmd} ${JSON.stringify(args)}`); + return child_process.execFileSync( + cmd, args, + { timeout: 20000 } + ); +} + +module.exports = { + runCommand, + runCommandOutput, +}; -- cgit v1.2.3-70-g09d2