diff --git a/.mocharc.js b/.mocharc.js index 683fb00e30..bc4e01a4e3 100644 --- a/.mocharc.js +++ b/.mocharc.js @@ -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', @@ -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', ] diff --git a/packages/core/src/command.ts b/packages/core/src/command.ts index e2dd8b2b0b..8185d43676 100644 --- a/packages/core/src/command.ts +++ b/packages/core/src/command.ts @@ -75,7 +75,7 @@ export class Command { + remove(this._aliases, name) + this.app._commands.delete(name) + }) } [Symbol.for('nodejs.util.inspect.custom')]() { @@ -109,14 +139,8 @@ export class Command { - remove(this._aliases, name) - this.app._commands.delete(name) - }) } return this } diff --git a/packages/core/src/internal/help.ts b/packages/core/src/internal/help.ts index 3faa1f4b81..7d8bd77d5d 100644 --- a/packages/core/src/internal/help.ts +++ b/packages/core/src/internal/help.ts @@ -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, '*') : ''})` } @@ -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) @@ -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)) diff --git a/packages/core/src/parser.ts b/packages/core/src/parser.ts index 9460beffac..1f4eb7ff8b 100644 --- a/packages/core/src/parser.ts +++ b/packages/core/src/parser.ts @@ -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 diff --git a/plugins/a11y/commands/src/index.ts b/plugins/a11y/commands/src/index.ts index 89fc783f7f..1b2816a447 100644 --- a/plugins/a11y/commands/src/index.ts +++ b/plugins/a11y/commands/src/index.ts @@ -76,8 +76,7 @@ export function apply(ctx: Context, config: Dict) { 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) { diff --git a/plugins/a11y/commands/tests/index.spec.ts b/plugins/a11y/commands/tests/index.spec.ts index 8ed367bafe..5b00636459 100644 --- a/plugins/a11y/commands/tests/index.spec.ts +++ b/plugins/a11y/commands/tests/index.spec.ts @@ -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 () => { @@ -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') @@ -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) @@ -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) @@ -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) @@ -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')