-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.js
72 lines (60 loc) · 1.85 KB
/
watch.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
var fs = require('fs'),
util = require('util'),
exec = require('child_process').exec;
try { var _ = require('underscore'); }
catch (err) { throw new Error("\n\nI love Underscore.js, don't you?\n\n"); }
console.log("Watching to convert Coffee/Stylus/Jade");
var jade = {
base: '/home/someuser/someproject/templates/',
out: '/var/www/templates/',
exec: 'jade --out %newpath% %filepath%',
extension: 'jade'
};
var stylus = {
base: '/home/someuser/someproject/css/',
out: '/var/www/css/',
exec: 'stylus --out %newpath% %filepath%',
extension: 'styl'
};
var coffee = {
base: '/home/someuser/someproject/js/',
out: '/var/www/js/',
exec: 'coffee --output %newpath% --compile %filepath%',
extension: 'coffee'
};
var convertors = [jade, stylus, coffee];
_.each(convertors, function(convertor) {
watchConvertor(convertor);
});
function watchConvertor(convertor) {
exec('find ' + convertor.base, function(error, stdout, stderr) {
if (error) throw error;
var dirs = _.compact(stdout.split('\n'));
_.each(dirs, function(dir) {
fs.stat(dir, function (error, stats) {
if (error) throw error;
if (stats.isDirectory()) {
fs.watch(dir, function(event, filename) {
if (filename.split('.').pop() === convertor.extension) {
execConvertor(convertor, dir + '/' + filename);
}
});
}
});
});
});
}
function execConvertor(convertor, filepath) {
var part = filepath.replace(convertor.base, ""); // main/login/login.jade
var dirs = _.initial(part.split('/')).join('/'); // main/login
var newpath = convertor.out + dirs; // /var/www/templates/main/login
var exec_str = convertor.exec.replace('%newpath%', newpath);
exec_str = exec_str.replace('%filepath%', filepath);
exec(exec_str,
function (error, stdout, stderr) {
if (error) throw error;
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
}
);
}