first draft
This commit is contained in:
parent
7eb5405791
commit
64b8901159
35
cli.js
Normal file
35
cli.js
Normal file
@ -0,0 +1,35 @@
|
||||
const WebSocket = require('ws');
|
||||
const chalk = require('chalk');
|
||||
|
||||
const { log } = console;
|
||||
|
||||
const logParse = (data) => {
|
||||
return data.map((it) => JSON.stringify(it)).join(' ');
|
||||
};
|
||||
|
||||
const wss = new WebSocket.Server({ port: 8080 });
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.on('message', function incoming(message) {
|
||||
const event = JSON.parse(message);
|
||||
const { type, data } = event;
|
||||
switch (type) {
|
||||
case 'log':
|
||||
log('>>', logParse(data));
|
||||
break;
|
||||
case 'error':
|
||||
log('>>', chalk.red(logParse(data)));
|
||||
break;
|
||||
case 'warn':
|
||||
log('>>', chalk.yellow(logParse(data)));
|
||||
break;
|
||||
case 'debug':
|
||||
log('>>', chalk.blue(logParse(data)));
|
||||
break;
|
||||
default:
|
||||
log('>>', logParse(data));
|
||||
}
|
||||
});
|
||||
|
||||
ws.send('something');
|
||||
});
|
2
index.d.ts
vendored
Normal file
2
index.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
export default tconsole;
|
||||
declare function tconsole(options: any): void;
|
54
index.js
Executable file
54
index.js
Executable file
@ -0,0 +1,54 @@
|
||||
const sendWhenConnected = (ws, msg, n = 0, maxTries = 100) => {
|
||||
setTimeout(() => {
|
||||
if (ws.readyState === 1) {
|
||||
ws.send(msg);
|
||||
} else if (n < maxTries) {
|
||||
sendWhenConnected(ws, msg, n + 1);
|
||||
} else{
|
||||
console.error("Exceed tries to send message: ", msg);
|
||||
}
|
||||
}, 10); // wait 10 milisecond for the connection...
|
||||
}
|
||||
|
||||
const tconsole = (options) => {
|
||||
|
||||
options = {
|
||||
host: "localhost",
|
||||
port: 8080,
|
||||
...options,
|
||||
}
|
||||
|
||||
const ws = new WebSocket(`ws://${options.host}:${options.port}`);
|
||||
|
||||
|
||||
ws.onopen = function () {
|
||||
console.log('[TCONSOLE]: connected');
|
||||
};
|
||||
|
||||
console.stdlog = console.log.bind(console);
|
||||
|
||||
console.log = function () {
|
||||
sendWhenConnected(ws, JSON.stringify({ type: 'log', data: Array.from(arguments) }));
|
||||
console.stdlog.apply(console, arguments);
|
||||
};
|
||||
|
||||
console.error = function () {
|
||||
sendWhenConnected(ws, JSON.stringify({ type: 'error', data: Array.from(arguments) }));
|
||||
console.stdlog.apply(console, arguments);
|
||||
};
|
||||
|
||||
console.defaultWarn = console.warn.bind(console);
|
||||
console.warn = function () {
|
||||
sendWhenConnected(ws, JSON.stringify({ type: 'warn', data: Array.from(arguments) }));
|
||||
console.defaultWarn.apply(console, arguments);
|
||||
};
|
||||
|
||||
console.defaultDebug = console.debug.bind(console);
|
||||
console.debug = function () {
|
||||
sendWhenConnected(ws, JSON.stringify({ type: 'debug', data: Array.from(arguments) }));
|
||||
console.defaultDebug.apply(console, arguments);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
export default tconsole;
|
155
package-lock.json
generated
Normal file
155
package-lock.json
generated
Normal file
@ -0,0 +1,155 @@
|
||||
{
|
||||
"name": "tconsole",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "tconsole",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"chalk": "^4.1.2",
|
||||
"ws": "^8.2.0"
|
||||
},
|
||||
"bin": {
|
||||
"tconsole": "server.js"
|
||||
}
|
||||
},
|
||||
"node_modules/ansi-styles": {
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
|
||||
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
|
||||
"dependencies": {
|
||||
"color-convert": "^2.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk": {
|
||||
"version": "4.1.2",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
|
||||
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
|
||||
"dependencies": {
|
||||
"ansi-styles": "^4.1.0",
|
||||
"supports-color": "^7.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/color-convert": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
|
||||
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
|
||||
"dependencies": {
|
||||
"color-name": "~1.1.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=7.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/color-name": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
|
||||
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
|
||||
},
|
||||
"node_modules/has-flag": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
|
||||
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/supports-color": {
|
||||
"version": "7.2.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
||||
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
|
||||
"dependencies": {
|
||||
"has-flag": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "8.2.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.2.0.tgz",
|
||||
"integrity": "sha512-uYhVJ/m9oXwEI04iIVmgLmugh2qrZihkywG9y5FfZV2ATeLIzHf93qs+tUNqlttbQK957/VX3mtwAS+UfIwA4g==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": "^5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"ansi-styles": {
|
||||
"version": "4.3.0",
|
||||
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
|
||||
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
|
||||
"requires": {
|
||||
"color-convert": "^2.0.1"
|
||||
}
|
||||
},
|
||||
"chalk": {
|
||||
"version": "4.1.2",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
|
||||
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
|
||||
"requires": {
|
||||
"ansi-styles": "^4.1.0",
|
||||
"supports-color": "^7.1.0"
|
||||
}
|
||||
},
|
||||
"color-convert": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
|
||||
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
|
||||
"requires": {
|
||||
"color-name": "~1.1.4"
|
||||
}
|
||||
},
|
||||
"color-name": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
|
||||
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
|
||||
},
|
||||
"has-flag": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
|
||||
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="
|
||||
},
|
||||
"supports-color": {
|
||||
"version": "7.2.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
||||
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
|
||||
"requires": {
|
||||
"has-flag": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"ws": {
|
||||
"version": "8.2.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.2.0.tgz",
|
||||
"integrity": "sha512-uYhVJ/m9oXwEI04iIVmgLmugh2qrZihkywG9y5FfZV2ATeLIzHf93qs+tUNqlttbQK957/VX3mtwAS+UfIwA4g==",
|
||||
"requires": {}
|
||||
}
|
||||
}
|
||||
}
|
29
package.json
Normal file
29
package.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "tconsole",
|
||||
"version": "1.0.0",
|
||||
"description": "Console log to terminal",
|
||||
"type": "main",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node server.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/qnkhuat/TConsole.git"
|
||||
},
|
||||
"bin": {
|
||||
"tconsole": "cli.js"
|
||||
},
|
||||
"preferGlobal": true,
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/qnkhuat/TConsole/issues"
|
||||
},
|
||||
"homepage": "https://github.com/qnkhuat/TConsole#readme",
|
||||
"dependencies": {
|
||||
"chalk": "^4.1.2",
|
||||
"ws": "^8.2.0"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user