50 lines
1.0 KiB
JavaScript
50 lines
1.0 KiB
JavaScript
// The Package Loader
|
|
|
|
var fs = require( "fs" );
|
|
var rootNS = {
|
|
botanss: "./"
|
|
};
|
|
|
|
var Package = function() { };
|
|
|
|
Package.prototype.rootNS = function( name, path )
|
|
{
|
|
if( rootNS[ name ] ) return;
|
|
rootNS[ name ] = fs.realpathSync( path ) + "/";
|
|
};
|
|
|
|
var _reload = function( e, filename )
|
|
{
|
|
if( this._lock == global.X_SERVER_CLUSTER.worker.id )
|
|
return;
|
|
this._lock = global.X_SERVER_CLUSTER.worker.id;
|
|
|
|
setTimeout( () =>
|
|
{
|
|
global.Dragonfly.Info( `Change detected: ${this.src}, reloading` );
|
|
global.X_SERVER_CLUSTER.worker.destroy();
|
|
} , 200 );
|
|
};
|
|
|
|
Package.prototype.load = function( _class )
|
|
{
|
|
var fSep = _class.indexOf( "." );
|
|
var nsdomain = _class.substr( 0, fSep );
|
|
_class = _class.substr( fSep + 1 ).replace( /\./g, "/" );
|
|
|
|
var file = rootNS[ nsdomain ] + _class;
|
|
|
|
if( global.debug && global.X_SERVER_CLUSTER && global.X_SERVER_CLUSTER.worker )
|
|
{
|
|
var src = require.resolve( file );
|
|
if(!( src in require.cache ))
|
|
{
|
|
fs.watch( src, _reload.bind({ "src": src }) );
|
|
}
|
|
}
|
|
|
|
return require( file );
|
|
};
|
|
|
|
global.botanLoader = new Package();
|