public
Last active

a console app for Windows that monitors changes to a directory and restarts node.js for me

  • Download Gist
ezekiel.cs
C#
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
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;
}
}
}
}

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.