]>
Commit | Line | Data |
---|---|---|
67fdfa7c BB |
1 | 'use strict'; |
2 | ||
d3f282a1 | 3 | const { access, cp } = require('fs/promises'); |
67fdfa7c | 4 | const { join } = require('path'); |
d3f282a1 RBR |
5 | const { debuglog } = require('util'); |
6 | const { rmIfExists } = require('../utils'); | |
7 | const { kFileNotFoundError } = require('../constants'); | |
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 | */ | |
22 | module.exports = async function StaticGenerator(source, target, posts) { | |
23 | ||
24 | const assetsTarget = join(target, internals.kAssetsDirectoryName); | |
25 | ||
26 | internals.debuglog(`Removing ${assetsTarget}`); | |
d3f282a1 | 27 | await rmIfExists(assetsTarget); |
67fdfa7c BB |
28 | |
29 | for (let i = 0; i < posts.length; ++i) { | |
30 | const postSourcePath = join(source, `${i}`); | |
31 | ||
32 | try { | |
33 | await access(postSourcePath); | |
34 | ||
35 | const assetsSource = join(postSourcePath, internals.kAssetsDirectoryName); | |
36 | ||
37 | internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`); | |
d3f282a1 | 38 | await cp(assetsSource, assetsTarget, { recursive: true }); |
67fdfa7c BB |
39 | } |
40 | catch (error) { | |
d3f282a1 | 41 | if (error.code === kFileNotFoundError) { |
67fdfa7c BB |
42 | internals.debuglog(`Skipping ${i}`); |
43 | continue; | |
44 | } | |
45 | ||
46 | throw error; | |
47 | } | |
48 | } | |
49 | }; |