diff --git a/modules/i18n-command/package.json b/modules/i18n-command/package.json index 5702da95..e19e988d 100644 --- a/modules/i18n-command/package.json +++ b/modules/i18n-command/package.json @@ -16,9 +16,7 @@ "kotori-bot": "workspace:^" }, "kotori": { - "config": { - "priority": 50 - }, + "enforce": "pre", "meta": { "languages": [ "zh_CN" diff --git a/packages/core/src/consts.ts b/packages/core/src/consts.ts index 91ee4fc3..9ac46ae3 100644 --- a/packages/core/src/consts.ts +++ b/packages/core/src/consts.ts @@ -20,12 +20,6 @@ export const DEFAULT_COMMAND_PREFIX = '/'; export const DEFAULT_ENV = 'dev'; -export const DEFAULT_PRIORITY = 70; - -export const DEFAULT_CUSTOM_PRIORITY = 40; - -export const DEFAULT_ADAPTER_PRIORITY = 30; - -export const DEFAULT_DATABASE_PRIORITY = 20; - -export const DEFAULT_FILTER = {}; +export const CORE_MODULES = [ + '@kotori-bot/kotori-plugin-core' +] diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index cd511660..784bf2d8 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -9,10 +9,8 @@ import type Elements from './components/elements'; import { DEFAULT_COMMAND_PREFIX, DEFAULT_ENV, - DEFAULT_FILTER, DEFAULT_LANG, DEFAULT_MODULES_DIR, - DEFAULT_PRIORITY, DEFAULT_ROOT_DIR, } from './consts'; import type Service from './components/service'; @@ -65,11 +63,10 @@ const adapterConfigBaseSchemaController = ( CommonConfigSchemaController(lang, commandPrefix), ]); -export const ModuleConfigBaseSchemaController = (priority?: number, filter?: object) => +export const ModuleConfigBaseSchema = Tsu.Object({ - priority: Tsu.Number().min(0).optional().default(priority), - filter: Tsu.Object({}).optional().default(filter), - }); + filter: Tsu.Object({}).default({}), + }).default({ filter: {} }); export const globalConfigSchemaController = ( lang: LocaleType = DEFAULT_LANG, @@ -83,16 +80,20 @@ export const globalConfigSchemaController = ( CommonConfigSchemaController(), ]), adapter: Tsu.Object({}).index(adapterConfigBaseSchemaController(lang, commandPrefix)).default({}), - plugin: Tsu.Object({}).index(ModuleConfigBaseSchemaController()).default({}), + plugin: Tsu.Object({}).index(ModuleConfigBaseSchema).default({}), }); export type GlobalConfig = Tsu.infer>; export type AdapterConfig = Tsu.infer>; -export type ModuleConfig = Tsu.infer>; +export type ModuleConfig = Tsu.infer; + +const moduleEnforceSchema = Tsu.Union([Tsu.Literal('pre'), Tsu.Literal('post')]); -export const ModulePackageSchemaController = (priority: number = DEFAULT_PRIORITY, filter: object = DEFAULT_FILTER) => +export type ModuleEnforce = Tsu.infer; + +export const ModulePackageSchema = Tsu.Object({ name: Tsu.String().regexp(/kotori-plugin-[a-z]([a-z,0-9]{3,13})\b/), version: Tsu.String(), @@ -104,22 +105,20 @@ export const ModulePackageSchemaController = (priority: number = DEFAULT_PRIORIT 'kotori-bot': Tsu.String(), }), kotori: Tsu.Object({ - config: ModuleConfigBaseSchemaController(priority, filter).default({ priority, filter }), + enforce: moduleEnforceSchema.optional(), + config: ModuleConfigBaseSchema, meta: Tsu.Object({ language: Tsu.Array(localeTypeSchema).default([]), service: Tsu.Array(Tsu.String()).default([]), }).default({ language: [], service: [] }), - required: Tsu.Array(Tsu.String()).default([]), - optional: Tsu.Array(Tsu.String()).default([]), }).default({ - config: { priority, filter }, + enforce: undefined, + config: { filter: {} }, meta: { language: [], service: [] }, - required: [], - optional: [], }), }); -export type ModulePackage = Tsu.infer>; +export type ModulePackage = Tsu.infer; export interface ModuleData { package: ModulePackage; @@ -241,14 +240,14 @@ export interface CommandData { } /* export enum CommandResult { - SUCCESS = 0, - OPTION_ERROR, - ARG_ERROR, - MANY_ARG, - FEW_ARG, - SYNTAX, - UNKNOWN, - ERROR, + SUCCESS = 0, + OPTION_ERROR, + ARG_ERROR, + MANY_ARG, + FEW_ARG, + SYNTAX, + UNKNOWN, + ERROR, } */ export interface CommandParseResult { parsed: { @@ -408,8 +407,8 @@ interface EventDataBeforeSend extends EventDataBase<'before_send'> { interface EventDataSend extends EventDataBase<'send'> { api: Api; /* message: MessageRaw; - messageType: MessageScope; - targetId: EventDataTargetId; */ + messageType: MessageScope; + targetId: EventDataTargetId; */ messageId: EventDataTargetId; } diff --git a/packages/loader/src/modules.ts b/packages/loader/src/modules.ts index 4506ca9b..f1f2f7a0 100644 --- a/packages/loader/src/modules.ts +++ b/packages/loader/src/modules.ts @@ -2,17 +2,14 @@ import fs from 'fs'; import path from 'path'; import { ADAPTER_PREFIX, + CORE_MODULES, CUSTOM_PREFIX, Context, DATABASE_PREFIX, - DEFAULT_ADAPTER_PRIORITY, - DEFAULT_CUSTOM_PRIORITY, - DEFAULT_DATABASE_PRIORITY, - DEFAULT_PRIORITY, DevError, ModuleData, ModulePackage, - ModulePackageSchemaController, + ModulePackageSchema, OFFICIAL_MODULES_SCOPE, PLUGIN_PREFIX, clearObject, @@ -28,13 +25,6 @@ declare module '@kotori-bot/core' { } } -function getDefaultPriority(pkgName: string) { - if (pkgName.includes(DATABASE_PREFIX)) return DEFAULT_DATABASE_PRIORITY; - if (pkgName.includes(ADAPTER_PREFIX)) return DEFAULT_ADAPTER_PRIORITY; - if (pkgName.includes(CUSTOM_PREFIX)) return DEFAULT_CUSTOM_PRIORITY; - return DEFAULT_PRIORITY; -} - export class Modules extends Context { private isDev = this.options.env === 'dev'; @@ -74,12 +64,12 @@ export class Modules extends Context { } catch { throw new DevError(`illegal package.json ${packagePath}`); } - const result = ModulePackageSchemaController().parseSafe(packageJson); + const result = ModulePackageSchema.parseSafe(packageJson); if (!result.value) { if (rootDir !== this.baseDir.modules) return; throw new DevError(`package.json format error ${packagePath}: ${result.error.message}`); } - packageJson = ModulePackageSchemaController(getDefaultPriority(this.package.name), {}).parse(result.data); + packageJson = result.data; const mainPath = path.join(dir, this.isDev ? DEV_IMPORT : packageJson.main); if (!fs.existsSync(mainPath)) throw new DevError(`cannot find ${mainPath}`); const codeDirs = path.join(dir, this.isDev ? DEV_CODE_DIRS : path.dirname(packageJson.main)); @@ -104,11 +94,17 @@ export class Modules extends Context { this.moduleRootDir.forEach(dir => { this.getModuleList(dir); }); - const array = this.moduleStack.filter(data => data.package.name.startsWith(OFFICIAL_MODULES_SCOPE)); - array.push(...this.moduleStack.filter(data => !array.includes(data))); - array.forEach(moduleData => { + const handle = (pkg: ModulePackage) => { + if (pkg.name.includes(DATABASE_PREFIX)) return 1; + if (pkg.name.includes(ADAPTER_PREFIX)) return 2; + if (CORE_MODULES.includes(pkg.name)) return 3; + if (pkg.kotori.enforce === 'pre') return 4; + if (!pkg.kotori.enforce) return 5; + return 5; + } + this.moduleStack.sort((el1, el2) => handle(el1.package) - handle(el2.package)).forEach(moduleData => { this.moduleQuick(moduleData); - }); + }) }; public readonly watchFile = async () => {