@@ -11,63 +11,66 @@ interface Command {
11
11
commandId : string ;
12
12
args : any [ ] ;
13
13
expectResponse : boolean ;
14
- timestamp : Date ;
15
14
}
16
15
17
- // this method is called when your extension is activated
18
- // your extension is activated the very first time the command is executed
19
- export function activate ( context : vscode . ExtensionContext ) {
20
- // Use the console to output diagnostic information (console.log) and errors (console.error)
21
- // This line of code will only be executed once when your extension is activated
22
- console . log (
23
- 'Congratulations, your extension "command-server" is now active!'
24
- ) ;
25
-
26
- var port : number | null = null ;
27
- //create a server object:
28
- const server = http . createServer ( function ( req , res ) {
29
- console . log ( "Got request" ) ;
16
+ function getBody ( req : http . IncomingMessage ) {
17
+ return new Promise < any > ( ( resolve , reject ) => {
30
18
var body = "" ;
31
19
req . on ( "data" , function ( chunk ) {
32
20
body += chunk ;
33
21
} ) ;
34
- req . on ( "end" , function ( ) {
35
- console . log ( "POSTed: " + body ) ;
36
- const { timestamp : rawTimestamp , ...rest } = JSON . parse ( body ) ;
37
- const commandInfo = {
38
- ...rest ,
39
- // timestamp: new Date(rawTimestamp),
40
- } ;
41
- console . dir ( commandInfo ) ;
22
+ req . on ( "end" , ( ) => resolve ( JSON . parse ( body ) ) ) ;
23
+ } ) ;
24
+ }
42
25
43
- vscode . commands . executeCommand (
44
- commandInfo . commandId ,
45
- ...commandInfo . args
46
- ) ;
47
- res . writeHead ( 200 ) ;
48
- res . end ( "Hello World!" ) ;
49
- } ) ;
26
+ export function activate ( context : vscode . ExtensionContext ) {
27
+ var port : number | null = null ;
28
+
29
+ const server = http . createServer ( async function ( req , res ) {
30
+ if ( ! vscode . window . state . focused ) {
31
+ res . writeHead ( 401 ) ;
32
+ res . end ( "This editor is not active" ) ;
33
+ return ;
34
+ }
35
+
36
+ const commandInfo : Command = await getBody ( req ) ;
37
+
38
+ vscode . commands . executeCommand ( commandInfo . commandId , ...commandInfo . args ) ;
39
+
40
+ res . writeHead ( 200 ) ;
41
+ res . end ( ) ;
50
42
} ) ;
51
43
52
44
server . listen ( 0 , "localhost" , function ( ) {
53
45
const address : AddressInfo = ( server . address ( ) as unknown ) as AddressInfo ;
54
46
port = address . port ;
47
+
55
48
console . log ( "Listening on port " + address . port ) ;
49
+
56
50
if ( vscode . window . state . focused ) {
57
51
writePort ( ) ;
58
52
}
59
53
} ) ;
60
54
61
- vscode . window . onDidChangeWindowState ( ( event ) => {
62
- if ( event . focused && port !== null ) {
63
- writePort ( ) ;
55
+ const windowStateDisposable = vscode . window . onDidChangeWindowState (
56
+ ( event ) => {
57
+ if ( event . focused && port !== null ) {
58
+ writePort ( ) ;
59
+ }
64
60
}
65
- } ) ;
61
+ ) ;
66
62
67
63
function writePort ( ) {
68
64
const path = join ( tmpdir ( ) , "vscode-port" ) ;
65
+ console . log ( `Saved port ${ port } to path ${ path } ` ) ;
69
66
writeFileSync ( path , `${ port } ` ) ;
70
67
}
68
+
69
+ context . subscriptions . push ( windowStateDisposable , {
70
+ dispose ( ) {
71
+ server . close ( ) ;
72
+ } ,
73
+ } ) ;
71
74
}
72
75
73
76
// this method is called when your extension is deactivated
0 commit comments