summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/blog/blog.js24
-rw-r--r--modules/blog/template.js18
-rw-r--r--modules/blog/util.js24
3 files changed, 47 insertions, 19 deletions
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: <time datetime="${last}">${last}</time>`;
+ else
+ return `Created: <time datetime="${first}">${first}</time>, last updated: <time datetime="${last}">${last}</time>`;
+}
+
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("<!-- REPLACE TREE -->", generateTree(tree, pathRoot));
html = html.replace("<!-- REPLACE CONTENT -->", content);
+ html = html.replace("<!-- REPLACE DATETIME -->", 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,
+};