forked from caolan/petrify
/
buildrunner.js
71 lines (56 loc) · 2.01 KB
/
buildrunner.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
// If this is run from the command-line:
if(module.id === '.'){
require.paths.push(__dirname);
require.paths.push(__dirname + '/../deps');
require.paths.push(__dirname + '/../deps/json-template/lib');
require.paths.push(__dirname + '/../deps/markdown-js/lib');
}
var petrify = require('petrify'),
sys = require('sys');
var displayEvent = function(eventname){
return function(name){
sys.log(eventname + ': ' + name);
};
};
var displayError = function(context_type){
return function(err, context){
sys.log(
'ERROR in ' + context + ' ' + context_type + ': ' +
(err.message || err.toString()) +
(err.stack ? '\n' + err.stack : '')
);
};
};
exports.run = function(options){
var start = new Date().getTime();
var runner = petrify.run(options);
runner.data.addListener('load', displayEvent('load'));
runner.templates.addListener('load', displayEvent('load'));
runner.views.addListener('load', displayEvent('load'));
runner.views.addListener('view_done', displayEvent('view_done'));
runner.data.addListener('error', displayError('document'));
runner.views.addListener('error',displayError('view'));
runner.templates.addListener('error', displayError('template'));
runner.addListener('error', displayError);
runner.views.addListener('emit', function(view, path){
sys.log('emit: ' + view + ' => ' + path);
});
runner.addListener('finished', function(err){
var duration = (new Date().getTime()) - start;
if(err) sys.puts('Errors during build (' + duration + 'ms)');
else sys.puts('Done (' + duration + 'ms)');
});
return runner;
};
// If this is run from the command-line:
if(module.id === '.'){
var path = process.ARGV[2];
require.paths.push(path);
exports.run({
data_dir: path + '/data',
view_dir: path + '/views',
template_dir: path + '/templates',
output_dir: path + '/www',
media_dirs: [path + '/media']
});
}