47 lines
1.4 KiB
JavaScript
Executable File
47 lines
1.4 KiB
JavaScript
Executable File
const net = require('net');
|
|
const through = require('through2');
|
|
const colors = require('ansi-colors');
|
|
const log = require('fancy-log');
|
|
|
|
const color = (level) => {
|
|
return {
|
|
'[DEBUG]': colors.white,
|
|
'[INFO]': colors.cyan,
|
|
'[ERROR]': colors.red,
|
|
'[WARNING]': colors.yellow,
|
|
}[level] || colors.reset;
|
|
};
|
|
|
|
const _log = (line) => {
|
|
const result = line.split(' ');
|
|
console.log(colors.gray(result[0]) + ' ' + color(result[1])(result.slice(1).join(' ')));
|
|
};
|
|
|
|
module.exports = () => {
|
|
return through.obj(function (file, enc, callback) {
|
|
const debug = file.debug;
|
|
if (!file.debug) {
|
|
this.emit('end');
|
|
callback();
|
|
return;
|
|
}
|
|
const server = net.createServer((socket) => {
|
|
socket.on("data", (data) => {
|
|
const lines = data.toString().split('\n');
|
|
for (let line of lines) if (line.length > 2) {
|
|
_log(line);
|
|
}
|
|
});
|
|
socket.on("close", () => {
|
|
socket.destroy();
|
|
server.close();
|
|
this.emit('end');
|
|
callback();
|
|
});
|
|
});
|
|
log(colors.green('[debug]'), colors.cyan('listen on'), colors.magenta(`${debug.host}:${debug.port}`));
|
|
server.listen(debug.port, debug.host);
|
|
})
|
|
};
|
|
|
|
module.exports.log = log; |