@webfaas/webfaas
Version:
WebFaaS Framework
227 lines • 6.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebFaaS = void 0;
const fs = require("fs");
const path = require("path");
const webfaas_core_1 = require("@webfaas/webfaas-core");
const Config_1 = require("@webfaas/webfaas-core/lib/Config/Config");
class WebFaaS {
constructor() {
this.config = null;
this.core = null;
this.pluginManager = null;
this.pathConfigFile = null;
this.pathNodeModulesDirectory = null;
this.pathRootPackageDirectory = null;
this.pathCurrentWorkingDirectory = null;
this.started = false;
}
getStarted() {
return this.started;
}
/**
* return WebFaaS - Config
*/
getConfig() {
if (!this.config) {
let newConfig = new Config_1.Config();
newConfig.read(this.getPathConfigFile());
this.setConfig(new Config_1.Config());
return newConfig;
}
else {
return this.config;
}
}
/**
* set config
* @param config
*/
setConfig(config) {
this.config = config;
}
/**
* return WebFaaS - Core
*/
getCore() {
if (!this.core) {
this.core = new webfaas_core_1.Core(this.getConfig());
}
return this.core;
}
/**
* return cwd
*/
getPathCurrentWorkingDirectory() {
if (this.pathCurrentWorkingDirectory === null) {
this.setPathCurrentWorkingDirectory(process.cwd());
return process.cwd();
}
else {
return this.pathCurrentWorkingDirectory;
}
}
/**
* set cwd
* @param value cwd
*/
setPathCurrentWorkingDirectory(value) {
this.pathCurrentWorkingDirectory = value;
}
/**
* return path config files
*/
getPathConfigFile() {
if (this.pathConfigFile === null) {
let newPathConfigFile = this.searchConfigFile();
this.setPathConfigFile(newPathConfigFile);
return newPathConfigFile;
}
else {
return this.pathConfigFile;
}
}
/**
* set path config files
* @param value path config files
*/
setPathConfigFile(value) {
this.pathConfigFile = value;
}
/**
* return path node modules directory
*/
getPathNodeModulesDirectory() {
if (this.pathNodeModulesDirectory === null) {
let newPathNodeModulesDirectory = this.searchNodeModulesDirectory();
this.setPathNodeModulesDirectory(newPathNodeModulesDirectory);
return newPathNodeModulesDirectory;
}
else {
return this.pathNodeModulesDirectory;
}
}
/**
* set path node_modules directory
* @param value path node modules
*/
setPathNodeModulesDirectory(value) {
this.pathNodeModulesDirectory = value;
}
/**
* return path root package directory
*/
getPathRootPackageDirectory() {
if (this.pathRootPackageDirectory === null) {
let newPathRootPackageDirectory = this.searchRootPackageDirectory();
this.setPathRootPackageDirectory(newPathRootPackageDirectory);
return newPathRootPackageDirectory;
}
else {
return this.pathRootPackageDirectory;
}
}
/**
* set path root package directory
* @param value path node modules
*/
setPathRootPackageDirectory(value) {
this.pathRootPackageDirectory = value;
}
/**
* return plugin manager
*/
getPluginManager() {
if (!this.pluginManager) {
this.pluginManager = new webfaas_core_1.PluginManager(this.getCore());
}
return this.pluginManager;
}
/**
* scan and load plugins
*/
scanAndLoadPlugins() {
let folder = this.getPathNodeModulesDirectory();
if (folder) {
this.loadPluginsByFolder(folder);
}
}
/**
* load plugins by folder
* @param baseFolder
*/
loadPluginsByFolder(baseFolder) {
this.getPluginManager().loadPluginsByFolder(baseFolder);
}
/**
* search config file
*/
searchConfigFile() {
const listFile = [];
if (process.env["WEBFAAS_CONFIG"]) {
listFile.push(process.env["WEBFAAS_CONFIG"]);
}
listFile.push(path.join(this.getPathCurrentWorkingDirectory(), "webfaas.json"));
listFile.push(path.join(__dirname, "webfaas.json"));
return this.searchExistFileInArray(listFile);
}
/**
* search node modules directory
*/
searchNodeModulesDirectory() {
const listFile = [];
listFile.push(path.join(this.getPathCurrentWorkingDirectory(), "node_modules"));
if (require.main) {
listFile.push(...require.main.paths);
}
return this.searchExistFileInArray(listFile);
}
/**
* search root package directory
*/
searchRootPackageDirectory() {
if (this.searchExistFileInArray([path.join(this.getPathCurrentWorkingDirectory(), "package.json")])) {
return this.getPathCurrentWorkingDirectory();
}
else {
return "";
}
}
/**
* search exist file in array
* @param listFile
*/
searchExistFileInArray(listFile) {
for (let i = 0; i < listFile.length; i++) {
let file = listFile[i];
if (fs.existsSync(file)) {
return file;
}
}
return "";
}
/**
* start
*/
async start() {
if (this.started === false) {
const config = this.getConfig();
this.getCore().getPackageRegistryManager().setDefaultRegistryName(config.get("registry.default", "npm"));
await this.getPluginManager().start();
this.started = true;
}
}
/**
* stop
*/
async stop() {
if (this.started) {
await this.getPluginManager().stop();
this.started = false;
}
}
}
exports.WebFaaS = WebFaaS;
var webfaas_core_2 = require("@webfaas/webfaas-core");
Object.defineProperty(exports, "Core", { enumerable: true, get: function () { return webfaas_core_2.Core; } });
//# sourceMappingURL=WebFaaS.js.map