Created
July 27, 2011 05:15
-
-
Save bennage/1108727 to your computer and use it in GitHub Desktop.
a console app for Windows that monitors changes to a directory and restarts node.js for me
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Diagnostics; | |
namespace ezekiel | |
{ | |
class Program | |
{ | |
static DateTime lastRead = DateTime.MinValue; | |
static string monitoring = @"C:\node.js\stuff"; | |
static string startup = @"server.js"; | |
static int processId; | |
static void Main(string[] args) | |
{ | |
var watcher = new FileSystemWatcher(); | |
watcher.Path = monitoring; | |
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName; | |
watcher.Filter = "*.js"; | |
watcher.Changed += Changed; | |
watcher.Created += Changed; | |
watcher.Deleted += Changed; | |
watcher.Renamed += Renamed; | |
// Begin watching | |
watcher.EnableRaisingEvents = true; | |
Start(); | |
Console.WriteLine("Press Enter to exit"); | |
Console.ReadLine(); | |
Kill(); | |
} | |
static void Kill() { | |
var matches = Process.GetProcessesByName("node"); | |
matches.ToList().ForEach(match => { | |
Console.WriteLine("attempting to close node.js [" + match.Id + "]"); | |
match.Kill(); | |
match.WaitForExit(3000); | |
Console.WriteLine("successfully closed"); | |
}); | |
} | |
static void Start() { | |
Kill(); | |
Console.WriteLine("starting node.js"); | |
var start = new ProcessStartInfo(); | |
start.FileName = @"C:\node.js\node.exe"; | |
start.UseShellExecute = false; | |
start.CreateNoWindow = true; | |
start.RedirectStandardOutput = true; | |
start.RedirectStandardInput = true; | |
start.Arguments = Path.Combine(monitoring, startup); | |
var node = new Process(); | |
node.EnableRaisingEvents = true; | |
node.OutputDataReceived += OutputHandler; | |
node.StartInfo = start; | |
node.Start(); | |
node.Refresh(); | |
Console.WriteLine(node.ProcessName); | |
Console.WriteLine("[" + node.Id + "] node.exe started"); | |
processId = node.Id; | |
// close the input, we won't use it | |
var input = node.StandardInput; | |
input.Close(); | |
node.BeginOutputReadLine(); | |
} | |
static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) | |
{ | |
if (!String.IsNullOrEmpty(outLine.Data)) | |
{ | |
Console.WriteLine(outLine.Data); | |
} | |
} | |
static void Renamed(object sender, System.IO.RenamedEventArgs e) | |
{ | |
Start(); | |
} | |
static void Changed(object sender, System.IO.FileSystemEventArgs e) | |
{ | |
// sometimes a text editor makes multiple operations in a single save | |
// we only handle the first | |
var lastWriteTime = File.GetLastWriteTime(e.FullPath); | |
if (lastWriteTime != lastRead) | |
{ | |
Start(); | |
lastRead = lastWriteTime; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I've been using this for a while now (I'm still new with node).
I've found it to work better than some native hot reloaders that I've tried.
They tend to run my atom at 90% usage... :)
Hope you don't mind, but I've packaged this up in a solution with a few fixes.
https://github.com/augmenter/nodemoncs
If you have any objections whatsoever, please notify me if you want me to take it down.
Cheers.