X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/65d379f5db381423165e0da8cc788f85112873b8..6cd62e795e3716aa0cbd2d1ff8c1b3a345803563:/lib/archivers/gemlog.js diff --git a/lib/archivers/gemlog.js b/lib/archivers/gemlog.js index 50bb334..d56a1f6 100644 --- a/lib/archivers/gemlog.js +++ b/lib/archivers/gemlog.js @@ -1,14 +1,13 @@ -const { mkdir, readdir, rm, writeFile } = require('fs/promises'); -const { debuglog, promisify } = require('util'); -const { ncp } = require('ncp'); -const { resolve, join } = require('path'); +import Dot from 'dot'; +import { cp, mkdir, readdir, readFile, writeFile } from 'fs/promises'; +import { debuglog } from 'util'; +import { join } from 'path'; +import { rmIfExists } from '../utils.js'; const internals = { - kArchiveName: resolve(join(__dirname, '../..', '.gemlog')), kIndexName: 'index.gmi', kGeminiRe: /\.gmi$/i, - ncp: promisify(ncp), debuglog: debuglog('blog'), buildUrl(id, slug) { @@ -27,13 +26,13 @@ const internals = { } }; -module.exports = async function(archiveDirectory) { - internals.debuglog(`Reading archive ${archiveDirectory}`); - const postIds = (await readdir(archiveDirectory)) +export default async function(templateDirectory, source, target) { + internals.debuglog(`Reading archive ${source}`); + const postIds = (await readdir(source)) .sort((a, b) => Number(b) - Number(a)); const posts = []; for (const id of postIds) { - const postDirectory = join(archiveDirectory, id); + const postDirectory = join(source, id); const slug = (await readdir(postDirectory)) .filter((entry) => internals.kGeminiRe.test(entry))[0]; @@ -42,26 +41,31 @@ module.exports = async function(archiveDirectory) { internals.debuglog(`Read ${posts.length} posts`); - const index = [ - '# Unlimited Pizza Gemlog Archive', '', - '=> https://blog.unlimited.pizza/feed.xml 📰 RSS Feed', - '=> https://blog.unlimited.pizza/index.txt 📑 http text version (latest 3 posts)', - '', - ...posts.map((post) => internals.buildLink(post.id, post.slug)), - '', '=> ../ 🪴 Back to main page' - ].join('\n'); + internals.debuglog('Generating Archive Index'); + const indexLocation = join(templateDirectory, internals.kIndexName); + + internals.debuglog(`Reading ${indexLocation}`); + const indexTemplate = await readFile(indexLocation, { encoding: 'utf8' }); + + internals.debuglog('Writing Archive Index'); + const index = Dot.template(indexTemplate, { + ...Dot.templateSettings, + strip: false + })({ + posts: posts.map((post) => internals.buildLink(post.id, post.slug)).join('\n'), + }); try { internals.debuglog('Removing index'); - await rm(internals.kArchiveName, { recursive: true }); + await rmIfExists(target); } finally { internals.debuglog('Creating index'); - await mkdir(internals.kArchiveName); - const indexFile = join(internals.kArchiveName, internals.kIndexName); + await mkdir(target); + const indexFile = join(target, internals.kIndexName); await writeFile(indexFile, index); internals.debuglog('Copying posts to archive'); - await internals.ncp(archiveDirectory, internals.kArchiveName); + await cp(source, target, { recursive: true }); } };