-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwatchman.js
executable file
·180 lines (168 loc) · 4.94 KB
/
watchman.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
// Generated by CoffeeScript 1.4.0
(function() {
var cli, eco, exec, fs, getFormattedDate, log, path;
cli = require('cli');
eco = require('eco');
fs = require('fs');
path = require('path');
exec = require('child_process');
exec = exec.exec;
cli.setUsage('watchman [options] target action');
cli.parse({
"ignore-hidden": ['i', "Do not watch hidden files"],
"rate": ['r', "Rate limit actions [like Ns, Nm, Nh, N is int]", "string", null],
"queue": ['q', "Action queue size", "number", 1],
"watch-first": ['w', "Perform the action only when there is a change. Otherwise, action is executed once immediately"]
});
getFormattedDate = function() {
var d, s;
d = new Date();
return s = "" + (d.toDateString()) + " " + (d.toTimeString().split(" ")[0]);
};
log = function(s) {
var d;
d = getFormattedDate();
return console.log(d + " - " + s);
};
cli.main(function(args, options) {
var action, actionQueue, directoryWatcher, execAction, execFromQueue, find_files, onFileChange, queueAction, rate, rateMap, target, testHidden, useQueue, watched, watcher;
if (args.length < 2) {
console.log("Please specify a target and action");
return;
}
rateMap = {
s: 1000,
m: 1000 * 60,
h: 1000 * 60 * 60
};
target = args[0];
action = args[1];
watched = {};
useQueue = false;
actionQueue = [];
queueAction = function(action, file) {
if (actionQueue.length < options["queue"]) {
return actionQueue.push([action, file]);
}
};
execFromQueue = function() {
var a, _i, _len;
for (_i = 0, _len = actionQueue.length; _i < _len; _i++) {
a = actionQueue[_i];
execAction(a[0], a[1]);
}
return actionQueue = [];
};
execAction = function(toExec, file) {
if (toExec == null) {
toExec = action;
}
toExec = eco.render(toExec, {
file: file
});
log("Running action...");
return exec(toExec, function(error, stdout, stderr) {
log("stderr: " + stderr);
return log("stdout: " + stdout);
});
};
onFileChange = function(file) {
log("File changed: " + file);
if (useQueue) {
return queueAction(action, file);
} else {
return execAction(action, file);
}
};
watcher = function(file) {
if (file in watched) {
return;
}
watched[file] = true;
log("watching: " + file);
return fs.watchFile(file, {
persistent: true,
interval: 500
}, function(curr, prev) {
if (curr.size === prev.size && curr.mtime.getTime() === prev.mtime.getTime()) {
return;
}
return onFileChange(file);
});
};
directoryWatcher = function(dir) {
if (dir in watched) {
return;
}
watched[dir] = true;
log("watching directory: " + dir);
return fs.watchFile(dir, {
persistent: true,
interval: 500
}, function(curr, prev) {
try {
return find_files(dir, true);
} catch (error) {
return log("Error while watching dir " + dir + ": " + error);
}
});
};
testHidden = function(file) {
return options["ignore-hidden"] && file[0] === '.';
};
if (options["rate"] != null) {
useQueue = true;
rate = options["rate"];
try {
rate = parseInt(rate.slice(0, -1)) * rateMap[rate.slice(-1)];
} catch (error) {
console.log("Error parsing rate. Rates must be expressed as Ns, Nm, Nh where N is an integer and s, m, h stand for seconds, mintues, hours respectively");
return;
}
setInterval(execFromQueue, rate);
}
find_files = function(target, quiet) {
return fs.exists(target, function(exists) {
if (!quiet && !exists) {
throw "Target file not found: " + target;
}
return fs.stat(target, function(err, stats) {
if (err != null) {
console.log(err + " for target: " + target);
return;
}
if ((stats != null) && stats.isDirectory()) {
directoryWatcher(target);
return fs.readdir(target, function(err, files) {
var file, _i, _len, _results;
_results = [];
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
if (!testHidden(file)) {
_results.push(find_files(target + "/" + file));
}
}
return _results;
});
} else {
if (!testHidden(target)) {
return watcher(target);
}
}
});
});
};
try {
find_files(target);
} catch (error) {
log("Error: " + error);
}
if (!options["watch-first"]) {
if (useQueue) {
return queueAction();
} else {
return execAction();
}
}
});
}).call(this);