]>
git.r.bdr.sh - rbdr/blog/blob - lib/generators/static.js
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');
10 debuglog: debuglog('blog'),
11 kAssetsDirectoryName: 'assets'
15 * Generates the static assets required for the blog
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
22 module
.exports
= async
function StaticGenerator(source
, target
, posts
) {
24 const assetsTarget
= join(target
, internals
.kAssetsDirectoryName
);
26 internals
.debuglog(`Removing ${assetsTarget}`);
27 await
rmIfExists(assetsTarget
);
29 for (let i
= 0; i
< posts
.length
; ++i
) {
30 const postSourcePath
= join(source
, `${i}`);
33 await
access(postSourcePath
);
35 const assetsSource
= join(postSourcePath
, internals
.kAssetsDirectoryName
);
37 internals
.debuglog(`Copying ${assetsSource} to ${assetsTarget}`);
38 await
cp(assetsSource
, assetsTarget
, { recursive: true });
41 if (error
.code
=== kFileNotFoundError
) {
42 internals
.debuglog(`Skipping ${i}`);