Skip to content

Commit

Permalink
fix: module load logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Jan 7, 2024
1 parent 6df9f60 commit 8b4c508
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 56 deletions.
4 changes: 1 addition & 3 deletions modules/i18n-command/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
"kotori-bot": "workspace:^"
},
"kotori": {
"config": {
"priority": 50
},
"enforce": "pre",
"meta": {
"languages": [
"zh_CN"
Expand Down
12 changes: 3 additions & 9 deletions packages/core/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
51 changes: 25 additions & 26 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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<ReturnType<typeof globalConfigSchemaController>>;

export type AdapterConfig = Tsu.infer<ReturnType<typeof adapterConfigBaseSchemaController>>;

export type ModuleConfig = Tsu.infer<ReturnType<typeof ModuleConfigBaseSchemaController>>;
export type ModuleConfig = Tsu.infer<typeof ModuleConfigBaseSchema>;

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<typeof moduleEnforceSchema>;

export const ModulePackageSchema =
Tsu.Object({
name: Tsu.String().regexp(/kotori-plugin-[a-z]([a-z,0-9]{3,13})\b/),
version: Tsu.String(),
Expand All @@ -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<ReturnType<typeof ModulePackageSchemaController>>;
export type ModulePackage = Tsu.infer<typeof ModulePackageSchema>;

export interface ModuleData {
package: ModulePackage;
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
}

Expand Down
32 changes: 14 additions & 18 deletions packages/loader/src/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';

Expand Down Expand Up @@ -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));
Expand All @@ -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 () => {
Expand Down

0 comments on commit 8b4c508

Please sign in to comment.