bb-inspired
Version:
Core library for BB-inspired NestJS backend
157 lines • 7.07 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var PluginLoader_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginLoader = void 0;
const common_1 = require("@nestjs/common");
const fs = require("fs");
const path = require("path");
const plugin_manager_1 = require("./plugin.manager");
const logger_1 = require("../../utils/logger");
let PluginLoader = PluginLoader_1 = class PluginLoader {
constructor(options = {}, pluginManager) {
this.options = options;
this.pluginManager = pluginManager;
this.logger = new logger_1.AppLogger(PluginLoader_1.name);
}
async discoverAndLoadPlugins() {
const pluginsDir = this.options.pluginsDir;
if (!pluginsDir) {
this.logger.warn('No plugins directory specified');
return [];
}
if (!fs.existsSync(pluginsDir)) {
this.logger.warn(`Plugins directory ${pluginsDir} does not exist`);
return [];
}
try {
const entries = fs.readdirSync(pluginsDir, { withFileTypes: true });
const pluginDirs = entries
.filter(entry => entry.isDirectory())
.map(entry => entry.name);
this.logger.log(`Discovered ${pluginDirs.length} potential plugins in ${pluginsDir}`);
const loadedPlugins = [];
for (const pluginDir of pluginDirs) {
const pluginPath = path.join(pluginsDir, pluginDir);
try {
const loaded = await this.loadPlugin(pluginPath);
if (loaded) {
loadedPlugins.push(loaded);
}
}
catch (error) {
this.logger.error(`Error loading plugin from ${pluginPath}: ${error.message}`, error.stack);
}
}
this.logger.log(`Successfully loaded ${loadedPlugins.length} plugins`);
return loadedPlugins;
}
catch (error) {
this.logger.error(`Error discovering plugins in ${pluginsDir}: ${error.message}`, error.stack);
return [];
}
}
async loadPlugin(pluginPath) {
var _a;
try {
const manifestPath = path.join(pluginPath, 'plugin.json');
if (!fs.existsSync(manifestPath)) {
this.logger.warn(`No plugin.json found in ${pluginPath}`);
return null;
}
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
if (!manifest.name || !manifest.version) {
this.logger.warn(`Invalid plugin manifest in ${pluginPath}: missing name or version`);
return null;
}
const entryPoint = this.findEntryPoint(pluginPath);
if (!entryPoint) {
this.logger.warn(`No entry point found for plugin ${manifest.name} in ${pluginPath}`);
return null;
}
const pluginModule = require(entryPoint);
const pluginExport = pluginModule.default || pluginModule;
if (!pluginExport || typeof pluginExport !== 'function') {
this.logger.warn(`Invalid plugin export in ${entryPoint}`);
return null;
}
const pluginInstance = pluginExport();
const pluginConfig = ((_a = this.options.pluginConfig) === null || _a === void 0 ? void 0 : _a[manifest.name]) || {};
const registrationOptions = {
metadata: {
name: manifest.name,
version: manifest.version,
description: manifest.description || '',
author: manifest.author || '',
dependencies: manifest.dependencies || [],
tags: manifest.tags || [],
},
config: pluginConfig,
hooks: pluginInstance.hooks || {},
lifecycle: {
onLoad: pluginInstance.onLoad,
onEnable: pluginInstance.onEnable,
onDisable: pluginInstance.onDisable,
},
};
await this.pluginManager.register(registrationOptions);
if (this.options.loadAll) {
await this.pluginManager.enable(manifest.name);
}
this.logger.log(`Loaded plugin: ${manifest.name} v${manifest.version} from ${pluginPath}`);
return manifest.name;
}
catch (error) {
this.logger.error(`Error loading plugin from ${pluginPath}: ${error.message}`, error.stack);
return null;
}
}
findEntryPoint(pluginPath) {
const possibleEntryPoints = [
path.join(pluginPath, 'dist', 'index.js'),
path.join(pluginPath, 'lib', 'index.js'),
path.join(pluginPath, 'build', 'index.js'),
path.join(pluginPath, 'index.js'),
];
for (const entryPoint of possibleEntryPoints) {
if (fs.existsSync(entryPoint)) {
return entryPoint;
}
}
const packageJsonPath = path.join(pluginPath, 'package.json');
if (fs.existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
if (packageJson.main) {
const mainPath = path.join(pluginPath, packageJson.main);
if (fs.existsSync(mainPath)) {
return mainPath;
}
}
}
catch (error) {
this.logger.error(`Error parsing package.json in ${pluginPath}: ${error.message}`);
}
}
return null;
}
};
exports.PluginLoader = PluginLoader;
exports.PluginLoader = PluginLoader = PluginLoader_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Optional)()),
__param(0, (0, common_1.Inject)('PLUGIN_OPTIONS')),
__metadata("design:paramtypes", [Object, plugin_manager_1.PluginManagerMain])
], PluginLoader);
//# sourceMappingURL=plugin.loader.js.map