nope-js-node
Version:
NoPE Runtime for Nodejs. For Browser-Support please use nope-browser
240 lines (239 loc) • 8.93 kB
JavaScript
;
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
var _a, _b, _c, _d;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadFunctions = exports.loadPackages = exports.writeDefaultConfig = exports.listFunctions = exports.listPackages = void 0;
const promises_1 = require("fs/promises");
const path_1 = require("path");
require("reflect-metadata");
const async_1 = require("../helpers/async");
const fileMethods_1 = require("../helpers/fileMethods");
const setMethods_1 = require("../helpers/setMethods");
const index_browser_1 = require("../index.browser");
const getLogger_1 = require("../logger/getLogger");
const runNopeBackend_1 = require("../cli/runNopeBackend");
const logger = (0, getLogger_1.getNopeLogger)("helper-load-packages");
/**
* List the available Packages
*
* @export
* @param {string} [dir='./modules']
* @return {*}
*/
async function listPackages(dir = "./modules") {
// Define the Return Array.
const ret = new Array();
// Scan for the Package-Files
// And iterate over them.
for (const fileName of await (0, fileMethods_1.listFiles)(dir, ".package.js")) {
// Now Try to load a Package, to test whether is is an assembly.
try {
logger.info("found ", fileName);
ret.push({
package: (await (_a = (0, path_1.resolve)(fileName), Promise.resolve().then(() => require(_a))))
.DESCRIPTION,
path: fileName,
});
}
catch (e) {
logger.error("Failed Loading the Package " + fileName);
logger.error(e);
}
}
return ret;
}
exports.listPackages = listPackages;
async function listFunctions(dir = "./modules") {
// Define the Return Array.
const ret = new Array();
// Scan for the Package-Files
// And iterate over them.
for (const fileName of await (0, fileMethods_1.listFiles)(dir, ".functions.js")) {
// Now Try to load a Package, to test whether is is an assembly.
try {
logger.info("found ", fileName);
ret.push({
content: (await (_b = (0, path_1.resolve)(fileName), Promise.resolve().then(() => require(_b)))).DESCRIPTION,
path: fileName,
});
}
catch (e) {
logger.error("Failed Loading the functions in file: " + fileName);
logger.error(e);
}
}
return ret;
}
exports.listFunctions = listFunctions;
/**
* Helper Function to write a default configuration.
*
* @export
* @param {string} [dir='./modules']
* @param {string} [filename=join(resolve(process.cwd()), 'config', 'assembly.json')]
*/
async function writeDefaultConfig(dir = "./modules", filename = (0, path_1.join)((0, path_1.resolve)(process.cwd()), "config", "settings.json")) {
// Determine all Packages
const packages = (await listPackages(dir)).map((item) => {
return {
nameOfPackage: item.package.nameOfPackage,
defaultInstances: item.package.defaultInstances,
autostart: item.package.autostart,
path: item.path,
};
});
const functions = (await listFunctions(dir)).map((item) => {
return {
path: item.path,
functions: Object.getOwnPropertyNames(item.content || {}),
};
});
const config = (0, index_browser_1.deepClone)(runNopeBackend_1.DEFAULT_SETTINGS);
config.channelParams = "localhost:7000";
config.channel = "event";
delete config.id;
const file = {
functions,
packages,
// Export the configuration
config,
connections: [],
};
await (0, fileMethods_1.createFile)(filename, (0, index_browser_1.stringifyWithFunctions)(file, 4));
}
exports.writeDefaultConfig = writeDefaultConfig;
/**
* Function to load the Packages.
*
* @export
* @param {INopePackageLoader} loader
* @param {string} filename
*/
async function loadPackages(loader, filename = (0, path_1.join)((0, path_1.resolve)(process.cwd()), "config", "settings.json"), delay = 2) {
let data = {
functions: [],
packages: [],
connections: [],
config: {},
};
try {
/** Load the File and Parse it. */
data = (0, index_browser_1.parseWithFunctions)(await (0, promises_1.readFile)(filename, { encoding: "utf8" }));
}
catch (e) {
// Generate the Default File
await writeDefaultConfig(filename);
// Show an Hint
logger.warn("No configuration was present. Created a new config file in " + filename);
// Readin the newly created Data.
data = JSON.parse(await (0, promises_1.readFile)(filename, {
encoding: "utf8",
}));
}
// Define the Return Array.
const packages = new Array();
// Scan for the Package-Files
// And iterate over them.
for (const item of data.packages) {
// Now Try to load a Package, to test whether is is an assembly.
try {
const loadedPackage = (await (_c = (0, path_1.resolve)(item.path), Promise.resolve().then(() => require(_c))))
.DESCRIPTION;
loadedPackage.autostart = item.autostart;
loadedPackage.defaultInstances = item.defaultInstances;
packages.push(loadedPackage);
}
catch (e) {
logger.error("Failed Loading the Package " + item.nameOfPackage);
logger.error(e);
}
}
await loader.dispatcher.ready.waitFor();
// Iterate over the Packages
for (const thePackageToLoad of packages) {
try {
await loader.addPackage(thePackageToLoad);
}
catch (e) {
logger.error('Failed Add the Package "' +
thePackageToLoad.nameOfPackage +
'" to the PackageLoader', e);
}
}
if (delay > 0) {
logger.info(`Waiting ${delay / 2} [s] before hosting services.`);
await (0, async_1.sleep)(delay * 500);
}
await loader.generateInstances();
if (delay > 0) {
logger.info(`Waiting ${delay / 2} [s] before creating instances.`);
await (0, async_1.sleep)(delay * 500);
}
// Generate the instances.
await loader.generateInstances();
}
exports.loadPackages = loadPackages;
/**
* Helper to read function provided in the defined configuration.
*
* @author M.Karkowski
* @export
* @param {string} [filename=join(resolve(process.cwd()), "config", "assembly.json")]
* @return {*}
*/
async function loadFunctions(loader, filename = (0, path_1.join)((0, path_1.resolve)(process.cwd()), "config", "settings.json"), delay = 2) {
let data = {
functions: [],
packages: [],
connections: [],
config: {},
};
try {
/** Load the File and Parse it. */
data = JSON.parse(await (0, promises_1.readFile)(filename, { encoding: "utf8" }));
}
catch (e) {
// Generate the Default File
await writeDefaultConfig(filename);
// Show an Hint
logger.warn("No configuration was present. Created a new config file in " + filename);
// Readin the newly created Data.
data = JSON.parse(await (0, promises_1.readFile)(filename, {
encoding: "utf8",
}));
}
// Define the Return Array.
const successfull = new Array();
// Get the container containing all registered Services and Classes.
const CONTAINER = (0, index_browser_1.getCentralDecoratedContainer)();
// Scan for the Package-Files
// And iterate over them.
for (const item of data.functions) {
// Now Try to load a Package, to test whether is is an assembly.
try {
// Load the Function:
const before = new Set(CONTAINER.services.keys());
await (_d = (0, path_1.resolve)(item.path), Promise.resolve().then(() => require(_d)));
const after = new Set(CONTAINER.services.keys());
const diff = (0, setMethods_1.determineDifference)(before, after);
if (diff.added.size > 0) {
logger.info("loaded services of file", '"' + item.path + '"', "found:" + JSON.stringify(Array.from(diff.added), undefined, 4));
}
// Mark the file as sucessfully loaded.
successfull.push(item.path);
}
catch (e) {
logger.error("Failed Loading function-file at " + item.path);
logger.error(e);
}
}
await loader.addDecoratedElements({
consider: ["services"],
});
return successfull;
}
exports.loadFunctions = loadFunctions;