-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (49 loc) · 1.58 KB
/
index.js
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 fs from 'fs';
import path from 'path';
import vhtml from 'vhtml';
import copyDir from './utils/copyDir.js';
import app from './template/index.js';
const h = function (tagName, ...children) {
let options = {};
if (typeof children[0] === 'object') {
options = children[0];
children = children.slice(1);
}
return vhtml(tagName, options, ...children);
};
const mainWebsiteUrl = process.env.MAIN_WEBSITE_URL || 'http://127.0.0.1:9000';
const appWebsiteUrl = process.env.APP_WEBSITE_URL || 'http://127.0.0.1:8080';
const indexHtml = fs.readFileSync('template/index.html', 'utf8');
function createPage (pathname, title, tree) {
const nav = `
<nav>
<a href="${mainWebsiteUrl}/">Home</a>
<a href="/" ${!pathname.startsWith('/blog') && 'class="active"'}>Docs</a>
<a href="/blog" ${pathname.startsWith('/blog') && 'class="active"'}>Blog</a>
<a href="${mainWebsiteUrl}/support">Support</a>
</nav>
`;
const page = indexHtml
.replace('{{title}}', title)
.replace('{{content}}', tree)
.replace('{{nav}}', nav)
.replace('{{mainWebsiteUrl}}', mainWebsiteUrl);
const filepath = path.join('dist', pathname.substr(1));
try {
fs.mkdirSync(filepath, { recursive: true });
} catch (error) {
if (error.code !== 'EEXIST') {
throw error;
}
}
console.log('creating file:', filepath + '/index.html');
fs.writeFile(filepath + '/index.html', page, function (error) {
if (error) {
throw error;
}
});
}
fs.rmdirSync('dist', { recursive: true });
fs.mkdirSync('dist');
copyDir('static', 'dist');
app(h, createPage);