]>
Commit | Line | Data |
---|---|---|
fac54389 RBR |
1 | const { mkdir, readdir, rm, writeFile } = require('fs/promises'); |
2 | const { debuglog, promisify } = require('util'); | |
3 | const { ncp } = require('ncp'); | |
65d379f5 | 4 | const { resolve, join } = require('path'); |
fac54389 RBR |
5 | |
6 | const internals = { | |
65d379f5 | 7 | kArchiveName: resolve(join(__dirname, '../..', '.gemlog')), |
fac54389 RBR |
8 | kIndexName: 'index.gmi', |
9 | kGeminiRe: /\.gmi$/i, | |
10 | ||
11 | ncp: promisify(ncp), | |
12 | debuglog: debuglog('blog'), | |
65d379f5 RBR |
13 | |
14 | buildUrl(id, slug) { | |
15 | return `./${id}/${slug}`; | |
16 | }, | |
17 | ||
18 | buildTitle (id, slug) { | |
19 | const date = new Date(Number(id)); | |
20 | const shortDate = date.toISOString().split('T')[0] | |
21 | const title = slug.split('-').join(' '); | |
22 | return `${shortDate} ${title}` | |
23 | }, | |
24 | ||
25 | buildLink(id, slug) { | |
26 | return `=> ${internals.buildUrl(id,slug)} ${internals.buildTitle(id,slug)}`; | |
27 | } | |
fac54389 RBR |
28 | }; |
29 | ||
30 | module.exports = async function(archiveDirectory) { | |
31 | internals.debuglog(`Reading archive ${archiveDirectory}`); | |
65d379f5 RBR |
32 | const postIds = (await readdir(archiveDirectory)) |
33 | .sort((a, b) => Number(b) - Number(a)); | |
fac54389 RBR |
34 | const posts = []; |
35 | for (const id of postIds) { | |
36 | const postDirectory = join(archiveDirectory, id); | |
37 | const slug = (await readdir(postDirectory)) | |
38 | .filter((entry) => internals.kGeminiRe.test(entry))[0]; | |
39 | ||
40 | posts.push({ id, slug }) | |
41 | }; | |
42 | ||
43 | internals.debuglog(`Read ${posts.length} posts`); | |
44 | ||
65d379f5 RBR |
45 | const index = [ |
46 | '# Unlimited Pizza Gemlog Archive', '', | |
47 | '=> https://blog.unlimited.pizza/feed.xml ð° RSS Feed', | |
48 | '=> https://blog.unlimited.pizza/index.txt ð http text version (latest 3 posts)', | |
49 | '', | |
50 | ...posts.map((post) => internals.buildLink(post.id, post.slug)), | |
51 | '', '=> ../ ðŠī Back to main page' | |
52 | ].join('\n'); | |
fac54389 RBR |
53 | |
54 | try { | |
55 | internals.debuglog('Removing index'); | |
56 | await rm(internals.kArchiveName, { recursive: true }); | |
57 | } | |
58 | finally { | |
59 | internals.debuglog('Creating index'); | |
60 | await mkdir(internals.kArchiveName); | |
61 | const indexFile = join(internals.kArchiveName, internals.kIndexName); | |
62 | await writeFile(indexFile, index); | |
63 | ||
64 | internals.debuglog('Copying posts to archive'); | |
65 | await internals.ncp(archiveDirectory, internals.kArchiveName); | |
66 | } | |
67 | }; |