BotanSS/package.js

56 lines
1.0 KiB
JavaScript
Raw Normal View History

"use strict";
2015-04-18 14:14:36 +00:00
// The Package Loader
const fs = require( "fs" );
const cluster = require( "cluster" );
2015-04-18 14:14:36 +00:00
class Package
2022-04-03 12:52:44 +00:00
{
constructor()
{
this._rootNS = { botanss: "./" };
}
2022-04-03 12:52:44 +00:00
rootNS( name, path )
2022-04-03 12:52:44 +00:00
{
if( this._rootNS[ name ] ) return;
this._rootNS[ name ] = fs.realpathSync( path ) + "/";
}
2022-04-03 12:52:44 +00:00
load( _class )
{
var fSep = _class.indexOf( "." );
var nsdomain = _class.substr( 0, fSep );
_class = _class.substr( fSep + 1 ).replace( /\./g, "/" );
2015-04-18 14:14:36 +00:00
var file = this._rootNS[ nsdomain ] + _class;
2015-04-18 14:14:36 +00:00
if( global.debug && cluster.worker )
2022-04-03 12:52:44 +00:00
{
var src = require.resolve( file );
if(!( src in require.cache ))
{
fs.watch( src, this._reload.bind({ "src": src }) );
}
2022-04-03 12:52:44 +00:00
}
return require( file );
2015-06-03 06:45:45 +00:00
}
2022-04-03 12:52:44 +00:00
_reload( e, filename )
{
if( this._lock == cluster.worker.id )
return;
this._lock = cluster.worker.id;
setTimeout( () =>
{
global.Dragonfly.Info( `Change detected: ${this.src}, reloading` );
cluster.worker.disconnect();
2022-04-09 12:40:34 +00:00
setTimeout( () => process.exit(0), 3000 ).unref();
} , 200 );
}
}
2015-04-18 14:14:36 +00:00
global.botanLoader = new Package();