BotanSS/package.js
2022-04-03 21:52:44 +09:00

50 lines
961 B
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 )
return;
this._lock = true;
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 )
{
var src = require.resolve( file );
if(!( src in require.cache ))
{
fs.watch( src, _reload.bind({ "src": src }) );
}
}
return require( file );
};
global.botanLoader = new Package();