@storm-stack/plugin-system
Version:
A library used to create and manage a plugin-styled architecture in a TypeScript application.
56 lines (55 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PluginLoader = void 0;
var _dateTime = require("@storm-stack/date-time");
var _errors = require("@storm-stack/errors");
var _errors2 = require("../errors.cjs");
var _createResolver = require("../utilities/create-resolver.cjs");
class PluginLoader {
constructor(rootPath, tsconfig, autoInstall) {
this.rootPath = rootPath;
this.tsconfig = tsconfig;
this.autoInstall = autoInstall;
this.resolver = (0, _createResolver.createResolver)(tsconfig);
}
resolver;
load = async (definition, options = {}) => {
const module = await this.resolve(definition, options);
if (!this.isValid(module)) {
throw new _errors.StormError(_errors2.PluginSystemErrorCode.plugin_loading_failure, {
message: `The plugin module "${definition.provider}" does not contain the required exports.`
});
}
return this.instantiate(definition, module, definition.provider, options);
};
// eslint-disable-next-line class-methods-use-this
isValid = module => {
if (!module) {
return false;
}
return true;
};
instantiate = (definition, module, resolvedPath, options = {}) => {
const instance = {
loader: this,
definition,
module,
options,
resolvedPath,
executionDateTime: _dateTime.StormDateTime.minimum()
};
return instance;
};
resolve = async (definition, _ = {}) => {
const resolved = await this.resolver(definition.provider);
if (!resolved) {
throw new _errors.StormError(_errors2.PluginSystemErrorCode.plugin_loading_failure, {
message: `Cannot find plugin ${definition.provider}`
});
}
return await Promise.resolve(`${resolved}`).then(s => require(s));
};
}
exports.PluginLoader = PluginLoader;