{"size": 1396, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "Sanshain/tcp-bundler", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "Sanshain/tcp-bundler", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "Sanshain/tcp-bundler", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.8852459016, "max_line_length": 78, "alphanum_fraction": 0.6353868195}

const fs = require("fs");
const sock = require('net');
const path = require('path')

// const cmd = require("child_process");
var tss = require('typescript-simple');
var obfuscator = require("uglify-js");

const bundler = require('./pack')

function startListen(host, port, options){
	host = host || 'localhost'
	port = port || 9098
	options = options || {}

	console.log(`bundler start listen on ${host}:${port}`)    	

	sock.createServer(function (socket) {

		console.log('connection estableshed')    
	
		socket.on('data', async function(data) {
			 			
			console.time('Time');		
	
			let filename = data.toString();
			console.log(`received task for '${filename}'`)
				
			if (fs.existsSync(filename)){
	
				var content = fs.readFileSync(filename, "utf8");
				var result = bundler.combine(content, path.dirname(filename), options)
				
				if (options.tsc)  result = tss(result);		
				if (options.minify)	result = obfuscator.minify(result).code.valueOf();				
				let pathinfo = path.parse(filename);

				fs.writeFileSync(pathinfo.dir + path.sep + pathinfo.name + '.js', result)
			}
			else throw new Error(`file ${filename} does not exists`)
					
			console.timeEnd('Time');
			socket.write('success');
				  
		});
		 
	}).listen(port, host);

}

module.exports = { startListen }



/*
var s = require('net').Socket();
s.connect(8080, 'localhost');
s.write('Hello');
s.end();
//*/