geddy-hotswapper

Track changes in JavaScript files and reload them automatically without need to restart Geddy

npm install geddy-hotswapper
5 downloads in the last month

JavaScript Hot Swapper for Geddy

A small utility that tracks changes in JavaScript modules and reloads them automatically without the need to restart Geddy.

(This will also let you debug Geddy easily with WebStorm.)

Intended only for development. Do not use on production systems.

Note: Experimental. Use at your own risk.

Purpose

The purpose of this package is to minimize the need to restart Geddy while developing. The focus is in allowing small fixes to be made quickly, with the understanding that massive changes will still require restarting Geddy.

The package does not attempt to preserve any data between reloads. The principle here is the assumption that processing a HTTP request is a containable event with little effect on other HTTP requests. If that's not the case for your application, this package may not be good for you.

Setting Up

1. Install Packages

  1. Install Geddy locally using npm: npm install geddy
  2. Install Geddy Hot Swapper locally using npm: npm install geddy-hotswapper

2. Modify Geddy Just a Bit

Make a simple modification to your local copy of Geddy(!) to expose the application:

In this.start function of node_modules/geddy/lib/geddy.js, add the following lines below app.init() call.

if( geddy.config.debug === true ) {
  geddy.app = app;
}

View example

3. Prepare Launch Script

Instead of launching Geddy with geddy, create a launch script which starts Geddy and initializes Hot Swapper for Geddy. This file should be placed in the root directory of your application.

Something along the lines of:

#!/usr/bin/env node

var HotSwapper    = require( 'geddy-hotswapper' );
var hotSwapper    = new HotSwapper();

// Ignore changes in node modules
hotSwapper.exclude( new RegExp( 'node_modules' ) );

// Ignore changes in the launcher script
hotSwapper.exclude( new RegExp( 'app-debug.js' ) );

// Start watching for changes in files
hotSwapper.start();


// Start Geddy
var geddy = require('geddy');

var geddyOpts = {
    environment: process.env.GEDDY_ENVIRONMENT || 'development'
};

geddy.start( geddyOpts );


// React to changes in files by reloading Geddy
hotSwapper.on( 'swapped',
        function( filename )
        {
            var config = require( 'geddy/lib/config' );

            geddy.config = config.readConfig( geddyOpts );
            geddy.worker.config = geddy.config;

            hotSwapper.clear( require.resolve( './config/router' ) );

            var geddyInit = require( 'geddy/lib/init' );
            geddyInit.init( geddy.app, function() { console.log( 'Matrix reloaded' ); } );
        }
    );

4. Done!

If you want to set up WebStorm, create a Node.js Run/Debug Configuration with the following settings.

Node interpreter Point to node.exe
Node parameters Empty
Working Directory Your application's root directory
JavaScript File The launch file you just created
Application Parameters As you see fit
Environment variables As you see fit

Compatibility

Tested on Windows, Node 0.10.22, and Geddy 0.11.8.

Troubleshooting

  • Only works when Geddy configuration sets debug = true.
  • Only works when Geddy is running in single worker mode and does not use geddy.startCluster().
npm loves you