]>
git.r.bdr.sh - rbdr/blog/blob - lib/archivers/gemlog.js
d50d411bc8af23a4ff7777181cabc7c72cb3bcfd
1 const { mkdir
, readdir
, rm
, writeFile
} = require('fs/promises');
2 const { debuglog
, promisify
} = require('util');
3 const { ncp
} = require('ncp');
4 const { join
} = require('path');
7 kArchiveName: '.gemlog',
8 kIndexName: 'index.gmi',
12 debuglog: debuglog('blog'),
15 module
.exports
= async
function(archiveDirectory
) {
16 internals
.debuglog(`Reading archive ${archiveDirectory}`);
17 const postIds
= await
readdir(archiveDirectory
)
19 for (const id
of postIds
) {
20 const postDirectory
= join(archiveDirectory
, id
);
21 const slug
= (await
readdir(postDirectory
))
22 .filter((entry
) => internals
.kGeminiRe
.test(entry
))[0];
24 posts
.push({ id
, slug
})
27 internals
.debuglog(`Read ${posts.length} posts`);
29 const index
= ['# Unlimited Pizza Gemlog Archive', '',
30 ...posts
.map((post
) => `=> ./${post.id}/${post.slug}`)].join('\n');
33 internals
.debuglog('Removing index');
34 await
rm(internals
.kArchiveName
, { recursive: true });
37 internals
.debuglog('Creating index');
38 await
mkdir(internals
.kArchiveName
);
39 const indexFile
= join(internals
.kArchiveName
, internals
.kIndexName
);
40 await
writeFile(indexFile
, index
);
42 internals
.debuglog('Copying posts to archive');
43 await internals
.ncp(archiveDirectory
, internals
.kArchiveName
);