]> git.r.bdr.sh - rbdr/blog/blobdiff - lib/archivers/gemlog.js
Add remote management
[rbdr/blog] / lib / archivers / gemlog.js
index 3e08be4de7ad4de6bfcd5b08f5ac6c14466a75f4..1bb0c10d6d31740c930c09e29436018c0abc142a 100644 (file)
@@ -1,66 +1,75 @@
-const { cp, mkdir, readdir, writeFile } = require('fs/promises');
-const { debuglog } = require('util');
-const { resolve, join } = require('path');
-const { rmIfExists } = require('../utils');
+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,
 
   debuglog: debuglog('blog'),
 
   buildUrl(id, slug) {
+
     return `./${id}/${slug}`;
   },
 
-  buildTitle (id, slug) {
+  buildTitle(id, slug) {
+
     const date = new Date(Number(id));
-    const shortDate = date.toISOString().split('T')[0]
+    const shortDate = date.toISOString().split('T')[0];
     const title = slug.split('-').join(' ');
-    return `${shortDate} ${title}`
+    return `${shortDate} ${title}`;
   },
 
   buildLink(id, slug) {
+
     return `=> ${internals.buildUrl(id,slug)} ${internals.buildTitle(id,slug)}`;
   }
 };
 
-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];
+      .filter((entry) => internals.kGeminiRe.test(entry))[0];
 
-      posts.push({ id, slug })
-  };
+    posts.push({ id, slug });
+  }
 
   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 rmIfExists(internals.kArchiveName);
+    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 cp(archiveDirectory, internals.kArchiveName, { recursive: true });
+    await cp(source, target, { recursive: true });
   }
-};
+}