Created
May 29, 2012 20:47
-
-
Save AdamPflug/2830614 to your computer and use it in GitHub Desktop.
Jake task to continously monitor the javascript and less files for the project, and rebuild them as necessary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc('Continously monitors the javascript and less files for the project, and rebuilds them as necessary'); | |
task('watch', ['build'], function () { | |
// Recursively watch files with a callback | |
var fs = require('fs'); | |
var files = []; | |
var findFilesAndWatch = function (path) { | |
fs.stat(path, function (err, stats) { | |
if (err) { | |
return false; | |
} | |
if (stats.isFile()) { | |
files.push(path); | |
fs.watchFile(path, changeCallbackFactory(path)); | |
} | |
else if (stats.isDirectory()) { | |
fs.readdir(path, function (err, dirListing) { | |
if (err) { | |
return log.fatal(err); | |
} | |
for (var f in dirListing) { | |
findFilesAndWatch(path + '/' + dirListing[f]); | |
} | |
}); | |
} | |
}); | |
}; | |
var changeCallbackFactory = function (file) { | |
return function (cur, prev) { | |
if (cur.mtime > prev.mtime) { | |
stopWatch(); | |
var fileTask = jake.Task[file]; | |
if (fileTask) { | |
fileTask.modTime = cur.mtime; | |
} | |
jake.Task['build'].reenable(true); | |
jake.Task['build'].invoke(); | |
startWatch(); | |
} | |
}; | |
}; | |
var startWatch = function () { | |
for (var i = 0; i < files.length; i++) { | |
fs.watchFile(files[i], changeCallbackFactory(files[i])); | |
} | |
}; | |
var stopWatch = function () { | |
for (var i = 0; i < files.length; i++) { | |
fs.unwatchFile(files[i]); | |
} | |
}; | |
findFilesAndWatch('themes'); | |
}, {async: true}); |
Yea that works for me, thanks. On vacation in Italy for the next 2 weeks, but I'll work on this when I get back.
…Sent from my iPad
On Jun 9, 2012, at 11:34 PM, Matthew ***@***.*** wrote:
All right, this is in master. Just do someTask.addListener('error', function (e) {}); and call via `invoke`. If you don't set an explicit listener, it's handled the way it's always been. Let me know if this works for you.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2830614
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All right, this is in master. Just do someTask.addListener('error', function (e) {}); and call via
invoke
. If you don't set an explicit listener, it's handled the way it's always been. Let me know if this works for you.