1
1
import { Minimatch } from "minimatch" ;
2
+ import type { RequestCallbackOptions } from "talon-rpc" ;
2
3
import * as vscode from "vscode" ;
3
-
4
4
import { any } from "./regex" ;
5
- import { Request } from "./types" ;
6
- import { Io } from "./io" ;
7
5
8
6
export default class CommandRunner {
9
7
private allowRegex ! : RegExp ;
10
8
private denyRegex ! : RegExp | null ;
11
9
private backgroundWindowProtection ! : boolean ;
12
10
13
- constructor ( private io : Io ) {
11
+ constructor ( ) {
14
12
this . reloadConfiguration = this . reloadConfiguration . bind ( this ) ;
15
13
this . runCommand = this . runCommand . bind ( this ) ;
16
14
17
15
this . reloadConfiguration ( ) ;
18
16
vscode . workspace . onDidChangeConfiguration ( this . reloadConfiguration ) ;
19
17
}
20
18
21
- reloadConfiguration ( ) {
19
+ private reloadConfiguration ( ) {
22
20
const allowList = vscode . workspace
23
21
. getConfiguration ( "command-server" )
24
22
. get < string [ ] > ( "allowList" ) ! ;
@@ -41,77 +39,23 @@ export default class CommandRunner {
41
39
. get < boolean > ( "backgroundWindowProtection" ) ! ;
42
40
}
43
41
44
- /**
45
- * Reads a command from the request file and executes it. Writes information
46
- * about command execution to the result of the command to the response file,
47
- * If requested, will wait for command to finish, and can also write command
48
- * output to response file. See also documentation for Request / Response
49
- * types.
50
- */
51
- async runCommand ( ) {
52
- await this . io . prepareResponse ( ) ;
53
-
54
- let request : Request ;
55
-
56
- try {
57
- request = await this . io . readRequest ( ) ;
58
- } catch ( err ) {
59
- await this . io . closeResponse ( ) ;
60
- throw err ;
61
- }
62
-
63
- const { commandId, args, uuid, returnCommandOutput, waitForFinish } =
64
- request ;
65
-
66
- const warnings = [ ] ;
67
-
68
- let commandPromise : Thenable < unknown > | undefined ;
69
-
70
- try {
71
- if ( ! vscode . window . state . focused ) {
72
- if ( this . backgroundWindowProtection ) {
73
- throw new Error ( "This editor is not active" ) ;
74
- } else {
75
- warnings . push ( "This editor is not active" ) ;
76
- }
77
- }
78
-
79
- if ( ! commandId . match ( this . allowRegex ) ) {
80
- throw new Error ( "Command not in allowList" ) ;
42
+ runCommand ( commandId : string , args : any [ ] , options : RequestCallbackOptions ) {
43
+ if ( ! vscode . window . state . focused ) {
44
+ if ( this . backgroundWindowProtection ) {
45
+ throw new Error ( "This editor is not active" ) ;
46
+ } else {
47
+ options . warn ( "This editor is not active" ) ;
81
48
}
82
-
83
- if ( this . denyRegex != null && commandId . match ( this . denyRegex ) ) {
84
- throw new Error ( "Command in denyList" ) ;
85
- }
86
-
87
- commandPromise = vscode . commands . executeCommand ( commandId , ...args ) ;
88
-
89
- let commandReturnValue = null ;
90
-
91
- if ( returnCommandOutput ) {
92
- commandReturnValue = await commandPromise ;
93
- } else if ( waitForFinish ) {
94
- await commandPromise ;
95
- }
96
-
97
- await this . io . writeResponse ( {
98
- error : null ,
99
- uuid,
100
- returnValue : commandReturnValue ,
101
- warnings,
102
- } ) ;
103
- } catch ( err ) {
104
- await this . io . writeResponse ( {
105
- error : ( err as Error ) . message ,
106
- uuid,
107
- warnings,
108
- } ) ;
109
49
}
110
50
111
- await this . io . closeResponse ( ) ;
51
+ if ( ! commandId . match ( this . allowRegex ) ) {
52
+ throw new Error ( "Command not in allowList" ) ;
53
+ }
112
54
113
- if ( commandPromise != null ) {
114
- await commandPromise ;
55
+ if ( this . denyRegex != null && commandId . match ( this . denyRegex ) ) {
56
+ throw new Error ( "Command in denyList" ) ;
115
57
}
58
+
59
+ return vscode . commands . executeCommand ( commandId , ...args ) ;
116
60
}
117
61
}
0 commit comments