Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
61 lines (52 loc) · 1.5 KB
/
Copy pathbuild.js
File metadata and controls
61 lines (52 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const path = require('path');
const fs = require('fs');
const { directories: dirs } = require('./config.js');
const { getBundledCSS, getBundledJS } = require('./helpers/bundlers.js');
const parseAllFiles = require('./helpers/parseAllFiles.js');
const buildPage = require('./helpers/buildPage.js');
const removeOldFiles = async (directory = '') => {
try {
await fs.promises.access(path.join(dirs.content, directory));
} catch {
await fs.promises.rm(path.join(dirs.dist, directory), {
recursive: true,
});
return;
}
const content = await fs.promises.readdir(
path.join(dirs.content, directory)
);
const dist = await fs.promises.readdir(path.join(dirs.dist, directory));
for (const item of dist) {
const distPath = path.join(dirs.dist, directory, item);
if (
item.endsWith('.html') &&
!content.includes(item.replace('.html', '.md'))
) {
await fs.promises.unlink(distPath);
} else if (fs.statSync(distPath).isDirectory()) {
await removeOldFiles(path.join(directory, item));
}
}
};
const buildAssets = async () => {
await fs.promises.writeFile(
path.join(dirs.dist, 'styles.css'),
getBundledCSS()
);
await fs.promises.writeFile(
path.join(dirs.dist, 'scripts.js'),
getBundledJS()
);
};
const build = async () => {
await fs.promises.mkdir(dirs.dist, { recursive: true });
const pages = parseAllFiles();
for (const slug in pages) {
const page = pages[slug];
buildPage(page);
}
await Promise.all([removeOldFiles(), buildAssets()]);
return pages;
};
module.exports = build;