]> git.r.bdr.sh - rbdr/blog/blob - lib/generators/static.js
45f5a04ed1c1a763bc8e6b128ac5c6a80984934a
[rbdr/blog] / lib / generators / static.js
1 import { access, cp, readdir } from 'fs/promises';
2 import { constants } from 'fs';
3 import { join } from 'path';
4 import { debuglog } from 'util';
5 import { kFileNotFoundError } from '../constants.js';
6
7 const internals = {
8 debuglog: debuglog('blog'),
9 kAssetsDirectoryName: 'assets'
10 };
11
12 /**
13 * Generates the static assets required for the blog
14 *
15 * @name StaticGenerator
16 * @param {string} source the source directory
17 * @param {string} target the target directory
18 * @param {Array.<Blog.tPost>} posts the list of posts
19 */
20 export default async function StaticGenerator(source, target, _) {
21
22 try {
23 await access(source, constants.R_OK);
24
25 const entries = await readdir(source, { withFileTypes: true });
26 for (const entry of entries) {
27 const sourceAsset = join(source, entry.name);
28 const targetAsset = join(target, entry.name);
29
30 internals.debuglog(`Copying ${sourceAsset} to ${targetAsset}`);
31
32 if (entry.isDirectory()) {
33 await cp(sourceAsset, targetAsset, { recursive: true });
34 }
35 else {
36 await cp(sourceAsset, targetAsset);
37 }
38 }
39 }
40 catch (error) {
41 if (error.code === kFileNotFoundError) {
42 internals.debuglog(`No static directory found in ${source}`);
43 return;
44 }
45
46 throw error;
47 }
48 }