1- import fs from "fs" ;
2- import path from " path" ;
3- import util from " util" ;
4- import { execFile , spawn , exec } from " child_process" ;
5- import ValidationService from " ./validation.service" ;
1+ import fs from 'fs' ;
2+ import path from ' path' ;
3+ import util from ' util' ;
4+ import { execFile , spawn , exec } from ' child_process' ;
5+ import ValidationService from ' ./validation.service' ;
66const ROOT_DIR = `${ process . cwd ( ) } ` ;
7- const SOURCE_DIR = path . join ( ROOT_DIR , " executor" ) ;
7+ const SOURCE_DIR = path . join ( ROOT_DIR , ' executor' ) ;
88const TARGET_DIR = `/app/codes` ;
9- const IMAGE_NAME = " executor:1.0" ;
10- const VOL_NAME = `my_vol` ;
11- // const VOL_NAME = SOURCE_DIR;
9+ const IMAGE_NAME = ' executor:1.0' ;
10+ // const VOL_NAME = `my_vol`;
11+ const VOL_NAME = SOURCE_DIR ;
1212
1313class CodeService {
1414 async execute ( code , input , lang , id ) {
1515 try {
16- ! input ? ( input = "" ) : null ;
16+ ! input ? ( input = '' ) : null ;
1717
18- //validating code
18+ // validating code
1919 // await this.validateCode(code, input, lang, id);
2020 const { isValid, message } = await ValidationService . execute (
2121 code ,
@@ -25,7 +25,7 @@ class CodeService {
2525 ) ;
2626 if ( ! isValid ) {
2727 throw {
28- message,
28+ message
2929 } ;
3030 }
3131
@@ -61,20 +61,24 @@ class CodeService {
6161 async writeFile ( code , lang , input , id ) {
6262 let fileName = `${ id } code` ;
6363 switch ( lang ) {
64- case " javascript" : {
65- fileName += " .js" ;
64+ case ' javascript' : {
65+ fileName += ' .js' ;
6666 break ;
6767 }
68- case " c++" : {
69- fileName += " .cpp" ;
68+ case ' c++' : {
69+ fileName += ' .cpp' ;
7070 break ;
7171 }
72- case "python" : {
73- fileName += ".py" ;
72+ case 'python' : {
73+ fileName += '.py' ;
74+ break ;
75+ }
76+ case 'java' : {
77+ fileName += '.java' ;
7478 break ;
7579 }
7680 default : {
77- throw { message : " Invalid language" } ;
81+ throw { message : ' Invalid language' } ;
7882 }
7983 }
8084 const write = util . promisify ( fs . writeFile ) ;
@@ -84,30 +88,34 @@ class CodeService {
8488 await write ( path . join ( SOURCE_DIR , `${ id } input.txt` ) , input ) ;
8589 return {
8690 file : fileName ,
87- inputFile : `${ id } input.txt` ,
91+ inputFile : `${ id } input.txt`
8892 } ;
8993 } catch ( error ) {
9094 throw { message : error } ;
9195 }
9296 }
9397
9498 async writeCommand ( lang , file , input , id ) {
95- let command = "" ;
99+ let command = '' ;
96100 switch ( lang ) {
97- case " javascript" : {
101+ case ' javascript' : {
98102 command = `cd ${ TARGET_DIR } && node ${ file } < ${ input } ` ;
99103 break ;
100104 }
101- case " c++" : {
105+ case ' c++' : {
102106 command = `cd ${ TARGET_DIR } && g++ -o ${ id } ${ file } && ./${ id } < ${ input } ` ;
103107 break ;
104108 }
105- case " python" : {
109+ case ' python' : {
106110 command = `cd ${ TARGET_DIR } && python ${ file } < ${ input } ` ;
107111 break ;
108112 }
113+ case 'java' : {
114+ command = `cd ${ TARGET_DIR } && javac ${ file } && java Input < ${ input } ` ;
115+ break ;
116+ }
109117 default : {
110- throw { message : " Invalid language" } ;
118+ throw { message : ' Invalid language' } ;
111119 }
112120 }
113121
@@ -123,10 +131,10 @@ class CodeService {
123131 async execChild ( runCode , runContainer , id , file , inputFile , lang ) {
124132 return new Promise ( ( resolve , reject ) => {
125133 const execCont = exec ( `${ runContainer } ` ) ;
126- execCont . on ( " error" , ( err ) => {
127- throw { status : " 404" , message : err } ;
134+ execCont . on ( ' error' , err => {
135+ throw { status : ' 404' , message : err } ;
128136 } ) ;
129- execCont . stdout . on ( " data" , ( ) => {
137+ execCont . stdout . on ( ' data' , ( ) => {
130138 exec ( `${ runCode } ` , async ( error , stdout , stderr ) => {
131139 await this . endContainer ( id ) ;
132140 await this . deleteFiles ( file , inputFile , lang , id ) ;
@@ -141,19 +149,24 @@ class CodeService {
141149 }
142150
143151 async deleteFiles ( fileName , inputName , lang , id ) {
144- fs . unlinkSync ( path . join ( SOURCE_DIR , fileName ) , ( err ) => {
152+ fs . unlinkSync ( path . join ( SOURCE_DIR , fileName ) , err => {
145153 if ( err ) throw { message : err } ;
146154 } ) ;
147155 if ( inputName ) {
148- fs . unlinkSync ( path . join ( SOURCE_DIR , inputName ) , ( err ) => {
156+ fs . unlinkSync ( path . join ( SOURCE_DIR , inputName ) , err => {
149157 if ( err ) throw { message : err } ;
150158 } ) ;
151159 }
152- if ( lang == " c++" ) {
153- fs . unlinkSync ( path . join ( SOURCE_DIR , id ) , ( err ) => {
160+ if ( lang == ' c++' ) {
161+ fs . unlinkSync ( path . join ( SOURCE_DIR , id ) , err => {
154162 if ( err ) throw { message : err } ;
155163 } ) ;
156164 }
165+ if ( lang == 'java' ) {
166+ fs . unlinkSync ( path . join ( SOURCE_DIR , 'Input.class' ) , err => {
167+ if ( err ) throw err ;
168+ } ) ;
169+ }
157170 }
158171
159172 async endContainer ( id ) {
@@ -162,7 +175,7 @@ class CodeService {
162175 exec ( `${ exit } ` , ( error , stdout , stderr ) => {
163176 if ( error ) {
164177 console . log ( error ) ;
165- } else console . log ( " Container stoped and deleted" ) ;
178+ } else console . log ( ' Container stoped and deleted' ) ;
166179 } ) ;
167180 }
168181}
0 commit comments