-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePages.ts
62 lines (53 loc) · 1.82 KB
/
generatePages.ts
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
62
import * as fs from 'fs';
import findInDir from './findInDir';
console.log('/// Beginning generation of pages');
const templates = findInDir('./templates', '.html');
const pages = findInDir('./pages', '.html');
pages.forEach((page) => {
console.log(`Processing ${page}`);
let pageContents = fs.readFileSync(page, 'utf8');
let pageName = page.replace('pages/', '').replace('.html', '');
if (pageName === 'index') {
pageName = 'home';
}
let description = "I'm James King, and I make things for the web";
switch (pageName) {
case 'thoughts':
description = 'A peak inside my brain you ask? Reader, beware...';
break;
case 'profile':
description =
"So who am I? I'm James King, a 27 year old Software Engineer from Lincolnshire. You want some more info?";
break;
case 'coding':
description = "Shall we take a look at some projects I've done?";
break;
case 'changelog':
description = 'What changes have happened to this site?';
break;
default:
// do nothing - already set description for home/index
}
templates.forEach((template) => {
const templateContents = fs
.readFileSync(template, 'utf8')
.replace(new RegExp(`{page}`, 'g'), pageName);
pageContents = pageContents
.replace(
new RegExp(`{${template.replace('templates/', '')}}`, 'g'),
templateContents
)
.replace(/{subpage}/g, '')
.replace(/{description}/g, description)
.replace(
/{age}/g,
new Number(
(new Date().getTime() - new Date('1992-05-21').getTime()) /
31536000000
).toFixed(0)
)
.replace(/{year}/g, new Date().getFullYear().toString());
});
fs.writeFileSync(page.replace('pages/', 'public/'), pageContents, 'utf8');
});
console.log('/// Finished generation of pages');