-
Notifications
You must be signed in to change notification settings - Fork 48
/
javaModule.js
153 lines (142 loc) · 4.2 KB
/
javaModule.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var exec = require('child_process').exec;
var fs = require('fs');
var cuid = require('cuid');
var colors = require('colors');
exports.stats = false ;
exports.compileJava = function (envData , code , fn ){
//creating source file
var dirname = cuid.slug();
path = './temp/'+dirname;
fs.mkdir(path , 0777 , function(err){
if(err && exports.stats)
console.log(err.toString().red);
else
{
fs.writeFile( path + "/Main.java" , code , function(err ){
if(err && exports.stats)
console.log('ERROR: '.red + err);
else
{
if(exports.stats)
console.log('INFO: '.green + path + "/Main.java created");
if(envData.OS === "windows")
var command = "cd "+path+ " & " + " javac Main.java";
exec(command , function( error , stdout , stderr ){
if(error)
{
if(exports.stats)
console.log("INFO: ".green + path + "/Main.java contained an error while compiling");
var out = {error : stderr };
fn(out);
}
else
{
console.log("INFO: ".green + "compiled a java file");
var command = "cd "+path+" & java Main";
exec(command , function( error , stdout , stderr ){
if(error)
{
if(error.toString().indexOf('Error: stdout maxBuffer exceeded.') != -1)
{
var out = { error : 'Error: stdout maxBuffer exceeded. You might have initialized an infinite loop.'};
fn(out);
}
else
{
if(exports.stats)
{
console.log('INFO: '.green + path + '/Main.java contained an error while executing');
}
var out = { error : stderr};
fn(out);
}
}
else
{
if(exports.stats)
{
console.log('INFO: '.green + path + '/Main.java successfully compiled and executed !');
}
var out = { output : stdout};
fn(out);
}
});
}
});
}
});
}
});
}
exports.compileJavaWithInput = function (envData , code , input , fn ){
//creating source file
var dirname = cuid.slug();
path = './temp/'+dirname;
fs.mkdir(path , 0777 , function(err){
if(err && exports.stats)
console.log(err.toString().red);
else
{
fs.writeFile( path + "/Main.java" , code , function(err ){
if(err && exports.stats)
console.log('ERROR: '.red + err);
else
{
if(exports.stats)
console.log('INFO: '.green + path + "/Main.java created");
fs.writeFile( path + "/input.txt" , input , function (err){
if(err && exports.stats)
console.log('ERROR: '.red + err);
else
{
if(envData.OS === "windows")
var command = "cd "+path+ " & " + " javac Main.java";
exec(command , function( error , stdout , stderr ){
if(error)
{
if(exports.stats)
console.log("INFO: ".green + path + "/Main.java contained an error while compiling");
var out = {error : stderr };
fn(out);
}
else
{
console.log("INFO: ".green + "compiled a java file");
var command = "cd "+path+" & java Main < input.txt";
exec(command , function( error , stdout , stderr ){
if(error)
{
if(exports.stats)
{
console.log('INFO: '.green + path + '/Main.java contained an error while executing');
}
if(error.toString().indexOf('Error: stdout maxBuffer exceeded.') != -1)
{
var out = { error : 'Error: stdout maxBuffer exceeded. You might have initialized an infinite loop.'};
fn(out);
}
else
{
var out = { error : stderr};
fn(out);
}
}
else
{
if(exports.stats)
{
console.log('INFO: '.green + path + '/Main.java successfully compiled and executed !');
}
var out = { output : stdout};
fn(out);
}
});
}
});
}
});
}
});
}
});
}