Skip to content

Commit 93ebfbe

Browse files
committed
Support expectResponse and waitForCommand
1 parent 8830d3d commit 93ebfbe

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ POST /execute-command HTTP/1.1
2626
Upon receiving the above, this extension would run the command
2727
`some-command-id` with argument `"some-argument"`.
2828

29+
If you'd like the server to wait for the command to finish before responding,
30+
pass `waitForFinish=true`.
31+
32+
If you'd like the server to wait for the command to finish and then respond
33+
with the command return value encoded as JSON, pass `expectResponse=true`.
34+
2935
Note that the server is bound to `localhost`, so it will only accept commands
3036
from processes running on the same host as VSCode.
3137

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "git",
99
"url": "https://github.com/pokey/command-server"
1010
},
11-
"version": "0.1.3",
11+
"version": "0.2.2",
1212
"engines": {
1313
"vscode": "^1.53.0"
1414
},

src/extension.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import * as vscode from "vscode";
44
import * as http from "http";
55
import { AddressInfo } from "net";
6-
import { writeFileSync } from "fs";
6+
import { existsSync, readFileSync, writeFileSync } from "fs";
77
import { tmpdir } from "os";
88
import { join } from "path";
99
import { getRequestJSON } from "./getRequestJSON";
@@ -12,6 +12,7 @@ interface Command {
1212
commandId: string;
1313
args: any[];
1414
expectResponse: boolean;
15+
waitForFinish: boolean;
1516
}
1617

1718
export function activate(context: vscode.ExtensionContext) {
@@ -26,9 +27,23 @@ export function activate(context: vscode.ExtensionContext) {
2627

2728
const commandInfo: Command = await getRequestJSON(req);
2829

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+
}
3040

3141
res.writeHead(200);
42+
43+
if (commandInfo.expectResponse) {
44+
res.write(JSON.stringify(commandReturnValue));
45+
}
46+
3247
res.end();
3348
});
3449

@@ -43,6 +58,17 @@ export function activate(context: vscode.ExtensionContext) {
4358
}
4459
});
4560

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+
4672
const windowStateDisposable = vscode.window.onDidChangeWindowState(
4773
(event) => {
4874
if (event.focused && port !== null) {
@@ -52,7 +78,7 @@ export function activate(context: vscode.ExtensionContext) {
5278
);
5379

5480
function writePort() {
55-
const path = join(tmpdir(), "vscode-port");
81+
const path = getPortPath();
5682
console.log(`Saving port ${port} to path ${path}`);
5783
writeFileSync(path, `${port}`);
5884
}
@@ -62,6 +88,10 @@ export function activate(context: vscode.ExtensionContext) {
6288
server.close();
6389
},
6490
});
91+
92+
function getPortPath() {
93+
return join(tmpdir(), "vscode-port");
94+
}
6595
}
6696

6797
// this method is called when your extension is deactivated

0 commit comments

Comments
 (0)