Skip to content

Instantly share code, notes, and snippets.

@ken210
Created November 13, 2012 16:57
Show Gist options
  • Save ken210/4066975 to your computer and use it in GitHub Desktop.
Save ken210/4066975 to your computer and use it in GitHub Desktop.
A NodeJS file-watcher for .NET
var fs = require('fs'),
walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if(err) return done(err);
var pending = list.length;
if(!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if(stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if(!--pending) done(null, results);
});
} else {
results.push(file);
if(!--pending) done(null, results);
}
});
});
});
},
sys = require('sys'),
exec = require('child_process').exec,
isFrontEndFile = /\.(js\b)|(cshtml\b)/,
filename = __filename.split('/'),
output = '\n',
filename = filename[filename.length - 1],
resetCmd = 'C:\\Windows\\System32\\inetsrv\\appcmd.exe recycle APPPOOL appPoolName';
function puts(error, stdout, stderr) {
sys.puts(stdout);
}
function watchFile(file) {
fs.watchFile(file, function () {
console.log('\n----------------\n\033[0mFile \033[0;32m"' + file + '" \033[0mmodified\n----------------');
exec(resetCmd, puts);
});
}
walk('.', function (err, files){
files.forEach(function (file) {
if (isFrontEndFile.test(file) && file !== filename) {
watchFile(file);
}
});
});
output += 'iisWatcher.js:\nwatching \033[0;33m' + __dirname + '\033[0m\n';
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment