-
Notifications
You must be signed in to change notification settings - Fork 9
/
companion.ts
92 lines (78 loc) · 2.42 KB
/
companion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as vscode from "vscode";
import { newProblemFromCompanion } from "./core";
import sanitize from "sanitize-filename";
import express from "express";
import bodyParser = require("body-parser");
import { debug } from "./utils";
import { getAutoOpen } from "./webview/types";
export class CompanionConfig {
name: string;
group: string;
url: string;
memoryLimit: number;
timeLimit: number;
constructor(data: any) {
this.name = sanitize(data.name);
this.group = sanitize(data.group);
this.url = data.url;
this.memoryLimit = data.memoryLimit;
this.timeLimit = data.timeLimit;
}
}
export class TestCase {
input: string;
output: string;
constructor(data: any) {
this.input = data.input;
this.output = data.output;
}
}
export function startCompetitiveCompanionService() {
let port = 0;
const app = express();
if (process.env.ACMX_TESTING === "1") {
port = 10041; // Use this port for testing.
} else {
const port_: number | undefined = vscode.workspace
.getConfiguration("acmx.companion", null)
.get("port");
port = port_!;
}
app.use(bodyParser.json());
app.post("/", async (req: any, res: any) => {
const data = req.body;
const companionConfig = new CompanionConfig(data);
const tests: TestCase[] = data.tests.map((value: any) => {
return new TestCase(value);
});
res.sendStatus(200);
const problemInContest = newProblemFromCompanion(
companionConfig,
tests
);
const contestPath = problemInContest.contestPath;
const mainSolution = problemInContest.problemConfig.mainSolution.unwrapOr(
""
);
if (getAutoOpen()) {
await vscode.commands
.executeCommand(
"vscode.openFolder",
vscode.Uri.file(contestPath)
)
.then(async () => {
await vscode.commands.executeCommand(
"vscode.open",
vscode.Uri.file(mainSolution)
);
});
}
});
app.listen(port, (err: any) => {
if (err) {
console.error(err);
process.exit(1);
}
debug("companion", `Started companion. Listening on port ${port}`);
});
}