<commit_before>var io = require('socket.io');
var fs = require('fs');
var exec = require('child_process').exec;
var tmp = require('temporary');
var sep = getSeparator();

exports.init = function(server) {
	console.log('JavaDec initialized');
	io = io.listen(server);

	io.on('connection', function(socket) { // triggered by io.connect('/') at the client side
		console.log('JavaDec on connection');

		socket.on('code_sent', function(data) {
			var javaFile = data.className + '.java';
			console.log('code received');
			
			java2ByteCode(javaFile, data.code, socket);
		});
	});
}

function java2ByteCode(javaFile, code, socket) {
	var tmpDir = new tmp.Dir();
	console.log('tmpDir: ' + tmpDir.path);
	fs.writeFile(tmpDir.path + sep + javaFile, code, function(err) {
		if(err) {
			console.log(err);
			return;
		} 
		console.log('java file generated');
		compileJava(javaFile, socket, tmpDir);
	});
}

function hideErrorMsgPath(tmpDirPath, stderr) {
	var tmp = tmpDirPath.replace(/[\/\\]/g, '');
	return stderr.replace(/[\/\\]/g, '').replace(tmp, '');
}

function compileJava(javaFile, socket, tmpDir) {
	console.log('seperator: ' + sep);
	var child = exec(getEnvVarWin('JAVA_HOME') + sep + 'bin' + sep + 'javac ' + 
		tmpDir.path + sep + javaFile, function(error, stdout, stderr) {
		if ( error != null ) {
	        console.log('stderr(compieJava): ' + stderr);
	        var errMsg = hideErrorMsgPath(tmpDir.path, stderr);        
	        socket.emit('wrong', {
	        	err: errMsg
	        });
	        return;
	   }
	   console.log('class file generated');
	   genByteCode(javaFile.replace('java', 'class'), socket, tmpDir);
	});
}

function genByteCode(classFile, socket, tmpDir) {
	var child = exec(getEnvVarWin('JAVA_HOME')+ sep + 'bin' + sep + 'javap -c ' + 
		tmpDir.path + sep + classFile, function(error, stdout, stderr) {
		if ( error != null ) { 
			console.log('stderr(genByteCode): ' + stderr);
			var errMsg = hideErrorMsgPath(tmpDir.path, stderr);
	        socket.emit('wrong', {
	        	err: errMsg
	        });
	        return;
	   }
	   console.log('bytecode file generated');
	   console.log(stdout);
	   socket.emit('byte_code', {
	   		code: stdout
	   });
	});
}

function getEnvVarWin(envVar) {
	if(process.platform.indexOf('win32') > -1) { // windows operating system
		return '%'+envVar+'%';
	} else {
		return '$'+envVar;
	}
}

function getSeparator() {
	if(process.platform.indexOf('win32') > -1) { // contains win32
		return '\\';
	} else {
		return '/';
	}
}<commit_msg>A better way to find environment variable JAVA_HOME.<commit_after>var io = require('socket.io');
var fs = require('fs');
var exec = require('child_process').exec;
var tmp = require('temporary');
var sep = getSeparator();

exports.init = function(server) {
	console.log('JavaDec initialized');
	io = io.listen(server);

	io.on('connection', function(socket) { // triggered by io.connect('/') at the client side
		console.log('JavaDec on connection');

		socket.on('code_sent', function(data) {
			var javaFile = data.className + '.java';
			console.log('code received');
			
			java2ByteCode(javaFile, data.code, socket);
		});
	});
}

function java2ByteCode(javaFile, code, socket) {
	var tmpDir = new tmp.Dir();
	console.log('tmpDir: ' + tmpDir.path);
	fs.writeFile(tmpDir.path + sep + javaFile, code, function(err) {
		if(err) {
			console.log(err);
			return;
		} 
		console.log('java file generated');
		compileJava(javaFile, socket, tmpDir);
	});
}

function hideErrorMsgPath(tmpDirPath, stderr) {
	var tmp = tmpDirPath.replace(/[\/\\]/g, '');
	return stderr.replace(/[\/\\]/g, '').replace(tmp, '');
}

function compileJava(javaFile, socket, tmpDir) {
	console.log('seperator: ' + sep);
	var child = exec(process.env.JAVA_HOME + sep + 'bin' + sep + 'javac ' + 
		tmpDir.path + sep + javaFile, function(error, stdout, stderr) {
		if ( error != null ) {
	        console.log('stderr(compieJava): ' + stderr);
	        var errMsg = hideErrorMsgPath(tmpDir.path, stderr);        
	        socket.emit('wrong', {
	        	err: errMsg
	        });
	        return;
	   }
	   console.log('class file generated');
	   genByteCode(javaFile.replace('java', 'class'), socket, tmpDir);
	});
}

function genByteCode(classFile, socket, tmpDir) {
	var child = exec(process.env.JAVA_HOME + sep + 'bin' + sep + 'javap -c ' + 
		tmpDir.path + sep + classFile, function(error, stdout, stderr) {
		if ( error != null ) { 
			console.log('stderr(genByteCode): ' + stderr);
			var errMsg = hideErrorMsgPath(tmpDir.path, stderr);
	        socket.emit('wrong', {
	        	err: errMsg
	        });
	        return;
	   }
	   console.log('bytecode file generated');
	   console.log(stdout);
	   socket.emit('byte_code', {
	   		code: stdout
	   });
	});
}

function getSeparator() {
	if(process.platform.indexOf('win32') > -1) { // contains win32
		return '\\';
	} else {
		return '/';
	}
}
{'fraction_non_alphanumeric': 0.17404674046740468, 'fraction_numerical': 0.0034850348503485036, 'mean_word_length': 3.3836477987421385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2082527', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1650, 'n_words': 345}