-
Notifications
You must be signed in to change notification settings - Fork 11
/
builder.js
65 lines (57 loc) · 1.41 KB
/
builder.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
require.paths.unshift(__dirname);
var QueryString = require('querystring'),
Http = require('http'),
Build = require('./build');
process.mixin(require('sys'));
var PORT = 4242;
function log(message) {
puts((new Date()) + " - " + message);
}
Http.createServer(function (req, res) {
var body = "";
req.setBodyEncoding('utf8');
req.addListener('data', function (chunk) {
body += chunk;
});
req.addListener('end', function () {
if (body.length > 0) {
log("Received GitHub POST hook: " + body);
} else {
body = false;
log("Manual refresh");
}
build(body, function (output) {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.write(output);
res.close();
});
});
}).listen(PORT);
// simple semaphore
var building = false;
function build(data, next) {
function real_build() {
if (building) {
return;
}
building = true;
log("Rebuilding site");
Build.build(function (output) {
log(output);
building = false;
next(output);
}, log);
}
if (data) {
log("Pulling latest changes to content");
exec("cd " + __dirname + "/data && git pull origin master").addCallback(function (stdout, stderr) {
real_build();
});
} else {
real_build();
}
}
build(null, function(output) {
log("Initial build done. Waiting for more requests");
log('Server running at http://127.0.0.1:' + PORT + '/');
});