mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-14 05:45:02 +00:00
Auto-generate plugin docs (WIP)
This commit is contained in:
parent
6bdb05e678
commit
ee6d622941
44 changed files with 599 additions and 150 deletions
70
src/api/docs.ts
Normal file
70
src/api/docs.ts
Normal file
|
@ -0,0 +1,70 @@
|
|||
import express from "express";
|
||||
import { availablePlugins } from "../plugins/availablePlugins";
|
||||
import { ZeppelinPlugin } from "../plugins/ZeppelinPlugin";
|
||||
import { notFound } from "./responses";
|
||||
import { CommandManager, ICommandConfig } from "knub/dist/CommandManager";
|
||||
|
||||
const commandManager = new CommandManager();
|
||||
|
||||
export function initDocs(app: express.Express) {
|
||||
const docsPlugins = availablePlugins.filter(pluginClass => pluginClass.showInDocs);
|
||||
|
||||
app.get("/docs/plugins", (req: express.Request, res: express.Response) => {
|
||||
res.json(
|
||||
docsPlugins.map(pluginClass => {
|
||||
const thinInfo = pluginClass.pluginInfo ? { prettyName: pluginClass.pluginInfo.prettyName } : {};
|
||||
return {
|
||||
name: pluginClass.pluginName,
|
||||
info: thinInfo,
|
||||
};
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
app.get("/docs/plugins/:pluginName", (req: express.Request, res: express.Response) => {
|
||||
const pluginClass = docsPlugins.find(obj => obj.pluginName === req.params.pluginName);
|
||||
if (!pluginClass) {
|
||||
return notFound(res);
|
||||
}
|
||||
|
||||
const props = Reflect.ownKeys(pluginClass.prototype);
|
||||
const commands = props.reduce((arr, prop) => {
|
||||
if (typeof prop !== "string") return arr;
|
||||
const propCommands = Reflect.getMetadata("commands", pluginClass.prototype, prop);
|
||||
if (propCommands) {
|
||||
arr.push(
|
||||
...propCommands.map(cmd => {
|
||||
const trigger = typeof cmd.command === "string" ? cmd.command : cmd.command.source;
|
||||
const parameters = cmd.parameters
|
||||
? typeof cmd.parameters === "string"
|
||||
? commandManager.parseParameterString(cmd.parameters)
|
||||
: cmd.parameters
|
||||
: [];
|
||||
const config: ICommandConfig = cmd.options || {};
|
||||
if (config.overloads) {
|
||||
config.overloads = config.overloads.map(overload => {
|
||||
return typeof overload === "string" ? commandManager.parseParameterString(overload) : overload;
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
trigger,
|
||||
parameters,
|
||||
config,
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
return arr;
|
||||
}, []);
|
||||
|
||||
const options = (pluginClass as typeof ZeppelinPlugin).getStaticDefaultOptions();
|
||||
|
||||
res.json({
|
||||
name: pluginClass.pluginName,
|
||||
info: pluginClass.pluginInfo || {},
|
||||
options,
|
||||
commands,
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue