]> git.r.bdr.sh - rbdr/blog/blame - lib/generators/static.js
Use new generators
[rbdr/blog] / lib / generators / static.js
CommitLineData
67fdfa7c
BB
1'use strict';
2
3const { access, rmdir } = require('fs/promises');
4const { ncp } = require('ncp');
5const { join } = require('path');
6const { debuglog, promisify } = require('util');
7
8const internals = {
9 ncp: promisify(ncp),
10 debuglog: debuglog('blog'),
11
12 kAssetsDirectoryName: 'assets'
13};
14
15/**
16 * Generates the static assets required for the blog
17 *
18 * @name StaticGenerator
19 * @param {string} source the source directory
20 * @param {string} target the target directory
21 * @param {Array.<Blog.tPost>} posts the list of posts
22 */
23module.exports = async function StaticGenerator(source, target, posts) {
24
25 const assetsTarget = join(target, internals.kAssetsDirectoryName);
26
27 internals.debuglog(`Removing ${assetsTarget}`);
28 await rmdir(assetsTarget, { recursive: true });
29
30 for (let i = 0; i < posts.length; ++i) {
31 const postSourcePath = join(source, `${i}`);
32
33 try {
34 await access(postSourcePath);
35
36 const assetsSource = join(postSourcePath, internals.kAssetsDirectoryName);
37
38 internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`);
39 await internals.ncp(assetsSource, assetsTarget);
40 }
41 catch (error) {
42 if (error.code === internals.kFileNotFoundError) {
43 internals.debuglog(`Skipping ${i}`);
44 continue;
45 }
46
47 throw error;
48 }
49 }
50};