]> git.r.bdr.sh - rbdr/blog/blame_incremental - lib/generators/static.js
Add self link
[rbdr/blog] / lib / generators / static.js
... / ...
CommitLineData
1'use strict';
2
3const { access, cp } = require('fs/promises');
4const { join } = require('path');
5const { debuglog } = require('util');
6const { rmIfExists } = require('../utils');
7const { kFileNotFoundError } = require('../constants');
8
9const 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 */
22module.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};