-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrunCode.js
More file actions
56 lines (53 loc) · 1.41 KB
/
runCode.js
File metadata and controls
56 lines (53 loc) · 1.41 KB
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
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const fs = require('fs')
const runCode = async (language, filePath) => {
console.log('runCode called')
var result = { stderr: 'no output' }
switch (language) {
case 'cpp': {
console.log('c++ exec')
try {
result = await exec(
`g++ -o ${filePath}.out ${filePath}.cpp && ${filePath}.out < ${filePath}`,
{ timeout: 3000, maxBuffer: 1024 * 1024 * 5 }
)
fs.promises.unlink(filePath + '.out')
} catch (err) {
result = { stderr: err }
}
break
}
case 'js': {
console.log('js exec')
try {
result = await exec(`node ${filePath}.js < ${filePath}`, {
timeout: 5000,
maxBuffer: 1024 * 1024 * 5,
})
} catch (err) {
result = { stderr: err }
}
break
}
case 'py': {
console.log('runCode.js py switch')
console.log('executing')
try {
result = await exec(`cat ${filePath} | python3 ${filePath}.py`, {
timeout: 5000,
maxBuffer: 1024 * 1024 * 5,
})
} catch (err) {
result = { stderr: err }
}
break
}
}
fs.promises.unlink(filePath)
fs.promises.unlink(filePath + '.' + language)
// console.log('returning from runCode() with result');
// console.log(result);
return result
}
module.exports = runCode