]> git.r.bdr.sh - rbdr/blog/blob - lib/generators/static.js
60719e2ef5ee7e1fbe7159a92c321da7b1f6b7d9
[rbdr/blog] / lib / generators / static.js
1 'use strict';
2
3 import { access, cp, readdir } from 'fs/promises';
4 import { constants } from 'fs';
5 import { join } from 'path';
6 import { debuglog } from 'util';
7 import { kFileNotFoundError } from '../constants.js';
8
9 const 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 */
22 export default async function StaticGenerator(source, target, _) {
23
24 try {
25 await access(source, constants.R_OK);
26
27 const entries = await readdir(source, { withFileTypes: true });
28 for (const entry of entries) {
29 const sourceAsset = join(source, entry.name);
30 const targetAsset = join(target, entry.name);
31
32 internals.debuglog(`Copying ${sourceAsset} to ${targetAsset}`);
33
34 if (entry.isDirectory()) {
35 await cp(sourceAsset, targetAsset, { recursive: true });
36 } else {
37 await cp(sourceAsset, targetAsset);
38 }
39 }
40 }
41 catch (error) {
42 if (error.code === kFileNotFoundError) {
43 internals.debuglog(`No static directory found in ${source}`);
44 return;
45 }
46
47 throw error;
48 }
49 };