]>
Commit | Line | Data |
---|---|---|
67fdfa7c BB |
1 | 'use strict'; |
2 | ||
6cd62e79 RBR |
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'; | |
67fdfa7c BB |
8 | |
9 | const 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 | */ | |
6cd62e79 | 22 | export default async function StaticGenerator(source, target, _) { |
67fdfa7c | 23 | |
6cd62e79 RBR |
24 | try { |
25 | await access(source, constants.R_OK); | |
67fdfa7c | 26 | |
6cd62e79 RBR |
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); | |
67fdfa7c | 31 | |
6cd62e79 | 32 | internals.debuglog(`Copying ${sourceAsset} to ${targetAsset}`); |
67fdfa7c | 33 | |
6cd62e79 RBR |
34 | if (entry.isDirectory()) { |
35 | await cp(sourceAsset, targetAsset, { recursive: true }); | |
36 | } else { | |
37 | await cp(sourceAsset, targetAsset); | |
67fdfa7c | 38 | } |
67fdfa7c BB |
39 | } |
40 | } | |
6cd62e79 RBR |
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 | } | |
67fdfa7c | 49 | }; |