aboutsummaryrefslogtreecommitdiff
path: root/lib/archivers/gemlog.js
blob: d50d411bc8af23a4ff7777181cabc7c72cb3bcfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const { mkdir, readdir, rm, writeFile } = require('fs/promises');
const { debuglog, promisify } = require('util');
const { ncp } = require('ncp');
const { join } = require('path');

const internals = {
  kArchiveName: '.gemlog',
  kIndexName: 'index.gmi',
  kGeminiRe: /\.gmi$/i,

  ncp: promisify(ncp),
  debuglog: debuglog('blog'),
};

module.exports = async function(archiveDirectory) {
  internals.debuglog(`Reading archive ${archiveDirectory}`);
  const postIds = await readdir(archiveDirectory)
  const posts = [];
  for (const id of postIds) {
    const postDirectory = join(archiveDirectory, id);
    const slug = (await readdir(postDirectory))
        .filter((entry) => internals.kGeminiRe.test(entry))[0];

      posts.push({ id, slug })
  };

  internals.debuglog(`Read ${posts.length} posts`);

  const index = ['# Unlimited Pizza Gemlog Archive', '',
    ...posts.map((post) => `=> ./${post.id}/${post.slug}`)].join('\n');

  try {
    internals.debuglog('Removing index');
    await rm(internals.kArchiveName, { recursive: true });
  }
  finally {
    internals.debuglog('Creating index');
    await mkdir(internals.kArchiveName);
    const indexFile = join(internals.kArchiveName, internals.kIndexName);
    await writeFile(indexFile, index);

    internals.debuglog('Copying posts to archive');
    await internals.ncp(archiveDirectory, internals.kArchiveName);
  }
};