Skip to content

Instantly share code, notes, and snippets.

@bennage
Created July 27, 2011 05:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennage/1108727 to your computer and use it in GitHub Desktop.
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
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;
}
}
}
}
@augmenter
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment