Commit 77abbc29 authored by nanahira's avatar nanahira

add @If

parent 746d57aa
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"koishi-decorators": "^1.1.1", "koishi-decorators": "^1.1.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.0", "schemastery-gen": "^3.1.1",
"typed-reflector": "^1.0.9" "typed-reflector": "^1.0.9"
}, },
"devDependencies": { "devDependencies": {
...@@ -5786,9 +5786,9 @@ ...@@ -5786,9 +5786,9 @@
"peer": true "peer": true
}, },
"node_modules/schemastery-gen": { "node_modules/schemastery-gen": {
"version": "3.1.0", "version": "3.1.1",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.0.tgz", "resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.1.tgz",
"integrity": "sha512-rVvv/c47aKU40f49+MWiM/anoZoKgo6+54jn4XL3frren/KISwklsJ9EstkVwUGeGiwxekP4ZNlWGS/3k0n+Aw==", "integrity": "sha512-zwNQR9VL8o0QE9kDN6I0y9c7kqdGxolVAdJqn6FVW2epW8CyVY+h1gMkAvABvP0gT3Aj/fATNj0cG+ampTgMkw==",
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
...@@ -11198,9 +11198,9 @@ ...@@ -11198,9 +11198,9 @@
"peer": true "peer": true
}, },
"schemastery-gen": { "schemastery-gen": {
"version": "3.1.0", "version": "3.1.1",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.0.tgz", "resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.1.tgz",
"integrity": "sha512-rVvv/c47aKU40f49+MWiM/anoZoKgo6+54jn4XL3frren/KISwklsJ9EstkVwUGeGiwxekP4ZNlWGS/3k0n+Aw==", "integrity": "sha512-zwNQR9VL8o0QE9kDN6I0y9c7kqdGxolVAdJqn6FVW2epW8CyVY+h1gMkAvABvP0gT3Aj/fATNj0cG+ampTgMkw==",
"requires": { "requires": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
......
...@@ -5,5 +5,5 @@ export class BasePlugin<C, PC = Partial<C>> { ...@@ -5,5 +5,5 @@ export class BasePlugin<C, PC = Partial<C>> {
constructor(protected ctx: Context, config: PC) {} constructor(protected ctx: Context, config: PC) {}
@InjectConfig() @InjectConfig()
protected config: C; config: C;
} }
...@@ -2,6 +2,7 @@ import 'reflect-metadata'; ...@@ -2,6 +2,7 @@ import 'reflect-metadata';
import { App, Context, Selection } from 'koishi'; import { App, Context, Selection } from 'koishi';
import { Metadata } from './meta/metadata.decorators'; import { Metadata } from './meta/metadata.decorators';
import { import {
Condition,
KoishiAddUsingList, KoishiAddUsingList,
KoishiPartialUsing, KoishiPartialUsing,
KoishiServiceInjectSym, KoishiServiceInjectSym,
...@@ -120,3 +121,6 @@ export function UsingService( ...@@ -120,3 +121,6 @@ export function UsingService(
} }
}; };
} }
export const If = <T>(func: Condition<boolean, T>): MethodDecorator =>
Metadata.append('KoishiIf', func);
// metadatas // metadatas
import { Context } from 'koishi'; import { Context } from 'koishi';
import { ProvideDefinition, SystemInjectFun } from './interfaces'; import { Condition, ProvideDefinition, SystemInjectFun } from './interfaces';
export const KoishiServiceInjectSym = 'KoishiServiceInjectSym'; export const KoishiServiceInjectSym = 'KoishiServiceInjectSym';
export const KoishiServiceInjectSymKeys = 'KoishiServiceInjectSymKeys'; export const KoishiServiceInjectSymKeys = 'KoishiServiceInjectSymKeys';
...@@ -18,6 +18,7 @@ export interface MetadataArrayMap { ...@@ -18,6 +18,7 @@ export interface MetadataArrayMap {
KoishiSystemInjectSymKeys: string; KoishiSystemInjectSymKeys: string;
KoishiAddUsingList: keyof Context.Services; KoishiAddUsingList: keyof Context.Services;
KoishiPartialUsing: keyof Context.Services; KoishiPartialUsing: keyof Context.Services;
KoishiIf: Condition<boolean>;
} }
export interface MetadataMap { export interface MetadataMap {
......
...@@ -15,3 +15,9 @@ export interface ProvideOptions { ...@@ -15,3 +15,9 @@ export interface ProvideOptions {
export interface ProvideDefinition extends ProvideOptions { export interface ProvideDefinition extends ProvideOptions {
serviceName: keyof Context.Services; serviceName: keyof Context.Services;
} }
export type Condition<R, T = any> = (
o: T,
config: T extends { config: infer C } ? C : any,
ctx: Context,
) => R;
...@@ -113,6 +113,13 @@ export function DefinePlugin<T = any>( ...@@ -113,6 +113,13 @@ export function DefinePlugin<T = any>(
methodKey, methodKey,
false, false,
); );
const conditions = reflector.getArray('KoishiIf', this, methodKey);
if (
conditions.some(
(condition) => !condition(this, this.__config as any, this.__ctx),
)
)
return;
const partialUsing = reflector.getArray( const partialUsing = reflector.getArray(
KoishiPartialUsing, KoishiPartialUsing,
this, this,
......
import { DefinePlugin } from '../src/register';
import { OnGuild, UseCommand } from 'koishi-decorators';
import { BasePlugin } from '../dist';
import { If } from '../src/decorators';
import { App } from 'koishi';
@DefinePlugin()
class MyPlugin extends BasePlugin<{ foo: boolean; bar: boolean }> {
@If<MyPlugin>((o, config, ctx) => config.foo)
@UseCommand('foo')
foo() {
return 'foo';
}
@If<MyPlugin>((o, config, ctx) => config.bar)
@UseCommand('bar')
bar() {
return 'bar';
}
}
describe('It should register conditionally', () => {
it('register command on condition', async () => {
const app = new App();
app.plugin(MyPlugin, { foo: true, bar: false });
await app.start();
const commandFoo = app.command('foo');
const commandBar = app.command('bar');
expect(await commandFoo.execute({})).toBe('foo');
expect(await commandBar.execute({})).toBeFalsy();
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment