]> git.r.bdr.sh - rbdr/blog/blob - lib/generators/static.js
Escape ampersand in titles
[rbdr/blog] / lib / generators / static.js
1 'use strict';
2
3 const { access, cp } = require('fs/promises');
4 const { join } = require('path');
5 const { debuglog } = require('util');
6 const { rmIfExists } = require('../utils');
7 const { kFileNotFoundError } = require('../constants');
8
9 const internals = {
10 debuglog: debuglog('blog'),
11 kAssetsDirectoryName: 'assets'
12 };
13
14 /**
15 * Generates the static assets required for the blog
16 *
17 * @name StaticGenerator
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 */
22 module.exports = async function StaticGenerator(source, target, posts) {
23
24 const assetsTarget = join(target, internals.kAssetsDirectoryName);
25
26 internals.debuglog(`Removing ${assetsTarget}`);
27 await rmIfExists(assetsTarget);
28
29 for (let i = 0; i < posts.length; ++i) {
30 const postSourcePath = join(source, `${i}`);
31
32 try {
33 await access(postSourcePath);
34
35 const assetsSource = join(postSourcePath, internals.kAssetsDirectoryName);
36
37 internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`);
38 await cp(assetsSource, assetsTarget, { recursive: true });
39 }
40 catch (error) {
41 if (error.code === kFileNotFoundError) {
42 internals.debuglog(`Skipping ${i}`);
43 continue;
44 }
45
46 throw error;
47 }
48 }
49 };