springboot-express
Version:
express-spring-boot
115 lines • 4.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Application = void 0;
const express = require("express");
const fs_1 = require("fs");
const List_1 = require("lib-utils-ts/src/List");
const Predication_1 = require("lib-utils-ts/src/Predication");
const logger20js_ts_1 = require("logger20js-ts");
require("lib-utils-ts/src/globalUtils");
const Properties_1 = require("lib-utils-ts/src/file/Properties");
const Exception_1 = require("lib-utils-ts/src/Exception");
const fs = require("fs");
const Define_1 = require("lib-utils-ts/src/Define");
const Class_1 = require("lib-utils-ts/src/Class");
const Path_1 = require("lib-utils-ts/src/file/Path");
class Application {
constructor() {
this.logger = logger20js_ts_1.Logger.factory(Application.name);
this.loaded = false;
this.handler = new List_1.ArrayList();
this.preValidation = new List_1.ArrayList();
logger20js_ts_1.Logger.setPattern("[%hours{yellow}] %T{w?yellow;e?red;d?green}/%name - %error");
logger20js_ts_1.Logger.setColorize(true);
logger20js_ts_1.Logger.setSaveLog(false);
this.app = express();
}
getApp() { return this.app; }
addHandler(handler) {
if (!this.getHandler(handler.getClass().getName()))
this.handler.add(handler);
return handler;
}
getHandler(target) {
return this.handler
.stream()
.filter(value => value.getClass().getName().equals(target))
.findFirst()
.orElse(null);
}
setPreValidation(constructorName) { this.preValidation.add(constructorName); }
setConfigFileName(path) { this.configFile = path; }
getConfigFileName() { return this.configFile; }
getProperties() { return this.prop; }
loadConfiguration(path) {
if (!path && this.getConfigFileName())
this.loadConfiguration(this.getConfigFileName());
else {
try {
if (/json$/.test(path))
this.prop = new Properties_1.PropertiesJson();
else
this.prop = new Properties_1.Properties();
this.prop.load(Application.getClass().getResourcesAsStream(path));
}
catch (e) {
throw new Exception_1.RuntimeException(e);
}
}
}
tree(path, exclude = null, deepLimit = 50, deep = 0) {
let out = [];
if (fs.lstatSync(path).isDirectory()) {
List_1.ArrayList.of(fs_1.readdirSync(path))
.stream()
.each(subPath => {
let isDir;
if ((isDir = fs.statSync(`${path}/${subPath}`).isDirectory()) && (deep < deepLimit || deepLimit === -1)) {
out = out.concat(this.tree(`${path}/${subPath}`, exclude, deepLimit, 0));
deep++;
}
else if (!isDir) {
if (Define_1.Define.of(exclude).isNull() || !exclude.test(subPath))
out.push(`${path}/${subPath}`);
}
});
return out;
}
throw new Exception_1.RuntimeException(`${path} is not a directory !`);
}
init(pages_directory) {
let p0 = new Predication_1.Predication();
let p1 = new Predication_1.Predication();
if (this.loaded)
return this;
p1.test = (value) => value.endsWith(".d.ts");
p0.test = (value) => value.endsWith(".ts");
List_1.ArrayList.of(this.tree(pages_directory, /(.js|.map|.d.ts)$/))
.stream()
.filter(p0.and(p1.negate()))
.each(value => {
let constructor, msg = `@Spring.Instance : static '%s' class was been add with successful`, instance;
try {
value = value.explodeAsList(/\.\w+$/).get(0);
constructor = Class_1.Class.forName(new Path_1.Path(`${value}`));
instance = constructor.newInstance();
if (!this.preValidation.stream().filter(o => o.equals(constructor.getName())).findFirst().isEmpty()) {
this.preValidation.remove(constructor.getName());
this.addHandler(instance);
msg = `@Spring.Instance : new instance of '%s' was been created with successful`;
}
}
catch (e) {
throw new Exception_1.RuntimeException(e);
}
this.logger.debug(`import : %s page`, new Path_1.Path(`${value}`.replace(process.cwd(), "")).toForNamePath().replace(/^\./, "@").colorize().green);
this.logger.debug(msg, constructor.getName().colorize().green);
});
this.loaded = true;
return this;
}
static getInstance() { return Application.INSTANCE; }
}
exports.Application = Application;
Application.INSTANCE = new Application();
//# sourceMappingURL=Application.js.map