forked from Bit-Developer/code-editor-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptRunner.js
More file actions
48 lines (41 loc) · 1.21 KB
/
Copy pathJavaScriptRunner.js
File metadata and controls
48 lines (41 loc) · 1.21 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
const { spawn } = require('child_process');
const Runner = require('./Runner');
class JavaScriptRunner extends Runner {
defaultFile() {
return this.defaultfile;
}
constructor() {
super();
this.defaultfile = 'Hello.js';
}
run(file, directory, filename, extension, callback) {
if (extension.toLowerCase() !== '.js') {
console.log(`${file} is not a javascript file.`);
}
this.execute(file, directory, callback);
}
execute(file, directory, callback) {
// set working directory for child_process
const options = { cwd: directory };
const argsRun = [];
argsRun[0] = file;
console.log(`options: ${options}`);
console.log(`argsRun: ${argsRun}`);
const executor = spawn('node', argsRun, options);
executor.stdout.on('data', (output) => {
console.log(String(output));
callback('0', String(output)); // 0, no error
});
executor.stderr.on('data', (output) => {
console.log(`stderr: ${String(output)}`);
callback('2', String(output)); // 2, execution failure
});
executor.on('close', (output) => {
this.log(`stdout: ${output}`);
});
}
log(message) {
console.log(message);
}
}
module.exports = JavaScriptRunner;