-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_file.js
43 lines (38 loc) · 1.49 KB
/
run_file.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
40
41
42
43
const vscode = require('vscode');
const { exec } = require('child_process');
const path = require('path');
async function run_file(filepath, language) {
let run_cmd = "";
const dir_path = path.dirname(filepath);
const input_file = path.join(dir_path, 'cph_input.txt');
if(language == 'python'){
run_cmd = `python ${filepath} < ${input_file}`;
}else if(language == 'cpp'){
run_cmd = `g++ ${filepath} -o cph_outfile && cph_outfile < ${input_file}`;
}else if(language == 'c'){
run_cmd = `gcc ${filepath} -o c_outfile && c_outfile < ${input_file}`;
}else if(language == 'javascript'){
run_cmd = `node ${filepath} < ${input_file}`;
}else if(language == 'java'){
const classname = filepath.split('/').pop().replace('.java', '');
run_cmd = `javac ${filepath} && java ${classname} < ${input_file}`;
}
try {
const output = await new Promise((resolve, reject) => {
exec(run_cmd, (err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(`Error executing file: ${stderr}`);
reject(stderr);
} else {
vscode.window.showInformationMessage(`Execution result: ${stdout}`);
resolve(stdout);
}
});
});
console.log('Output:', output);
return output;
} catch (error) {
console.error('Error:', error);
}
}
module.exports = run_file;