-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
39 lines (35 loc) · 1.04 KB
/
index.js
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
import { readdirSync } from "fs";
import { join } from "path";
import { exec } from "child_process";
// TODO: Create a menu to choose programming language
// Choose random programming language
const languages = ["c", "c++", "go", "java", "javascript", "php", "python"];
let index = Math.round(Math.random() * (languages.length - 1 - 0) + 0);
const language = languages[index];
console.log(language)
let PATH = `./${language}/`;
// Make sure that the key corresponds to the file extension.
const METHODS = {
c: "gcc",
cpp: "g++",
js: "node",
go: "go run",
java: "javac",
php: "php",
python: "python",
};
// Choose a random file inside programming language path
const dirs = readdirSync(join(process.cwd(), PATH));
index = Math.round(Math.random() * (dirs.length - 1 - 0) + 0);
let extension = dirs[index].split(".").pop();
PATH += dirs[index];
exec(`${METHODS[extension]} ${PATH}`, (error, stdout, stderr) => {
if (error) {
throw Error(error);
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(stdout);
});