Skip to content

Commit

Permalink
fix(core): should not change command.name, fix #574
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 4, 2022
1 parent 93d855c commit 2c5d54b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const specs = [
'packages/segment/tests/*.spec.ts',
'packages/utils/tests/*.spec.ts',
'plugins/a11y/admin/tests/*.spec.ts',
'plugins/a11y/commands/tests/*.spec.ts',
'plugins/a11y/rate-limit/tests/*.spec.ts',
'plugins/a11y/schedule/tests/*.spec.ts',
'plugins/a11y/switch/tests/*.spec.ts',
Expand All @@ -25,7 +26,6 @@ const specs = [
'plugins/database/mysql/tests/*.spec.ts',
'plugins/database/sqlite/tests/*.spec.ts',
// 'plugins/eval/tests/*.spec.ts',
'plugins/frontend/commands/tests/*.spec.ts',
// 'plugins/github/tests/*.spec.ts',
'plugins/teach/tests/*.spec.ts',
]
Expand Down
44 changes: 34 additions & 10 deletions packages/core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,52 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
constructor(name: string, decl: string, context: Context) {
super(name, decl, context)
this.config = { ...Command.defaultConfig }
this._registerAlias(name.toLowerCase())
this._registerAlias(name)
context.app._commandList.push(this)
}

get app() {
return this.context.app
}

private _registerAlias(name: string) {
this._aliases.push(name)
get displayName() {
return this._aliases[0]
}

set displayName(name) {
this._registerAlias(name, true)
}

private _registerAlias(name: string, prepend = false) {
name = name.toLowerCase()

// add to list
const done = this._aliases.includes(name)
if (done) {
if (prepend) {
remove(this._aliases, name)
this._aliases.unshift(name)
}
return
} else if (prepend) {
this._aliases.unshift(name)
} else {
this._aliases.push(name)
}

// register global
const previous = this.app.getCommand(name)
if (!previous) {
this.app._commands.set(name, this)
} else if (previous !== this) {
throw new Error(`duplicate command names: "${name}"`)
}

// add disposable
this._disposables?.push(() => {
remove(this._aliases, name)
this.app._commands.delete(name)
})
}

[Symbol.for('nodejs.util.inspect.custom')]() {
Expand All @@ -109,14 +139,8 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev

alias(...names: string[]) {
if (this._disposed) return this
for (const _name of names) {
const name = _name.toLowerCase()
if (this._aliases.includes(name)) continue
for (const name of names) {
this._registerAlias(name)
this._disposables?.push(() => {
remove(this._aliases, name)
this.app._commands.delete(name)
})
}
return this
}
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/internal/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ function* getCommands(session: Session<'authority'>, commands: Command[], showHi
function formatCommands(path: string, session: Session<'authority'>, children: Command[], options: HelpOptions) {
const commands = Array
.from(getCommands(session, children, options.showHidden))
.sort((a, b) => a.name > b.name ? 1 : -1)
.sort((a, b) => a.displayName > b.displayName ? 1 : -1)
if (!commands.length) return []

let hasSubcommand = false
const output = commands.map(({ name, config, children }) => {
let output = ' ' + name
const output = commands.map(({ name, displayName, config, children }) => {
let output = ' ' + displayName
if (options.authority) {
output += ` (${config.authority}${children.length ? (hasSubcommand = true, '*') : ''})`
}
Expand Down Expand Up @@ -148,7 +148,7 @@ function getOptions(command: Command, session: Session<'authority'>, config: Hel
}

async function showHelp(command: Command, session: Session<'authority'>, config: HelpOptions) {
const output = [command.name + command.declaration]
const output = [command.displayName + command.declaration]

const description = session.text([`commands.${command.name}.description`, ''])
if (description) output.push(description)
Expand Down Expand Up @@ -186,7 +186,7 @@ async function showHelp(command: Command, session: Session<'authority'>, config:
output.push(session.text('.command-examples'), ...command._examples.map(example => ' ' + example))
} else {
const text = session.text([`commands.${command.name}.examples`, ''])
output.push(...text.split('\n').map(line => ' ' + line))
if (text) output.push(...text.split('\n').map(line => ' ' + line))
}

output.push(...formatCommands('.subcommand-prolog', session, command.children, config))
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export namespace Argv {
private _namedOptions: OptionDeclarationMap = {}
private _symbolicOptions: OptionDeclarationMap = {}

constructor(public name: string, declaration: string, public context: Context) {
constructor(public readonly name: string, declaration: string, public context: Context) {
if (!name) throw new Error('expect a command name')
const decl = this._arguments = parseDecl(declaration)
this.declaration = decl.stripped
Expand Down
3 changes: 1 addition & 2 deletions plugins/a11y/commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ export function apply(ctx: Context, config: Dict<Config>) {
const _name = locate(target, name)
if (!_name) return
// directly modify name of prototype
target.name = _name
command.alias(_name)
command.displayName = _name
}

for (const name of config.alias) {
Expand Down
12 changes: 6 additions & 6 deletions plugins/a11y/commands/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('@koishijs/plugin-override', () => {
await client.shouldNotReply('bar')
await client.shouldNotReply('baz')

await app.dispose(commands)
app.dispose(commands)
})

it('dispose plugin', async () => {
Expand All @@ -49,7 +49,7 @@ describe('@koishijs/plugin-override', () => {
await client.shouldReply('bar', 'test')
await client.shouldReply('baz', 'test')

await app.dispose(commands)
app.dispose(commands)

await client.shouldReply('bar', 'test')
await client.shouldNotReply('baz')
Expand All @@ -72,7 +72,7 @@ describe('@koishijs/plugin-override', () => {
await client.shouldReply('foo.bar', 'test')
await client.shouldReply('baz', 'test')

await app.dispose(commands)
app.dispose(commands)
await client.shouldReply('foo.bar', 'test')
await client.shouldNotReply('baz')
expect(foo.children).to.have.length(1)
Expand All @@ -94,7 +94,7 @@ describe('@koishijs/plugin-override', () => {
await client.shouldReply('bar', 'test')
await client.shouldReply('baz', 'test')

await app.dispose(commands)
app.dispose(commands)
await client.shouldReply('bar', 'test')
await client.shouldNotReply('baz')
expect(foo.children).to.have.length(0)
Expand All @@ -118,7 +118,7 @@ describe('@koishijs/plugin-override', () => {
expect(baz.children).to.have.length(1)
await client.shouldReply('foo', 'test')

await app.dispose(commands)
app.dispose(commands)
await client.shouldReply('foo', 'test')
expect(bar.children).to.have.length(1)
expect(baz.children).to.have.length(0)
Expand All @@ -143,7 +143,7 @@ describe('@koishijs/plugin-override', () => {
await client.shouldReply('foo', /baz/)
await client.shouldReply('baz', 'test')

await app.dispose(commands)
app.dispose(commands)
await client.shouldNotReply('foo')
await client.shouldNotReply('baz')
await client.shouldReply('bar', 'test')
Expand Down

0 comments on commit 2c5d54b

Please sign in to comment.