@@ -5,56 +5,26 @@ import type { Container } from '../../../../container';
55import { cherryPick , merge , rebase } from '../../../../git/actions/repository' ;
66import type { Repository } from '../../../../git/models/repository' ;
77import { executeCommand } from '../../../../system/-webview/command' ;
8+ import { createCommandDecorator } from '../../../../system/decorators/command' ;
89import type { CliCommandRequest , CliCommandResponse , CliIpcServer } from './integration' ;
910
10- interface CliCommand {
11- command : string ;
12- handler : ( request : CliCommandRequest , repo ?: Repository | undefined ) => Promise < CliCommandResponse > ;
13- }
14-
15- const commandHandlers : CliCommand [ ] = [ ] ;
16- function command ( command : string ) {
17- return function (
18- target : unknown ,
19- contextOrKey ?: string | ClassMethodDecoratorContext ,
20- descriptor ?: PropertyDescriptor ,
21- ) {
22- // ES Decorator
23- if ( contextOrKey && typeof contextOrKey === 'object' && 'kind' in contextOrKey ) {
24- if ( contextOrKey . kind !== 'method' ) {
25- throw new Error ( 'The command decorator can only be applied to methods' ) ;
26- }
11+ type CliCommandHandler = ( request : CliCommandRequest , repo ?: Repository | undefined ) => Promise < CliCommandResponse > ;
2712
28- commandHandlers . push ( { command : command , handler : target as CliCommand [ 'handler' ] } ) ;
29- return ;
30- }
31-
32- // TypeScript experimental decorator
33- if ( descriptor ) {
34- commandHandlers . push ( { command : command , handler : descriptor . value as CliCommand [ 'handler' ] } ) ;
35- return descriptor ;
36- }
37-
38- throw new Error ( 'Invalid decorator usage' ) ;
39- } ;
40- }
13+ const { command, getCommands } = createCommandDecorator < CliCommandHandler > ( ) ;
4114
4215export class CliCommandHandlers implements Disposable {
4316 constructor (
4417 private readonly container : Container ,
4518 private readonly server : CliIpcServer ,
4619 ) {
47- for ( const { command, handler } of commandHandlers ) {
20+ for ( const { command, handler } of getCommands ( ) ) {
4821 this . server . registerHandler ( command , rq => this . wrapHandler ( rq , handler ) ) ;
4922 }
5023 }
5124
5225 dispose ( ) : void { }
5326
54- private wrapHandler (
55- request : CliCommandRequest ,
56- handler : ( request : CliCommandRequest , repo ?: Repository | undefined ) => Promise < CliCommandResponse > ,
57- ) {
27+ private wrapHandler ( request : CliCommandRequest , handler : CliCommandHandler ) {
5828 let repo : Repository | undefined ;
5929 if ( request ?. cwd ) {
6030 repo = this . container . git . getRepository ( request . cwd ) ;
0 commit comments