]>
Commit | Line | Data |
---|---|---|
6cd62e79 RBR |
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'; | |
67fdfa7c BB |
6 | |
7 | const internals = { | |
67fdfa7c | 8 | debuglog: debuglog('blog'), |
67fdfa7c BB |
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 | */ | |
6cd62e79 | 20 | export default async function StaticGenerator(source, target, _) { |
67fdfa7c | 21 | |
6cd62e79 RBR |
22 | try { |
23 | await access(source, constants.R_OK); | |
67fdfa7c | 24 | |
6cd62e79 RBR |
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); | |
67fdfa7c | 29 | |
6cd62e79 | 30 | internals.debuglog(`Copying ${sourceAsset} to ${targetAsset}`); |
67fdfa7c | 31 | |
6cd62e79 RBR |
32 | if (entry.isDirectory()) { |
33 | await cp(sourceAsset, targetAsset, { recursive: true }); | |
10a76a5b RBR |
34 | } |
35 | else { | |
6cd62e79 | 36 | await cp(sourceAsset, targetAsset); |
67fdfa7c | 37 | } |
67fdfa7c BB |
38 | } |
39 | } | |
6cd62e79 RBR |
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 | } | |
10a76a5b | 48 | } |