]> git.r.bdr.sh - rbdr/blog/blame - lib/generators/static.js
Remove force rms and dependency on ncp
[rbdr/blog] / lib / generators / static.js
CommitLineData
67fdfa7c
BB
1'use strict';
2
d3f282a1 3const { access, cp } = require('fs/promises');
67fdfa7c 4const { join } = require('path');
d3f282a1
RBR
5const { debuglog } = require('util');
6const { rmIfExists } = require('../utils');
7const { kFileNotFoundError } = require('../constants');
67fdfa7c
BB
8
9const internals = {
67fdfa7c 10 debuglog: debuglog('blog'),
67fdfa7c
BB
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}`);
d3f282a1 27 await rmIfExists(assetsTarget);
67fdfa7c
BB
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}`);
d3f282a1 38 await cp(assetsSource, assetsTarget, { recursive: true });
67fdfa7c
BB
39 }
40 catch (error) {
d3f282a1 41 if (error.code === kFileNotFoundError) {
67fdfa7c
BB
42 internals.debuglog(`Skipping ${i}`);
43 continue;
44 }
45
46 throw error;
47 }
48 }
49};