ihub-framework-js
Version:
Legacy version of iHub Framework written in Javascript
47 lines (42 loc) • 1.58 kB
JavaScript
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
const fs = require('fs');
const path = require('path');
const log = require('../logger');
const responsePlugins = {};
async function loadPlugins(isToLoadFromFramework) {
return new Promise(async (resolve, reject) => {
const pluginsPath = path.join(process.cwd(), `${isToLoadFromFramework ? 'node_modules/ihub-framework-js/' : ''}plugins`);
if (fs.existsSync(pluginsPath)) {
const plugins = fs.readdirSync(pluginsPath);
await plugins.forEach(async (plugin) => {
if (!plugin.endsWith('.js')) return;
log.debug(`Loading plugin ${plugin} from the ${isToLoadFromFramework ? 'ihub-framework-js' : 'application'}`);
const pluginName = plugin.replace('.js', '');
if (!responsePlugins[pluginName]) {
let currentPLugin;
try {
currentPLugin = await require(path.join(pluginsPath, plugin));
} catch (error) {
log.error(error);
reject(error);
}
if (currentPLugin) responsePlugins[pluginName] = currentPLugin;
} else {
log.debug(`Skipped the loading plugin ${plugin} from application because a plugin with this same name was previously loaded from the ihub-framework`);
}
});
resolve(responsePlugins);
}
resolve();
});
}
module.exports = new Promise(async (resolve, reject) => {
try {
await loadPlugins(true);
await loadPlugins(false);
} catch (error) {
reject(error);
}
resolve(responsePlugins);
});