Skip to content

Commit

Permalink
add plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
taisan11 committed Aug 6, 2024
1 parent d4126b1 commit 46fd415
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 59 deletions.
2 changes: 2 additions & 0 deletions src/module/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type Config = {
driver:"unstorage"|"db"
UnstorageOptions?:Driver
drizzle:ReturnType<typeof drizzle>;
plugins?: Plugin[];
};
limit?: {
MaxSubject: number;
Expand Down Expand Up @@ -72,6 +73,7 @@ export type Config = {
import configa from "../../data/system.config";
import { RuntimeName, runtimeInfo } from "std-env";
import { Driver } from "unstorage";
import { Plugin } from "./plugin";
export function config():Config{
configa.preference.site.use = runtimeInfo?.name ||"other"|| configa.preference.site.use;
if (configa.preference.limit) {
Expand Down
45 changes: 24 additions & 21 deletions src/module/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
// const paths:string[] = await init();
import { config } from "./config";

export async function exic(type: number, data:{name:string,mail:string,message:string}): Promise<{code:number,data:{name:string,mail:string,message:string}}> {
return {code:10,data}
const result: any[] = [];
//@ts-ignore
for (const path of paths) {
const plugin = await import(path);
if (plugin.type.includes(type)) {
result.push(plugin);
}
// return {code:10,data}
const plugins = config().preference.site.plugins;
if (!plugins) {
return {code:11,data}
}
let resultData: { code: number, data: { name: string, mail: string, message: string } } | null = null;
for (const plugin of result) {
const module = await import(plugin.path);
const pluginResult = await module.main(type,{ data });
//@ts-ignore
if (resultData === null || pluginResult.code > resultData.code) {
resultData = pluginResult;
let result = {code:0,data}
config().preference.site.plugins!.forEach(plugin => {
if (plugin.PluginInfo().type.includes(type)) {
const a = plugin.main(type, result.data);
if (a.code !== 200) {
result = a;
}
}
}
console.log(resultData!)
return resultData!;
});
return result;
}
export type PluginInfo = {

type PluginInfo = {
/**
* @description プラグイン名
* @example och_template_plugin
Expand All @@ -42,4 +38,11 @@ export type PluginInfo = {
* @deprecated
*/
version?: string,
}
};

type main = (type: number, data: { name: string, mail: string, message: string }) => { code: number, data: { name: string, mail: string, message: string } };

export type Plugin = {
PluginInfo: () => PluginInfo,
main: main
};
33 changes: 19 additions & 14 deletions src/plugin/och_template_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
* @since 20xx-xx-xx
*/

import { PluginInfo } from "../module/plugin";
import { Plugin } from "../module/plugin";

export const info:PluginInfo = {
name: 'och_template_plugin',
description: 'サンプルプラグイン',
type: [1,2],
}

/**
* @description プラグインのメイン処理
* @param {object} - プラグインに渡されるデータ
* @returns {} - プラグインの処理結果
*/
export function main(type: number, data:{name:string,mail:string,message:string}):{code:number,data:{name:string,mail:string,message:string}} {
return {code:0, data}
export default function och_template_plugin(): Plugin {
return {
PluginInfo: () => ({
name: "och_template_plugin",
description: "サンプルプラグイン",
type: [1, 2],
}),
main: (type, data) => {
return {
code: 200,
data: {
name: data.name,
mail: data.mail,
message: data.message,
},
};
},
};
}
46 changes: 22 additions & 24 deletions src/plugin/och_test_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@
* @since 2024-07-03
*/

import { PluginInfo } from "../module/plugin";
import { Plugin } from "../module/plugin";

export const info:PluginInfo = {
name: 'och_test_plugin',
description: 'サンプルプラグイン',
type: [1,2],
}

/**
* @description プラグインのメイン処理
* @param {object} - プラグインに渡されるデータ
* @returns {} - プラグインの処理結果
*/
export function main(type: number, data:{name:string,mail:string,message:string}):{code:number,data:{name:string,mail:string,message:string}} {
console.log("running och_test_plugin")
if (type === 1) {
return {code:0, data}
} else
if (type === 2) {
let {name,mail,message} = data
name = name + 'さん'
return {code:0, data:{name,mail,message}}
}else {
return {code:1, data}
}
export default function och_test_plugin(): Plugin {
return {
PluginInfo: () => ({
name: "och_test_plugin",
description: "テスト用プラグイン",
type: [1, 2],
}),
main: (type, data) => {
if (type === 2) {
console.log(data)
}
return {
code: 200,
data: {
name: data.name,
mail: data.mail,
message: data.message,
},
};
},
};
}

0 comments on commit 46fd415

Please sign in to comment.