sungjin

Start Project with File Runner

node_modules
a.out
tmp.c
This diff could not be displayed because it is too large.
{
"name": "learn_with_code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm run build:live",
"build": "tsc -p .",
"build:live": "nodemon --watch 'src/**/*.ts' --exec \"ts-node\" src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^16.11.6",
"nodemon": "^2.0.14",
"ts-node": "^10.4.0",
"typescript": "^4.4.4"
},
"dependencies": {
"express": "^4.17.1"
}
}
const _PORT = 3000;
import * as express from "express";
import {Runner} from "./runner";
const app = express.default();
app.use(express.json());
/*RESTful API*/
app.post("/runcode", function (req, res) {
var run = new Runner(req);
run.run();
res.send(run.output);
});
app.listen(_PORT, () => {
console.log("server started at port " + _PORT);
});
import * as child_process from "child_process";
import * as fs from "fs";
export class Runner {
type: string;
input: Array<string>;
output: Array<string> = [];
time: Date = new Date();
constructor(req: any) {
this.type = req.body.type;
this.input = req.body.input;
fs.writeFileSync("tmp." + this.type, req.body.code);
}
run() {
switch (this.type) {
case "c": {
this.c();
}
case "c++": {
this.cpp();
}
case "js": {
this.js();
}
case "go": {
this.go();
}
case "ts": {
this.ts();
}
}
}
c() {
if ((this.input = [])) {
const test = child_process.spawnSync("gcc", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync("~/OSS/main/a.out", {
encoding: "utf8",
shell: true,
});
console.log(result.stdout);
this.output.push(fix(result.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("gcc", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync("~/OSS/main/a.out", {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(result.stdout);
this.output.push(fix(result.stdout));
}
}
cpp() {
if (this.input = []) {
const test = child_process.spawnSync("gcc", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync("~/OSS/main/a.out", {
encoding: "utf8",
shell: true,
});
console.log(result.stdout);
this.output.push(fix(result.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("gcc", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync("~/OSS/main/a.out", {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(result.stdout);
this.output.push(fix(result.stdout));
}
}
js() {
if (this.input = []) {
const test = child_process.spawnSync("node", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("node", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
}
go() {
if (this.input = []) {
const test = child_process.spawnSync("go", ["run", "~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("go", ["run", "~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
}
ts() {
if (this.input = []) {
const test = child_process.spawnSync("ts-node", ["run", "~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("ts-node", ["run", "~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
}
}
const fix = (a: any): string => {
if (typeof a === "string") {
return a;
}
return "";
};
This diff is collapsed. Click to expand it.
File mode changed
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="wrapper">
<div class="header">Code in Web</div>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</div>
</body>
</html>