diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2022-05-08 13:54:21 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2022-05-08 13:54:21 +0200 |
| commit | fac54389550aaab8bcb4ad1e6b0b1900fd8887d2 (patch) | |
| tree | a1a85902a90a0c542438bb22e5844d9b92069a4a /lib/archivers | |
| parent | 1ac49bad28800a79b25c1e22112e71018901d3ba (diff) | |
Add rudimentary gemlog archive support
Diffstat (limited to 'lib/archivers')
| -rw-r--r-- | lib/archivers/gemlog.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/archivers/gemlog.js b/lib/archivers/gemlog.js new file mode 100644 index 0000000..d50d411 --- /dev/null +++ b/lib/archivers/gemlog.js @@ -0,0 +1,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); + } +}; |