Skip to content

Commit 510b8b3

Browse files
committed
Initial working prototype
1 parent 8317853 commit 510b8b3

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "command-server",
2+
"name": "command-server",
33
"displayName": "Command server",
44
"description": "Accept commands on an HTTP server",
55
"version": "0.0.1",
@@ -10,7 +10,7 @@
1010
"Other"
1111
],
1212
"activationEvents": [
13-
"onCommand:command-server.helloWorld"
13+
"*"
1414
],
1515
"main": "./out/extension.js",
1616
"contributes": {
@@ -42,4 +42,4 @@
4242
"typescript": "^4.1.2",
4343
"vscode-test": "^1.4.1"
4444
}
45-
}
45+
}

src/extension.ts

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,84 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
3-
import * as vscode from 'vscode';
3+
import * as vscode from "vscode";
4+
import * as http from "http";
5+
import { AddressInfo } from "net";
6+
import { writeFileSync } from "fs";
7+
8+
interface Command {
9+
commandId: string;
10+
args: any[];
11+
expectResponse: boolean;
12+
timestamp: Date;
13+
}
414

515
// this method is called when your extension is activated
616
// your extension is activated the very first time the command is executed
717
export function activate(context: vscode.ExtensionContext) {
18+
// Use the console to output diagnostic information (console.log) and errors (console.error)
19+
// This line of code will only be executed once when your extension is activated
20+
console.log(
21+
'Congratulations, your extension "command-server" is now active!'
22+
);
23+
24+
var port: number | null = null;
25+
//create a server object:
26+
const server = http.createServer(function (req, res) {
27+
console.log("Got request");
28+
var body = "";
29+
req.on("data", function (chunk) {
30+
body += chunk;
31+
});
32+
req.on("end", function () {
33+
console.log("POSTed: " + body);
34+
const { timestamp: rawTimestamp, ...rest } = JSON.parse(body);
35+
const commandInfo = {
36+
...rest,
37+
// timestamp: new Date(rawTimestamp),
38+
};
39+
console.dir(commandInfo);
40+
41+
vscode.commands.executeCommand(
42+
commandInfo.commandId,
43+
...commandInfo.args
44+
);
45+
res.writeHead(200);
46+
res.end("Hello World!");
47+
});
48+
});
49+
50+
server.listen(0, "localhost", function () {
51+
const address: AddressInfo = (server.address() as unknown) as AddressInfo;
52+
port = address.port;
53+
console.log("Listening on port " + address.port);
54+
if (vscode.window.state.focused) {
55+
writeHost();
56+
}
57+
});
858

9-
// Use the console to output diagnostic information (console.log) and errors (console.error)
10-
// This line of code will only be executed once when your extension is activated
11-
console.log('Congratulations, your extension "command-server" is now active!');
59+
vscode.window.onDidChangeWindowState((event) => {
60+
if (event.focused && port !== null) {
61+
writeHost();
62+
}
63+
});
64+
// The command has been defined in the package.json file
65+
// Now provide the implementation of the command with registerCommand
66+
// The commandId parameter must match the command field in package.json
67+
let disposable = vscode.commands.registerCommand(
68+
"command-server.helloWorld",
69+
() => {
70+
// The code you place here will be executed every time your command is executed
1271

13-
// The command has been defined in the package.json file
14-
// Now provide the implementation of the command with registerCommand
15-
// The commandId parameter must match the command field in package.json
16-
let disposable = vscode.commands.registerCommand('command-server.helloWorld', () => {
17-
// The code you place here will be executed every time your command is executed
72+
// Display a message box to the user
73+
vscode.window.showInformationMessage("Hello World from Command server!");
74+
}
75+
);
1876

19-
// Display a message box to the user
20-
vscode.window.showInformationMessage('Hello World from Command server!');
21-
});
77+
context.subscriptions.push(disposable);
2278

23-
context.subscriptions.push(disposable);
79+
function writeHost() {
80+
writeFileSync("/tmp/vscode-host", `localhost:${port}`);
81+
}
2482
}
2583

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

0 commit comments

Comments
 (0)