]>
Commit | Line | Data |
---|---|---|
67fdfa7c BB |
1 | 'use strict'; |
2 | ||
fac54389 | 3 | const { access, rm } = require('fs/promises'); |
67fdfa7c BB |
4 | const { ncp } = require('ncp'); |
5 | const { join } = require('path'); | |
6 | const { debuglog, promisify } = require('util'); | |
7 | ||
8 | const internals = { | |
9 | ncp: promisify(ncp), | |
10 | debuglog: debuglog('blog'), | |
11 | ||
12 | kAssetsDirectoryName: 'assets' | |
13 | }; | |
14 | ||
15 | /** | |
16 | * Generates the static assets required for the blog | |
17 | * | |
18 | * @name StaticGenerator | |
19 | * @param {string} source the source directory | |
20 | * @param {string} target the target directory | |
21 | * @param {Array.<Blog.tPost>} posts the list of posts | |
22 | */ | |
23 | module.exports = async function StaticGenerator(source, target, posts) { | |
24 | ||
25 | const assetsTarget = join(target, internals.kAssetsDirectoryName); | |
26 | ||
27 | internals.debuglog(`Removing ${assetsTarget}`); | |
24de2f06 | 28 | await rm(assetsTarget, { recursive: true, force: true }); |
67fdfa7c BB |
29 | |
30 | for (let i = 0; i < posts.length; ++i) { | |
31 | const postSourcePath = join(source, `${i}`); | |
32 | ||
33 | try { | |
34 | await access(postSourcePath); | |
35 | ||
36 | const assetsSource = join(postSourcePath, internals.kAssetsDirectoryName); | |
37 | ||
38 | internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`); | |
39 | await internals.ncp(assetsSource, assetsTarget); | |
40 | } | |
41 | catch (error) { | |
42 | if (error.code === internals.kFileNotFoundError) { | |
43 | internals.debuglog(`Skipping ${i}`); | |
44 | continue; | |
45 | } | |
46 | ||
47 | throw error; | |
48 | } | |
49 | } | |
50 | }; |