11import { Minimatch } from "minimatch" ;
2+ import type { RequestCallbackOptions } from "talon-rpc" ;
23import * as vscode from "vscode" ;
3-
44import { any } from "./regex" ;
5- import { Request } from "./types" ;
6- import { Io } from "./io" ;
75
86export default class CommandRunner {
97 private allowRegex ! : RegExp ;
108 private denyRegex ! : RegExp | null ;
119 private backgroundWindowProtection ! : boolean ;
1210
13- constructor ( private io : Io ) {
11+ constructor ( ) {
1412 this . reloadConfiguration = this . reloadConfiguration . bind ( this ) ;
1513 this . runCommand = this . runCommand . bind ( this ) ;
1614
1715 this . reloadConfiguration ( ) ;
1816 vscode . workspace . onDidChangeConfiguration ( this . reloadConfiguration ) ;
1917 }
2018
21- reloadConfiguration ( ) {
19+ private reloadConfiguration ( ) {
2220 const allowList = vscode . workspace
2321 . getConfiguration ( "command-server" )
2422 . get < string [ ] > ( "allowList" ) ! ;
@@ -41,77 +39,23 @@ export default class CommandRunner {
4139 . get < boolean > ( "backgroundWindowProtection" ) ! ;
4240 }
4341
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" ) ;
8148 }
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- } ) ;
10949 }
11050
111- await this . io . closeResponse ( ) ;
51+ if ( ! commandId . match ( this . allowRegex ) ) {
52+ throw new Error ( "Command not in allowList" ) ;
53+ }
11254
113- if ( commandPromise != null ) {
114- await commandPromise ;
55+ if ( this . denyRegex != null && commandId . match ( this . denyRegex ) ) {
56+ throw new Error ( "Command in denyList" ) ;
11557 }
58+
59+ return vscode . commands . executeCommand ( commandId , ...args ) ;
11660 }
11761}
0 commit comments