Run server
This commit is contained in:
Generated
+887
-7
File diff suppressed because it is too large
Load Diff
+14
-4
@@ -26,6 +26,10 @@
|
|||||||
{
|
{
|
||||||
"command": "devrun-vscode.startServer",
|
"command": "devrun-vscode.startServer",
|
||||||
"title": "DevRun: Start the run server (for quick run starts)"
|
"title": "DevRun: Start the run server (for quick run starts)"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "devrun-vscode.stopServer",
|
||||||
|
"title": "DevRun: Stop the run server (for quick run starts)"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -38,14 +42,20 @@
|
|||||||
"test": "vscode-test"
|
"test": "vscode-test"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/vscode": "^1.109.0",
|
|
||||||
"@types/mocha": "^10.0.6",
|
"@types/mocha": "^10.0.6",
|
||||||
"@types/node": "18.x",
|
"@types/node": "18.x",
|
||||||
|
"@types/vscode": "^1.109.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^6.15.0",
|
"@typescript-eslint/eslint-plugin": "^6.15.0",
|
||||||
"@typescript-eslint/parser": "^6.15.0",
|
"@typescript-eslint/parser": "^6.15.0",
|
||||||
"eslint": "^8.56.0",
|
|
||||||
"typescript": "^5.3.3",
|
|
||||||
"@vscode/test-cli": "^0.0.4",
|
"@vscode/test-cli": "^0.0.4",
|
||||||
"@vscode/test-electron": "^2.3.8"
|
"@vscode/test-electron": "^2.3.8",
|
||||||
|
"eslint": "^8.56.0",
|
||||||
|
"typescript": "^5.3.3"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@types/cors": "^2.8.19",
|
||||||
|
"@types/express": "^5.0.6",
|
||||||
|
"cors": "^2.8.6",
|
||||||
|
"express": "^5.2.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-26
@@ -1,5 +1,6 @@
|
|||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { currentRun, startRun, stopRun } from "./run";
|
import { currentRun, startRun, stopRun } from "./run";
|
||||||
|
import { startServer, stopServer } from "./server";
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
console.log("dev-run Activated");
|
console.log("dev-run Activated");
|
||||||
@@ -9,36 +10,20 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
10,
|
10,
|
||||||
);
|
);
|
||||||
|
|
||||||
statusBarItem.text = "$(clock) No run yet";
|
statusBarItem.text = "$(clock) No active run";
|
||||||
statusBarItem.show();
|
statusBarItem.show();
|
||||||
|
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
vscode.commands.registerCommand("devrun-vscode.startRun", async () => {
|
vscode.commands.registerCommand(
|
||||||
if (currentRun) {
|
"devrun-vscode.startRun",
|
||||||
vscode.window.showErrorMessage("You are already in a run");
|
async (problemId?: string) => startRun(statusBarItem, problemId),
|
||||||
return;
|
),
|
||||||
}
|
|
||||||
|
|
||||||
const problemId = await vscode.window.showInputBox({
|
|
||||||
prompt: "Enter a valid problem ID",
|
|
||||||
placeHolder: "my-problem",
|
|
||||||
validateInput: (text) => {
|
|
||||||
return text.match(/^[a-zA-Z0-9-]+$/)
|
|
||||||
? null
|
|
||||||
: "Must only contain '-' and alphanumeric characters";
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!problemId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
startRun(statusBarItem, problemId);
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
vscode.commands.registerCommand("devrun-vscode.stopRun", async () => {
|
vscode.commands.registerCommand("devrun-vscode.stopRun", async () => {
|
||||||
|
if (!currentRun) return;
|
||||||
|
|
||||||
const isStop = await vscode.window.showWarningMessage(
|
const isStop = await vscode.window.showWarningMessage(
|
||||||
"Are you sure you want to stop the run?",
|
"Are you sure you want to stop the run?",
|
||||||
{ modal: true },
|
{ modal: true },
|
||||||
@@ -48,15 +33,21 @@ export function activate(context: vscode.ExtensionContext) {
|
|||||||
|
|
||||||
if (isStop === "Yes") {
|
if (isStop === "Yes") {
|
||||||
stopRun();
|
stopRun();
|
||||||
statusBarItem.text = "$(clock) No run yet";
|
statusBarItem.text = "$(clock) No active run";
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
vscode.commands.registerCommand("devrun-vscode.startServer", async () => {
|
vscode.commands.registerCommand("devrun-vscode.startServer", async () =>
|
||||||
|
startServer(statusBarItem),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
}),
|
context.subscriptions.push(
|
||||||
|
vscode.commands.registerCommand("devrun-vscode.stopServer", async () =>
|
||||||
|
stopServer(statusBarItem),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+27
-6
@@ -2,15 +2,29 @@ import * as vscode from "vscode";
|
|||||||
|
|
||||||
export var currentRun: NodeJS.Timeout | null = null;
|
export var currentRun: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
export async function stopRun() {
|
|
||||||
|
|
||||||
if (currentRun) clearTimeout(currentRun);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function startRun(
|
export async function startRun(
|
||||||
statusBarItem: vscode.StatusBarItem,
|
statusBarItem: vscode.StatusBarItem,
|
||||||
problemId: string,
|
problemId?: string,
|
||||||
) {
|
) {
|
||||||
|
if (currentRun) {
|
||||||
|
vscode.window.showErrorMessage("You are already in a run");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!problemId) {
|
||||||
|
problemId = await vscode.window.showInputBox({
|
||||||
|
prompt: "Enter a valid problem ID",
|
||||||
|
placeHolder: "my-problem",
|
||||||
|
validateInput: (text) => {
|
||||||
|
return text.match(/^[a-zA-Z0-9-]+$/)
|
||||||
|
? null
|
||||||
|
: "Must only contain '-' and alphanumeric characters";
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!problemId) return;
|
||||||
|
|
||||||
const mode = await vscode.window.showQuickPick(["any%", "100%"], {
|
const mode = await vscode.window.showQuickPick(["any%", "100%"], {
|
||||||
placeHolder: "Select a mode",
|
placeHolder: "Select a mode",
|
||||||
canPickMany: false,
|
canPickMany: false,
|
||||||
@@ -30,3 +44,10 @@ export async function startRun(
|
|||||||
.padStart(2, "0")}`;
|
.padStart(2, "0")}`;
|
||||||
}, 50);
|
}, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function stopRun() {
|
||||||
|
if (currentRun) {
|
||||||
|
clearTimeout(currentRun);
|
||||||
|
currentRun = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import * as vscode from "vscode";
|
||||||
|
import express from "express";
|
||||||
|
import * as http from "http";
|
||||||
|
import cors from "cors";
|
||||||
|
|
||||||
|
let server: http.Server | undefined;
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(
|
||||||
|
cors({
|
||||||
|
origin: (origin, callback) => {
|
||||||
|
// allow requests with no origin (e.g., direct curl)
|
||||||
|
if (
|
||||||
|
!origin ||
|
||||||
|
origin.includes("localhost") ||
|
||||||
|
origin.includes("127.0.0.1")
|
||||||
|
) {
|
||||||
|
callback(null, true);
|
||||||
|
} else {
|
||||||
|
callback(new Error("Not allowed by CORS"));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get("/start-run/:problemId", (req, res) => {
|
||||||
|
const { problemId } = req.params;
|
||||||
|
|
||||||
|
// trigger VS Code command
|
||||||
|
vscode.commands.executeCommand("devrun-vscode.startRun", problemId);
|
||||||
|
|
||||||
|
res.json({ ok: true, problemId });
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function startServer(statusBarItem: vscode.StatusBarItem) {
|
||||||
|
if (server) return;
|
||||||
|
|
||||||
|
statusBarItem.text = "$(clock) Starting server";
|
||||||
|
|
||||||
|
server = app.listen(63780, "127.0.0.1", () => {
|
||||||
|
setTimeout(() => (statusBarItem.text = "$(clock) No active run"), 500);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function stopServer(statusBarItem: vscode.StatusBarItem) {
|
||||||
|
if (!server) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
statusBarItem.text = "$(clock) Stopping server";
|
||||||
|
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
server!.close(() => resolve());
|
||||||
|
});
|
||||||
|
|
||||||
|
server = undefined;
|
||||||
|
setTimeout(() => (statusBarItem.text = "$(clock) No active run"), 500);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user