UNPKG

@mini-umi/core

Version:
75 lines (73 loc) 2.42 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/pluginAPI.ts var pluginAPI_exports = {}; __export(pluginAPI_exports, { PluginAPi: () => PluginAPi }); module.exports = __toCommonJS(pluginAPI_exports); var import_command = require("./command"); var import_hook = require("./hook"); var PluginAPi = class { constructor(opts) { this.onStart = () => { }; this.service = opts.service; this.plugin = opts.plugin; } register(opts) { (this.service.hooks[opts.key] || (this.service.hooks[opts.key] = [])).push(new import_hook.Hook({ ...opts, plugin: this.plugin })); } registerCommand(opts) { const { name } = opts; this.service.commands[name] = new import_command.Command({ ...opts, plugin: this.plugin }); } registerMethod(opts) { this.service.pluginMethods[opts.name] = { plugin: this.plugin, fn: opts.fn || function(fn) { this.register({ key: opts.name, fn }); } }; } static proxyPluginAPI(opts) { return new Proxy(opts.pluginAPI, { get: (target, prop) => { if (opts.service.pluginMethods[prop]) { return opts.service.pluginMethods[prop].fn; } if (opts.serviceProps.includes(prop)) { const serviceProp = opts.service[prop]; return typeof serviceProp === "function" ? serviceProp.bind(opts.service) : serviceProp; } if (prop in opts.staticProps) { return opts.staticProps[prop]; } return target[prop]; } }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { PluginAPi }); //# sourceMappingURL=pluginAPI.js.map