Skip to content

Commit

Permalink
refactor(core): enhance argv typings
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 22, 2021
1 parent db51775 commit 9139e7a
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 189 deletions.
14 changes: 7 additions & 7 deletions packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger, coerce, Time, template, remove } from 'koishi-utils'
import { Argv, Domain } from './parser'
import { Argv } from './parser'
import { Context, Disposable, NextFunction } from './context'
import { User, Channel } from './database'
import { FieldCollector, Session } from './session'
Expand Down Expand Up @@ -55,7 +55,7 @@ export namespace Command {
= string | ((session: Session<U, G>) => string | Promise<string>)
}

export class Command<U extends User.Field = never, G extends Channel.Field = never, A extends any[] = any[], O extends {} = {}> extends Domain.CommandBase {
export class Command<U extends User.Field = never, G extends Channel.Field = never, A extends any[] = any[], O extends {} = {}> extends Argv.CommandBase {
config: Command.Config
children: Command[] = []
parent: Command = null
Expand All @@ -78,7 +78,7 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
minInterval: 0,
}

static defaultOptionConfig: Domain.OptionConfig = {
static defaultOptionConfig: Argv.OptionConfig = {
authority: 0,
}

Expand Down Expand Up @@ -154,8 +154,8 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
return this
}

subcommand<D extends string>(def: D, config?: Command.Config): Command<never, never, Domain.ArgumentType<D>>
subcommand<D extends string>(def: D, desc: string, config?: Command.Config): Command<never, never, Domain.ArgumentType<D>>
subcommand<D extends string>(def: D, config?: Command.Config): Command<never, never, Argv.ArgumentType<D>>
subcommand<D extends string>(def: D, desc: string, config?: Command.Config): Command<never, never, Argv.ArgumentType<D>>
subcommand(def: string, ...args: any[]) {
def = this.name + (def.charCodeAt(0) === 46 ? '' : '/') + def
const desc = typeof args[0] === 'string' ? args.shift() as string : ''
Expand All @@ -174,10 +174,10 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
return this
}

option<K extends string, D extends string, T extends Domain.Type>(name: K, desc: D, config: Domain.OptionConfig<T> = {}) {
option<K extends string, D extends string, T extends Argv.Type>(name: K, desc: D, config: Argv.OptionConfig<T> = {}) {
this._createOption(name, desc, config)
this._disposables?.push(() => this.removeOption(name))
return this as Command<U, G, A, Extend<O, K, Domain.OptionType<D, T>>>
return this as Command<U, G, A, Extend<O, K, Argv.OptionType<D, T>>>
}

match(session: Session) {
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Logger, defineProperty, remove, segment, Random } from 'koishi-utils'
import { Command } from './command'
import { Session } from './session'
import { User, Channel, Database, Assets } from './database'
import { Argv, Domain } from './parser'
import { Argv } from './parser'
import { Platform, Bot } from './adapter'
import { App } from './app'
import { inspect } from 'util'
Expand Down Expand Up @@ -394,8 +394,8 @@ export class Context {
return this.createTimerDispose(setInterval(callback, ms, ...args))
}

command<D extends string>(def: D, config?: Command.Config): Command<never, never, Domain.ArgumentType<D>>
command<D extends string>(def: D, desc: string, config?: Command.Config): Command<never, never, Domain.ArgumentType<D>>
command<D extends string>(def: D, config?: Command.Config): Command<never, never, Argv.ArgumentType<D>>
command<D extends string>(def: D, desc: string, config?: Command.Config): Command<never, never, Argv.ArgumentType<D>>
command(def: string, ...args: [Command.Config?] | [string, Command.Config?]) {
const desc = typeof args[0] === 'string' ? args.shift() as string : ''
const config = args[0] as Command.Config
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TableType } from './database'
import { Session, FieldCollector } from './session'
import { template } from 'koishi-utils'
import { Context } from './context'
import { Domain } from './parser'
import { Argv } from './parser'

interface HelpConfig {
showHidden?: boolean
Expand Down Expand Up @@ -113,7 +113,7 @@ function formatCommands(path: string, session: Session<ValidationField>, childre
return output
}

function getOptionVisibility(option: Domain.OptionConfig, session: Session<ValidationField>) {
function getOptionVisibility(option: Argv.OptionConfig, session: Session<ValidationField>) {
if (session.user && option.authority > session.user.authority) return false
return !session.resolveValue(option.hidden)
}
Expand Down
Loading

0 comments on commit 9139e7a

Please sign in to comment.