BotanSS/package.js
2022-04-09 20:40:34 +08:00

56 lines
1.0 KiB
JavaScript

"use strict";
// The Package Loader
const fs = require( "fs" );
const cluster = require( "cluster" );
class Package
{
constructor()
{
this._rootNS = { botanss: "./" };
}
rootNS( name, path )
{
if( this._rootNS[ name ] ) return;
this._rootNS[ name ] = fs.realpathSync( path ) + "/";
}
load( _class )
{
var fSep = _class.indexOf( "." );
var nsdomain = _class.substr( 0, fSep );
_class = _class.substr( fSep + 1 ).replace( /\./g, "/" );
var file = this._rootNS[ nsdomain ] + _class;
if( global.debug && cluster.worker )
{
var src = require.resolve( file );
if(!( src in require.cache ))
{
fs.watch( src, this._reload.bind({ "src": src }) );
}
}
return require( file );
}
_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();
setTimeout( () => process.exit(0), 3000 ).unref();
} , 200 );
}
}
global.botanLoader = new Package();