|
| 1 | +const { v4: uuid } = require('uuid'); |
| 2 | +const { exec } = require('child_process'); |
| 3 | +const fs = require('fs'); |
| 4 | +const os = require('os'); |
| 5 | +const path = require('path'); |
| 6 | +const saveFile = (file, data) => { |
| 7 | + return new Promise((resolve, reject) => { |
| 8 | + fs.writeFile(file, data, function (err) { |
| 9 | + if (err) { |
| 10 | + reject(err); |
| 11 | + } else { |
| 12 | + resolve([file]); |
| 13 | + } |
| 14 | + }); |
| 15 | + }); |
| 16 | +}; |
| 17 | + |
| 18 | +async function deleteFiles(javaPath, inputPath, exePath) { |
| 19 | + if (fs.existsSync(javaPath)) { |
| 20 | + await fs.unlinkSync(javaPath); |
| 21 | + } |
| 22 | + |
| 23 | + if (fs.existsSync(inputPath)) { |
| 24 | + await fs.unlinkSync(inputPath); |
| 25 | + } |
| 26 | + |
| 27 | + if (fs.existsSync(exePath)) { |
| 28 | + await fs.unlinkSync(exePath); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +function getExecutablePath(fileName) { |
| 35 | + // console.log(os.platform()); |
| 36 | + if (os.platform() === 'win32') { |
| 37 | + return `${path.join(__dirname, '..', 'upload', fileName)}.exe`; |
| 38 | + } |
| 39 | + if (os.platform() === 'linux') { |
| 40 | + return `${path.join(__dirname, '..', 'upload', fileName)}`; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +function getRunCommand(input, CentralClass) { |
| 45 | + |
| 46 | + let path = getExecutablePath("") |
| 47 | + return `cd ${path} && \ java ${CentralClass} < ${input}`; |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +function getjavaPath(fileName) { |
| 52 | + return `${path.join(__dirname, '..', 'upload', fileName)}.java`; |
| 53 | +} |
| 54 | + |
| 55 | +function getInputPath(fileName) { |
| 56 | + return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`; |
| 57 | +} |
| 58 | + |
| 59 | +function compileProgram(javaPath) { |
| 60 | + return new Promise((resolve, reject) => { |
| 61 | + exec(`javac ${javaPath}`, (error, stdout, stderr) => { |
| 62 | + if (error) { |
| 63 | + console.log({ error, stdout, stderr }) |
| 64 | + reject({ error, stdout, stderr }); |
| 65 | + } else { |
| 66 | + console.log({ error, stdout, stderr }) |
| 67 | + resolve({ stdout, stderr }); |
| 68 | + } |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function runProgram(inputPath, CentralClass) { |
| 74 | + return new Promise((resolve, reject) => { |
| 75 | + exec(getRunCommand(inputPath, CentralClass), (error, stdout, stderr) => { |
| 76 | + if (error) { |
| 77 | + console.log({ error, stdout, stderr }) |
| 78 | + reject({ error, stdout, stderr }); |
| 79 | + } else { |
| 80 | + console.log({ error, stdout, stderr }) |
| 81 | + resolve({ stdout, stderr }); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +const javaCompile = async (code, input, CentralClass) => { |
| 89 | + console.log({ code, input }) |
| 90 | + let state = { |
| 91 | + stdout: null, |
| 92 | + stderr: null, |
| 93 | + statusMes: "", |
| 94 | + } |
| 95 | + let uniqueFileName = uuid(); |
| 96 | + // let executePath = getExecutablePath(uniqueFileName) |
| 97 | + let javaPath = getjavaPath(uniqueFileName) |
| 98 | + let ipPath = getInputPath(uniqueFileName) |
| 99 | + |
| 100 | + await saveFile(javaPath, code); |
| 101 | + await saveFile(ipPath, input); |
| 102 | + |
| 103 | + try { |
| 104 | + let { stdout, stderr } = await compileProgram(javaPath); |
| 105 | + console.log({ stdout, stderr }) |
| 106 | + } catch (err) { |
| 107 | + state.stderr = err.stderr; |
| 108 | + state.statusMes = "Compiler Error"; |
| 109 | + deleteFiles(javaPath, ipPath); |
| 110 | + return state; |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + let { stdout, stderr } = await runProgram(ipPath, CentralClass); |
| 115 | + state.stdout = stdout; |
| 116 | + state.stderr = stderr; |
| 117 | + console.log({ stdout, stderr }) |
| 118 | + |
| 119 | + } catch (err) { |
| 120 | + state.stderr = err.stderr; |
| 121 | + state.statusMes = "Run Time Error"; |
| 122 | + console.log({ state }) |
| 123 | + deleteFiles(javaPath, ipPath); |
| 124 | + } |
| 125 | + |
| 126 | + if (state.stderr === '') { |
| 127 | + state.stderr = null; |
| 128 | + } |
| 129 | + state.statusMes = "Successfully Compiled"; |
| 130 | + await deleteFiles(javaPath, ipPath); |
| 131 | + return state; |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +module.exports = { javaCompile }; |
0 commit comments