Skip to content

Commit 248fef5

Browse files
committed
Clean up
1 parent 6cbd899 commit 248fef5

File tree

2 files changed

+57
-33
lines changed

2 files changed

+57
-33
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
11
# command-server README
2+
3+
Creates an http server listening for commands.
4+
5+
## Features
6+
7+
On startup, spawns an http server listening for commands. The port of the server for the active editor is written to a file named `vscode-port` in the operating system's default directory for temporary files.
8+
9+
Accepts requests of the form:
10+
11+
```http
12+
POST /execute-command HTTP/1.1
13+
14+
{
15+
"commandId": "some-command-id",
16+
"args": [
17+
"default"
18+
]
19+
}
20+
```
21+
22+
## Release Notes

src/extension.ts

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,66 @@ interface Command {
1111
commandId: string;
1212
args: any[];
1313
expectResponse: boolean;
14-
timestamp: Date;
1514
}
1615

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) => {
3018
var body = "";
3119
req.on("data", function (chunk) {
3220
body += chunk;
3321
});
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+
}
4225

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();
5042
});
5143

5244
server.listen(0, "localhost", function () {
5345
const address: AddressInfo = (server.address() as unknown) as AddressInfo;
5446
port = address.port;
47+
5548
console.log("Listening on port " + address.port);
49+
5650
if (vscode.window.state.focused) {
5751
writePort();
5852
}
5953
});
6054

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+
}
6460
}
65-
});
61+
);
6662

6763
function writePort() {
6864
const path = join(tmpdir(), "vscode-port");
65+
console.log(`Saved port ${port} to path ${path}`);
6966
writeFileSync(path, `${port}`);
7067
}
68+
69+
context.subscriptions.push(windowStateDisposable, {
70+
dispose() {
71+
server.close();
72+
},
73+
});
7174
}
7275

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

0 commit comments

Comments
 (0)