Skip to content

Commit 89f55c7

Browse files
author
paulkoeckdev
committed
Implemented executor command for windows
1 parent 70551eb commit 89f55c7

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/executor.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { writeFileSync, unlinkSync, mkdirSync, readFileSync } from "fs";
33
import { exec } from "child_process";
44
import Language from "./languages";
55
import { sync as commandExists } from "command-exists";
6+
import path from "path";
7+
68
const uniqueFilename = require("unique-filename");
79

810
interface IOptions {
@@ -34,25 +36,37 @@ export default async function execute(
3436
}
3537

3638
// Write File to temp folder
37-
var filepath: string = uniqueFilename(tmpdir());
38-
if (language == Language.C) filepath += ".c";
39-
writeFileSync(filepath, input, { encoding: "utf-8" });
39+
var temppath: string = uniqueFilename(tmpdir());
40+
if (language == Language.C) temppath += ".c";
41+
writeFileSync(temppath, input, { encoding: "utf-8" });
42+
43+
// Command to execute runner
44+
var command = os == "win32" ? "" : "sh";
45+
46+
// Filetype of runner
47+
var filetype = os == "win32" ? "bat" : "sh";
48+
49+
// Path to runner file
50+
var runnerpath = path.join(
51+
__dirname,
52+
"..",
53+
"runners",
54+
os,
55+
`${language}.${filetype}`
56+
);
4057

4158
// Execute code
4259
return new Promise<string>((resolve, reject) => {
43-
exec(
44-
`sh ${__dirname}/../runners/${os}/${language}.sh ${filepath}`,
45-
(err, stdout, stderr) => {
46-
// Delete created file
47-
unlinkSync(filepath);
60+
exec(`${command} ${runnerpath} ${temppath}`, (err, stdout, stderr) => {
61+
// Delete created file
62+
unlinkSync(temppath);
4863

49-
if (stderr) return reject(stderr);
64+
if (stderr) return reject(stderr);
5065

51-
// Remove newline from stdout
52-
if (stdout.endsWith("\n")) stdout = stdout.slice(0, -1);
66+
// Remove newline from stdout
67+
if (stdout.endsWith("\n")) stdout = stdout.slice(0, -1);
5368

54-
resolve(stdout);
55-
}
56-
);
69+
resolve(stdout);
70+
});
5771
});
5872
}

0 commit comments

Comments
 (0)