3
3
import * as vscode from "vscode" ;
4
4
import * as http from "http" ;
5
5
import { AddressInfo } from "net" ;
6
- import { writeFileSync } from "fs" ;
6
+ import { existsSync , readFileSync , writeFileSync } from "fs" ;
7
7
import { tmpdir } from "os" ;
8
8
import { join } from "path" ;
9
9
import { getRequestJSON } from "./getRequestJSON" ;
@@ -12,6 +12,7 @@ interface Command {
12
12
commandId : string ;
13
13
args : any [ ] ;
14
14
expectResponse : boolean ;
15
+ waitForFinish : boolean ;
15
16
}
16
17
17
18
export function activate ( context : vscode . ExtensionContext ) {
@@ -26,9 +27,23 @@ export function activate(context: vscode.ExtensionContext) {
26
27
27
28
const commandInfo : Command = await getRequestJSON ( req ) ;
28
29
29
- vscode . commands . executeCommand ( commandInfo . commandId , ...commandInfo . args ) ;
30
+ const commandPromise = vscode . commands . executeCommand (
31
+ commandInfo . commandId ,
32
+ ...commandInfo . args
33
+ ) ;
34
+
35
+ var commandReturnValue ;
36
+
37
+ if ( commandInfo . expectResponse || commandInfo . waitForFinish ) {
38
+ commandReturnValue = await commandPromise ;
39
+ }
30
40
31
41
res . writeHead ( 200 ) ;
42
+
43
+ if ( commandInfo . expectResponse ) {
44
+ res . write ( JSON . stringify ( commandReturnValue ) ) ;
45
+ }
46
+
32
47
res . end ( ) ;
33
48
} ) ;
34
49
@@ -43,6 +58,17 @@ export function activate(context: vscode.ExtensionContext) {
43
58
}
44
59
} ) ;
45
60
61
+ setInterval ( ( ) => {
62
+ const path = getPortPath ( ) ;
63
+
64
+ if (
65
+ vscode . window . state . focused &&
66
+ ( ! existsSync ( path ) || parseInt ( readFileSync ( path ) . toString ( ) ) !== port )
67
+ ) {
68
+ writePort ( ) ;
69
+ }
70
+ } , 500 ) ;
71
+
46
72
const windowStateDisposable = vscode . window . onDidChangeWindowState (
47
73
( event ) => {
48
74
if ( event . focused && port !== null ) {
@@ -52,7 +78,7 @@ export function activate(context: vscode.ExtensionContext) {
52
78
) ;
53
79
54
80
function writePort ( ) {
55
- const path = join ( tmpdir ( ) , "vscode-port" ) ;
81
+ const path = getPortPath ( ) ;
56
82
console . log ( `Saving port ${ port } to path ${ path } ` ) ;
57
83
writeFileSync ( path , `${ port } ` ) ;
58
84
}
@@ -62,6 +88,10 @@ export function activate(context: vscode.ExtensionContext) {
62
88
server . close ( ) ;
63
89
} ,
64
90
} ) ;
91
+
92
+ function getPortPath ( ) {
93
+ return join ( tmpdir ( ) , "vscode-port" ) ;
94
+ }
65
95
}
66
96
67
97
// this method is called when your extension is deactivated
0 commit comments