|
1 | 1 | // The module 'vscode' contains the VS Code extensibility API
|
2 | 2 | // 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 | +} |
4 | 14 |
|
5 | 15 | // this method is called when your extension is activated
|
6 | 16 | // your extension is activated the very first time the command is executed
|
7 | 17 | 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 | + }); |
8 | 58 |
|
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 |
12 | 71 |
|
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 | + ); |
18 | 76 |
|
19 |
| - // Display a message box to the user |
20 |
| - vscode.window.showInformationMessage('Hello World from Command server!'); |
21 |
| - }); |
| 77 | + context.subscriptions.push(disposable); |
22 | 78 |
|
23 |
| - context.subscriptions.push(disposable); |
| 79 | + function writeHost() { |
| 80 | + writeFileSync("/tmp/vscode-host", `localhost:${port}`); |
| 81 | + } |
24 | 82 | }
|
25 | 83 |
|
26 | 84 | // this method is called when your extension is deactivated
|
|
0 commit comments