This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
/
damper
executable file
·187 lines (158 loc) · 5.69 KB
/
damper
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
var fs = require('fs');
var http = require('http');
var stylus = require('stylus');
var utils = require('../lib/utils.js');
var opts = utils.opts;
var glob = utils.glob;
var srcdir = fs.existsSync('./hearth/') ? './hearth' : './src'; // For Fireplace compat
process.title = 'damper';
var mimes = {
'css': 'text/css',
'jpg': 'image/jpeg',
'js': 'application/javascript',
'png': 'image/png',
'svg': 'image/svg+xml',
'webapp': 'application/x-web-app-manifest+json',
'woff': 'application/font-woff'
};
var opts = opts(process.argv.slice(2),
{host: '0.0.0.0', port: '6666', l10n: false, compile: false});
if (!opts.compile) {
// Here's the local server.
http.createServer(function(request, response) {
var now = new Date();
console.log(
'[' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '] ' +
request.url);
function writeIndex() {
fs.readFile(srcdir + '/index.html', function(error, content) {
// We'll assume that you don't delete index.html.
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(content, 'utf-8');
});
}
if (request.url == '/') {
return writeIndex();
}
var qindex;
if ((qindex = request.url.indexOf('?')) !== -1) {
request.url = request.url.substr(0, qindex);
}
var filePath = srcdir + request.url;
fs.exists(filePath, function(exists) {
if (exists && !fs.statSync(filePath).isDirectory()) {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
console.error(error);
} else {
var dot = request.url.lastIndexOf('.');
if (dot > -1) {
var extension = request.url.substr(dot + 1);
response.writeHead(200, {'Content-Type': mimes[extension]});
}
response.end(content, 'utf-8');
}
});
} else {
writeIndex();
}
});
}).listen(opts.port, opts.host);
console.log('Server running at http://' + opts.host + ':' + opts.port);
} else {
console.log('Starting compilation...');
}
var watched_filepaths = [];
function reload() {
watched_filepaths.forEach(fs.unwatchFile);
watched_filepaths = [];
// "restart" is a special action keyword
//watch('./damper', null, 'restart');
watch('../lib/compile.js', null, 'nunjucks');
//watch('../lib', 'js', 'nunjucks');
watch(srcdir + '/media/css', 'styl', 'stylus');
watch(srcdir + '/templates', 'html', 'nunjucks');
// When the builder is updated, recompile the templates.
watch(srcdir + '/js/builder.js', null, 'nunjucks');
}
var nunjucks_opts = {};
if (opts.l10n || opts.compile === true || opts.compile === 'l10n') {
nunjucks_opts.l10n = true;
}
function runCommand(command, filepath) {
switch (command) {
case 'restart':
console.log('Restarting...');
return reload();
case 'stylus':
filepath = filepath || opts.path;
var filepathDir = filepath.split('/').slice(0, -1).join('/');
fs.readFile(filepath, function (err, data) {
data = data.toString();
if (err) {
console.error(filepath + ' not found!');
return;
}
stylus(data)
.set('filename', filepath + '.css')
.set('include css', true)
.include(filepathDir)
.render(function(err, css) {
fs.writeFileSync(filepath + '.css', css);
if (err) console.error(err);
});
});
break;
case 'nunjucks':
console.log('Recompiling templates...');
require('../lib/compile').process(
srcdir + '/templates',
srcdir + '/templates.js',
'locale/templates/LC_MESSAGES/messages.pot',
nunjucks_opts
);
break;
}
}
function watch(globpath, ext, command) {
var cb = function(err, filepaths) {
filepaths.forEach(function(filepath) {
watched_filepaths.push(filepath);
if (command == 'stylus') {
fs.exists(filepath, function(exists) {
if (exists) {
runCommand(command, filepath);
}
});
}
// If we're compiling, we don't want to watch the files.
if (opts.compile) {
return;
}
fs.watchFile(filepath, {interval: 250}, function(curr, prev) {
if (curr.mtime.valueOf() != prev.mtime.valueOf() ||
curr.ctime.valueOf() != prev.ctime.valueOf()) {
console.warn('> ' + filepath + ' changed.');
runCommand(command, filepath);
}
});
});
if (filepaths.length > 1 && !opts.compile) {
console.log('Watching ' + filepaths.length + ' ' + ext + ' files.');
}
};
if (globpath.substr(1).indexOf('.') > -1) {
cb(null, [globpath]);
} else {
glob(globpath, ext, cb);
}
}
if (opts.compile && opts.compile !== true) {
runCommand(opts.compile);
} else {
runCommand('nunjucks');
reload();
}