forked from mozilla/fireplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdamper.js
184 lines (161 loc) · 5.54 KB
/
damper.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
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
var fs = require('fs');
var http = require('http');
var path = require('path');
// Here's the local server.
var indexdata = 'Still loading...';
fs.readFile('./hearth/index.html', function(err, data) {
indexdata = data;
});
var mimes = {
'css': 'text/css',
'js': 'application/javascript',
'woff': 'application/font-woff'
};
var options = function(opts, defaults) {
var out = defaults || {},
last, i, is_flag;
for(i = 0; i < opts.length; i++) {
is_flag = opts[i].substr(0, 1) === '-';
if (is_flag && last) {
out[last] = true;
} else if (!is_flag && last) {
while(last.substr(0, 1) === '-'){
last = last.substr(1);
}
out[last] = opts[i];
}
last = is_flag ? opts[i] : null;
}
return out;
};
var opts = options(process.argv.slice(2), {'host': '0.0.0.0', 'port': '8675'});
http.createServer(function(request, response) {
var now = new Date();
console.log(
'[' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '] ' +
request.url);
function writeIndex() {
fs.readFile('./hearth/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 filePath = './hearth' + request.url;
fs.exists(filePath, function(exists) {
if (exists) {
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);
var child_process = require('child_process'),
watched_filepaths = [];
function glob(path, ext, done) {
var results = [];
fs.readdir(path, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
glob(file, ext, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
// If it's got the right extension, add it to the list.
if(file.substr(file.length - ext.length) == ext)
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
}
function reload() {
watched_filepaths.forEach(function(filepath) {
fs.unwatchFile(filepath);
});
watched_filepaths = [];
// "restart" is a special action keyword
watch('./damper.js', null, 'restart');
watch('./hearth/media/css', 'less', 'less');
watch('./hearth/templates', 'html', 'nunjucks');
}
function compileNunjucks() {
child_process.exec('./nunjucks/bin/precompile ./hearth/templates -f --amd > hearth/templates.js', function(e, so, se) {
console.log(se); // stderr
if (e !== null) {
console.error(e);
}
});
}
function runCommand(command, filepath) {
switch (command) {
case 'restart':
return reload();
case 'less':
child_process.exec('lessc ' + filepath + ' ' + filepath + '.css', function(e, so, se) {
if (e !== null) {
console.error(e);
}
});
break;
case 'nunjucks':
compileNunjucks();
break;
}
}
function watch(globpath, ext, command) {
var cb = function(err, filepaths) {
// for single files, filepaths will just be one file: the exact match
filepaths.forEach(function(filepath) {
// save the filepath so that we can unwatch it easily when reloading, and start the watch
watched_filepaths.push(filepath);
if (command == 'less') {
fs.exists(filepath, function(exists) {
if (exists) {
runCommand(command, filepath);
}
});
}
fs.watchFile(filepath, {interval: 250}, function(curr, prev) {
// ignore simple accesses
if (curr.mtime.valueOf() != prev.mtime.valueOf() || curr.ctime.valueOf() != prev.ctime.valueOf()) {
console.warn('> ' + filepath + ' changed.');
runCommand(command, filepath);
}
});
});
if (filepaths.length > 1)
console.log('Watching ' + filepaths.length + ' ' + ext + ' files.');
};
if (globpath.substr(1).indexOf('.') > -1) {
cb(null, [globpath]);
} else {
glob(globpath, ext, cb);
}
}
compileNunjucks();
reload();