]> git.r.bdr.sh - rbdr/blog/blame - lib/generators/txt.js
Use modules, use XDG dirs
[rbdr/blog] / lib / generators / txt.js
CommitLineData
5f31ea34
BB
1'use strict';
2
6cd62e79
RBR
3import Dot from 'dot';
4import { readFile, writeFile } from 'fs/promises';
5import { join } from 'path';
6import { debuglog } from 'util';
5f31ea34
BB
7
8const internals = {
9 debuglog: debuglog('blog'),
10
11 kTextName: 'index.txt'
12};
13
14/**
15 * Generates a TXT version of the blog
16 *
17 * @name TXTGenerator
18 * @param {string} source the source directory
19 * @param {string} target the target directory
20 * @param {Array.<Blog.tPost>} posts the list of posts
21 */
6cd62e79 22export default async function TXTGenerator(source, target, posts) {
5f31ea34
BB
23
24 internals.debuglog('Generating TXT');
25 const textTarget = join(target, internals.kTextName);
26 const textLocation = join(source, internals.kTextName);
27
28 internals.debuglog(`Reading ${textLocation}`);
29 const textTemplate = await readFile(textLocation, { encoding: 'utf8' });
30
31 internals.debuglog('Writing TXT');
6cd62e79
RBR
32 const text = Dot.template(textTemplate, {
33 ...Dot.templateSettings,
5f31ea34
BB
34 strip: false
35 })({ posts });
36 await writeFile(textTarget, text);
37};