First commit
This commit is contained in:
224
framework/server/moduleLoader.js
Normal file
224
framework/server/moduleLoader.js
Normal file
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020, 2023, The Unified Company.
|
||||
|
||||
This code is part of Unify.
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
|
||||
as published by the ESA.
|
||||
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
|
||||
|
||||
https://unifyjs.org
|
||||
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
|
||||
import path from 'path';
|
||||
|
||||
import tools from '../unify/tools.js';
|
||||
|
||||
import Console from '../server/console.js';
|
||||
|
||||
import simplePath from '../unify/simplePath.js';
|
||||
|
||||
|
||||
var __dirname = path.resolve();
|
||||
|
||||
global.path = path;
|
||||
|
||||
|
||||
|
||||
export default class moduleLoader{
|
||||
|
||||
parseNewFiles = true;
|
||||
|
||||
files = new Array();
|
||||
|
||||
constructor( os, device, tint ) {
|
||||
|
||||
this.createPlatformCachePath();
|
||||
|
||||
this.setDirectory( os, device, tint );
|
||||
|
||||
}
|
||||
|
||||
createPlatformCachePath() {
|
||||
|
||||
this.pathToPlatforms = new simplePath();
|
||||
|
||||
this.pathToPlatforms.beginWithSlash = false;
|
||||
|
||||
this.pathToPlatforms.endWithSlash = true;
|
||||
|
||||
this.pathToPlatforms.add( "framework" )
|
||||
|
||||
this.pathToPlatforms.add( "cache" )
|
||||
|
||||
this.pathToPlatforms.add( "platforms" )
|
||||
|
||||
}
|
||||
|
||||
setDirectory( os, device, tint ) {
|
||||
|
||||
if( tint ) {
|
||||
|
||||
this.absolutePath = this.pathToPlatforms.clone();
|
||||
|
||||
this.absolutePath.add( os );
|
||||
|
||||
this.absolutePath.add( device );
|
||||
|
||||
this.absolutePath.add( tint );
|
||||
|
||||
} else {
|
||||
|
||||
this.absolutePath = this.pathToPlatforms.clone();
|
||||
|
||||
this.absolutePath.add( global.os );
|
||||
|
||||
this.absolutePath.add( global.device );
|
||||
|
||||
this.absolutePath.add( global.tint );
|
||||
|
||||
}
|
||||
|
||||
this.absolutePath.endWithSlash = true;
|
||||
|
||||
this.absolutePath.absolute = true;
|
||||
|
||||
}
|
||||
|
||||
async loadModulesFromFiles( directory, core, client ) {
|
||||
|
||||
this.client = client;
|
||||
|
||||
this.core = core;
|
||||
|
||||
var path = this.absolutePath.clone();
|
||||
|
||||
path.add( directory );
|
||||
|
||||
|
||||
var files = await fs.readdirSync( path.resolve() + "/" );
|
||||
|
||||
for(var c = 0; c<files.length; c++) {
|
||||
|
||||
var filename = files[c];
|
||||
|
||||
await this.parseFileDirectory( filename, directory );
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
async parseFileDirectory( filename, directory ) {
|
||||
|
||||
var join = path.join;
|
||||
|
||||
var exclude = new Array();
|
||||
|
||||
|
||||
var fileParts = filename.split(".");
|
||||
|
||||
var filePath = join( directory, filename );
|
||||
|
||||
|
||||
if( fileParts.length == 1 ) {
|
||||
|
||||
await this.parseDirectory( filePath );
|
||||
|
||||
} else {
|
||||
|
||||
await this.parseFile( filePath, fileParts );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async parseFile( filePath, fileParts ) {
|
||||
|
||||
var extension = fileParts.pop();
|
||||
|
||||
if( extension == "js" ) {
|
||||
|
||||
await this.loadModule( filePath );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async loadModule( filePath ) {
|
||||
|
||||
var excludeFiles = new Array("config.js");
|
||||
|
||||
if( !excludeFiles.includes( filePath ) ) {
|
||||
|
||||
var absolutePath = this.absolutePath.clone();
|
||||
|
||||
absolutePath.add( filePath );
|
||||
|
||||
var importObject = await import( tools.slash( "file://" + absolutePath.resolve() ) );
|
||||
|
||||
var contructor = importObject.default;
|
||||
|
||||
if( contructor && contructor.prototype ) {
|
||||
|
||||
await this.initializeModule( contructor, filePath );
|
||||
|
||||
} else {
|
||||
|
||||
Console.warning( "\n Warning: " + filePath + " does not contain a default extended class.\n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async initializeModule( initializer, path ) {
|
||||
|
||||
var object = new initializer();
|
||||
|
||||
var className = tools.getClassName( object );
|
||||
|
||||
var exists = this.core.getObjectByclassName( className, this.client );
|
||||
|
||||
if( !exists ) {
|
||||
|
||||
//console.log( "Module loader import unify object", tools.getClassName( object ) );
|
||||
|
||||
this.files.push( object );
|
||||
|
||||
if( this.parseNewFiles ){
|
||||
|
||||
await this.core.parse( object, this.client );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async parseDirectory( filePath ) {
|
||||
|
||||
var excludeDirectorys = new Array("assets");
|
||||
|
||||
var filePathParts = filePath.split("/");
|
||||
|
||||
var directoryName = filePathParts[ filePathParts.length - 1 ];
|
||||
|
||||
|
||||
if( !excludeDirectorys.includes( directoryName ) ) {
|
||||
|
||||
await this.loadModulesFromFiles( "./" + filePath, this.core, this.client );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user