Permalink
Browse files

Add in better feedback from the build process.

  • Loading branch information...
1 parent 4c20972 commit 58abfa0fb5269598fea23513ffc7e2504d143527 @creationix committed Feb 2, 2010
Showing with 87 additions and 21 deletions.
  1. +47 −7 build.js
  2. +40 −14 builder.js
View
54 build.js
@@ -115,8 +115,46 @@ function loop(obj, callback) {
}
}
-function render(data) {
+// combo library. Allows to group several callbacks.
+function Combo(callback) {
+ this.callback = callback;
+ this.items = 0;
+ this.results = [];
+}
+Combo.prototype = {
+ add: function () {
+ var self = this;
+ this.items++;
+ return function () {
+ self.check(self.items - 1, arguments);
+ };
+ },
+ check: function (id, arguments) {
+ this.results[id] = Array.prototype.slice.call(arguments);
+ this.items--;
+ if (this.items == 0) {
+ this.callback.apply(this, this.results);
+ }
+ }
+};
+
+
+function render(data, next) {
var Helpers, haml;
+ var group = new Combo(function() {
+ var args = Array.prototype.slice.call(arguments);
+ next(args.map(function (arg) {
+ return arg[0];
+ }).join("\n"))
+ });
+
+ function write_file(filename, content) {
+ var cb = group.add();
+ File.write(filename, content).addCallback(function () {
+ cb("Wrote " + content.length + " bytes to " + filename);
+ })
+ }
+
Helpers = {
github: function (name) {
return "http://github.com/" + name;
@@ -138,7 +176,7 @@ function render(data) {
// Generate a page for each author...
loop(data.authors, function (name, props) {
props.link = name.toLowerCase().replace(/ /g, "_") + ".html";
- File.write(PUBLIC_DIR + "/" + props.link, haml("layout", {
+ write_file(PUBLIC_DIR + "/" + props.link, haml("layout", {
title: "About " + name,
content: haml("author", props)
}));
@@ -148,31 +186,33 @@ function render(data) {
loop(data.articles, function (name, props) {
props.link = name + ".html";
props.author = data.authors[props.author];
- File.write(PUBLIC_DIR + "/" + props.link, haml("layout", {
+ write_file(PUBLIC_DIR + "/" + props.link, haml("layout", {
title: props.title,
content: haml("article", props)
}));
});
// Generate a index page...
- File.write(PUBLIC_DIR + "/index.html", haml("layout", {
+ write_file(PUBLIC_DIR + "/index.html", haml("layout", {
title: "Index",
content: haml("index", data)
}));
// Write the static files as is...
loop(data.static, function (filename, content) {
- File.write(PUBLIC_DIR + "/" + filename, content.content || content);
+ write_file(PUBLIC_DIR + "/" + filename, content.content || content);
});
}
-exports.build = function () {
+exports.build = function (next) {
// Kick off the process
main([
["articles", ARTICLE_DIR, /^(.*)\.(markdown)$/],
["authors", AUTHOR_DIR, /^(.*)\.(markdown)$/],
["templates", SKIN_DIR, /^(.*)\.(haml)$/],
["static", SKIN_DIR, /^(.*)\.([^.]+)\.([^.]+)$/]
- ], render);
+ ], function (data) {
+ render(data, next);
+ });
};
View
54 builder.js
@@ -1,10 +1,16 @@
-var Markdown = require('markdown'),
- Url = require('url'),
+var QueryString = require('querystring'),
Http = require('http'),
Build = require('./build');
process.mixin(require('sys'));
var PORT = 4242;
+function log(message) {
+ var date = new Date();
+ message.split("\n").forEach(function (line) {
+ puts(date + " - " + line);
+ })
+}
+
Http.createServer(function (req, res) {
var body = "";
req.setBodyEncoding('utf8');
@@ -13,27 +19,47 @@ Http.createServer(function (req, res) {
});
req.addListener('complete', function () {
try {
- body = Url.parse("?" + body, true).query.payload;
- body = JSON.parse(body);
+ body = QueryString.parse(body).query.payload;
+ log("Received GitHub POST hook: " + body);
} catch(e) {
body = false;
+ log("Manual refresh");
}
- build(body);
- res.sendHeader(200, {'Content-Type': 'text/plain'});
- res.sendBody('Thanks for the tip!');
- res.finish();
+ build(body, function (output) {
+ res.sendHeader(200, {'Content-Type': 'text/plain'});
+ res.sendBody(output);
+ res.finish();
+ });
});
}).listen(PORT);
-function build(pull) {
- if (pull) {
+// 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 + "\nDone!");
+ building = false;
+ next(output);
+ });
+ }
+
+ if (data) {
+ log("Pulling latest changes to content");
exec("cd data && git pull origin master").addCallback(function (stdout, stderr) {
- puts(stdout);
- Build.build();
+ real_build();
});
} else {
- Build.build();
+ real_build();
}
}
-puts('Server running at http://127.0.0.1:' + PORT + '/');
+log('Server running at http://127.0.0.1:' + PORT + '/');

0 comments on commit 58abfa0

Please sign in to comment.