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