forked from jsconf/jsconf.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
124 lines (105 loc) · 3.05 KB
/
build.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
const path = require('path');
const fs = require('fs');
const yaml = require('node-yaml');
const pug = require('pug');
const Promise = require('bluebird');
const OUTPUT_DIR = process.env.OUTPUT_DIR || 'dist';
// site configuration
const pages = {
home: {
template: 'index',
filename: 'index.html'
},
onboard: {
template: 'onboard',
filename: 'i-want-to-run-a-jsconf.html'
},
coc: {
template: 'coc',
filename: 'codeofconduct.html'
}
};
buildSite();
function loadEvents() {
const jsconf = yaml.readSync(path.resolve(__dirname, 'conferences/jsconf.yaml')) || [];
const family = yaml.readSync(path.resolve(__dirname, 'conferences/family.yaml')) || [];
return {
jsconf: jsconf.map(cleanup),
family: family.map(cleanup)
};
function cleanup(item) {
const id = Object.keys(item)[0];
let conf = Object.assign({}, item[id]);
conf.id = id;
// status can be a string or list item(s) with dates
if (!!conf.status) {
if (conf.status instanceof Array) {
let status = null;
conf.status.forEach((stat) => {
let c = Object.keys(stat)[0];
let s = stat[c];
let start = !!s.start ? new Date(s.start) : null;
let end = !!s.end ? new Date(s.end) : null;
// if only end is defined, assume yesterday as start time
if (end !== null && start === null) {
let dt = new Date();
dt.setDate(dt.getDate() - 1);
start = dt;
}
// if we are betwen start and end, this status is valid
let now = Date.now();
if (start <= now && now <= end) {
conf.status = c;
}
});
}
// clear out any status that isn't a string
if (typeof conf.status !== 'string') {
conf.status = null;
}
}
return conf;
}
}
async function buildSite() {
try {
const start = +(new Date);
await buildCSS();
await buildHTML();
const end = +(new Date);
console.info(`[DONE] Site successfully built in ${end - start} ms`);
} catch (err) {
console.error(err);
}
}
async function buildHTML() {
console.info(`[HTML] Building pages`);
const conferences = loadEvents();
return Promise.all([
buildFile(pages.home, { conferences }),
buildFile(pages.onboard),
buildFile(pages.coc)
]);
function buildFile(page, props) {
return new Promise((resolve, reject) => {
const render = pug.compileFile(path.resolve(__dirname, `templates/${page.template}.pug`), {pretty:true});
const html = render(props);
fs.writeFile(path.resolve(__dirname, OUTPUT_DIR, page.filename), html, err => {
if (err) return reject(err);
resolve();
});
});
}
}
async function buildCSS() {
console.info(`[CSS] Copying CSS`);
const source = path.resolve(__dirname, 'css/main.css');
const output = path.resolve(__dirname, OUTPUT_DIR, 'css/main.css');
return new Promise((resolve, reject) => {
fs.copyFile(source, output, err => {
if (err) return reject(err);
resolve();
});
});
}