]> git.r.bdr.sh - rbdr/blog/blame_incremental - lib/generators/static.js
Use serde and time
[rbdr/blog] / lib / generators / static.js
... / ...
CommitLineData
1import { access, cp, readdir } from 'fs/promises';
2import { constants } from 'fs';
3import { join } from 'path';
4import { debuglog } from 'util';
5import { kFileNotFoundError } from '../constants.js';
6
7const 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 */
20export 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}