forked from jamesshore/quixote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.js
executable file
·79 lines (62 loc) · 1.82 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
73
74
75
76
77
78
79
#!/usr/local/bin/node
// Automatically runs Jake when files change.
//
// Thanks to Davide Alberto Molin for contributing this code.
// See http://www.letscodejavascript.com/v3/comments/live/7 for details.
(function() {
"use strict";
var gaze = require("gaze");
var spawn = require("child_process").spawn;
var path = require("path");
var WATCH = [
"build/**/*.js",
"src/**/*.js", "src/**/*.html", "src/**/*.css",
"vendor/**/*.js"
];
var COMMAND = require("./build/config/build_command.js");
var args = process.argv.slice(2);
var child = null;
var buildQueued = false;
var buildStartedAt;
gaze(WATCH, function(err, watcher) {
if (err) {
console.log("WATCH ERROR:", err);
return;
}
console.log("Will run " + COMMAND + " when " + WATCH.join(" or ") + " changes.");
watcher.on("all", triggerBuild);
triggerBuild(); // Always run after startup
});
function triggerBuild(event, filepath) {
logEvent(event, filepath);
if (child === null) runJake();
else queueAnotherBuild();
}
function runJake() {
buildStartedAt = Date.now();
console.log("\n*** RUN> " + COMMAND + " " + args.join(" "));
child = spawn(COMMAND, args, { stdio: "inherit" });
child.once("exit", function(code) {
child = null;
});
}
function queueAnotherBuild() {
if (buildQueued) return;
if (debounce()) return;
console.log("*** Build queued");
buildQueued = true;
child.once("exit", function(code) {
buildQueued = false;
triggerBuild();
});
function debounce() {
var msSinceLastBuild = Date.now() - buildStartedAt;
return msSinceLastBuild < 1000;
}
}
function logEvent(event, filepath) {
if (filepath === undefined) return;
var truncatedPath = path.basename(path.dirname(filepath)) + "/" + path.basename(filepath);
console.log("*** " + event.toUpperCase() + ": .../" + truncatedPath);
}
}());