usable
This commit is contained in:
parent
64b8901159
commit
fab43703cc
@ -1,2 +1,5 @@
|
|||||||
# TConsole
|
# TConsole
|
||||||
Console log to terminal
|
Console log to terminal
|
||||||
|
|
||||||
|
|
||||||
|
# https://stackoverflow.com/questions/65822409/logging-to-terminal-console-instead-of-browser-console-in-react-js
|
||||||
|
51
cli.js
51
cli.js
@ -1,35 +1,58 @@
|
|||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
|
const repl = require('repl');
|
||||||
|
|
||||||
const { log } = console;
|
const getTime = () => {
|
||||||
|
const now = new Date();
|
||||||
|
const hours = (now.getHours() > 9 ? "" : "0") + now.getHours();
|
||||||
|
const minutes = (now.getMinutes() > 9 ? "" : "0") + now.getMinutes();
|
||||||
|
const seconds = (now.getSeconds() > 9 ? "" : "0") + now.getSeconds();
|
||||||
|
return `[${hours}:${minutes}:${seconds}]`;
|
||||||
|
}
|
||||||
|
|
||||||
const logParse = (data) => {
|
const out = (text, color) => {
|
||||||
return data.map((it) => JSON.stringify(it)).join(' ');
|
switch (color) {
|
||||||
};
|
case "white":
|
||||||
|
console.log(getTime(), chalk.white(text))
|
||||||
|
break;
|
||||||
|
case "red":
|
||||||
|
console.log(getTime(), chalk.red(text))
|
||||||
|
break;
|
||||||
|
case "yellow":
|
||||||
|
console.log(getTime(), chalk.yellow(text))
|
||||||
|
break;
|
||||||
|
case "blue":
|
||||||
|
console.log(getTime(), chalk.blue(text))
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log(getTime(), chalk.white(text))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const wss = new WebSocket.Server({ port: 8080 });
|
const server = new WebSocket.Server({ port: 8080 });
|
||||||
|
|
||||||
wss.on('connection', function connection(ws) {
|
server.on('connection', (ws) => {
|
||||||
ws.on('message', function incoming(message) {
|
ws.on('message', (message) => {
|
||||||
const event = JSON.parse(message);
|
const event = JSON.parse(message);
|
||||||
const { type, data } = event;
|
const { type, data } = event;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'log':
|
case 'log':
|
||||||
log('>>', logParse(data));
|
data.forEach((text) => out(text, "white"));
|
||||||
break;
|
break;
|
||||||
case 'error':
|
case 'error':
|
||||||
log('>>', chalk.red(logParse(data)));
|
data.forEach((text) => out(text, "red"));
|
||||||
break;
|
break;
|
||||||
case 'warn':
|
case 'warn':
|
||||||
log('>>', chalk.yellow(logParse(data)));
|
data.forEach((text) => out(text, "yellow"));
|
||||||
break;
|
break;
|
||||||
case 'debug':
|
case 'debug':
|
||||||
log('>>', chalk.blue(logParse(data)));
|
data.forEach((text) => out(text, "blue"));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
log('>>', logParse(data));
|
data.forEach((text) => out(text, "white"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.send('something');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const r = repl.start('> ');
|
||||||
|
2
index.d.ts
vendored
2
index.d.ts
vendored
@ -1,2 +1,2 @@
|
|||||||
export default tconsole;
|
export default tconsole;
|
||||||
declare function tconsole(options: any): void;
|
declare function tconsole(options: any = {}): void;
|
||||||
|
111
index.js
111
index.js
@ -1,4 +1,74 @@
|
|||||||
|
let ws = null;
|
||||||
|
|
||||||
|
// https://stackoverflow.com/a/44782052/7539840
|
||||||
|
const defaultConsole = Object.assign(Object.create(Object.getPrototypeOf(console)), console);
|
||||||
|
|
||||||
|
const configure = (conn) => {
|
||||||
|
// already configured
|
||||||
|
if (console.stdlog) return;
|
||||||
|
|
||||||
|
console.log = (...args) => {
|
||||||
|
sendWhenConnected(conn, JSON.stringify({ type: 'log', data: Array.from(args) }));
|
||||||
|
defaultConsole.log.apply(defaultConsole, args);
|
||||||
|
};
|
||||||
|
|
||||||
|
console.error = (...args) => {
|
||||||
|
sendWhenConnected(conn, JSON.stringify({ type: 'error', data: Array.from(args) }));
|
||||||
|
defaultConsole.error.apply(defaultConsole, args);
|
||||||
|
};
|
||||||
|
|
||||||
|
console.warn = (...args) => {
|
||||||
|
sendWhenConnected(conn, JSON.stringify({ type: 'warn', data: Array.from(args) }));
|
||||||
|
defaultConsole.warn.apply(defaultConsole, args);
|
||||||
|
};
|
||||||
|
|
||||||
|
console.debug = (...args) => {
|
||||||
|
sendWhenConnected(conn, JSON.stringify({ type: 'debug', data: Array.from(args) }));
|
||||||
|
defaultConsole.debug.apply(defaultConsole, args);
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("[TCONSOLE] CONFIGURED");
|
||||||
|
}
|
||||||
|
|
||||||
|
const release = () => {
|
||||||
|
console = defaultConsole;
|
||||||
|
console.log("[TCONSOLE]: Released");
|
||||||
|
ws = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tconsole = (options = {}) => {
|
||||||
|
if (ws) return; // already running
|
||||||
|
|
||||||
|
options = {
|
||||||
|
host: "localhost",
|
||||||
|
port: 8080,
|
||||||
|
...options,
|
||||||
|
}
|
||||||
|
|
||||||
|
ws = new WebSocket(`ws://${options.host}:${options.port}`);
|
||||||
|
ws.onopen = () => {
|
||||||
|
console.log('[TCONSOLE]: connected');
|
||||||
|
configure(ws);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onclose = (event) => {
|
||||||
|
release();
|
||||||
|
console.log("[TCONSOLE]: Disconnected", event.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.onerror = (event) => {
|
||||||
|
release();
|
||||||
|
console.error("[TCONSOLE]: Disconnected", event.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// *** Utils *** //
|
||||||
const sendWhenConnected = (ws, msg, n = 0, maxTries = 100) => {
|
const sendWhenConnected = (ws, msg, n = 0, maxTries = 100) => {
|
||||||
|
// Closing or closed
|
||||||
|
if (ws.readyState === 2 || ws.readyState === 3) return;
|
||||||
|
|
||||||
|
// try sending
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (ws.readyState === 1) {
|
if (ws.readyState === 1) {
|
||||||
ws.send(msg);
|
ws.send(msg);
|
||||||
@ -10,45 +80,4 @@ const sendWhenConnected = (ws, msg, n = 0, maxTries = 100) => {
|
|||||||
}, 10); // wait 10 milisecond for the connection...
|
}, 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;
|
export default tconsole;
|
||||||
|
16
package-lock.json
generated
16
package-lock.json
generated
@ -10,10 +10,11 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^4.1.2",
|
"chalk": "^4.1.2",
|
||||||
|
"repl": "^0.1.3",
|
||||||
"ws": "^8.2.0"
|
"ws": "^8.2.0"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"tconsole": "server.js"
|
"tconsole": "cli.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/ansi-styles": {
|
"node_modules/ansi-styles": {
|
||||||
@ -69,6 +70,14 @@
|
|||||||
"node": ">=8"
|
"node": ">=8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/repl": {
|
||||||
|
"version": "0.1.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/repl/-/repl-0.1.3.tgz",
|
||||||
|
"integrity": "sha1-LwXUKwyItD0FzL2hDtFK7/Vpm2A=",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/supports-color": {
|
"node_modules/supports-color": {
|
||||||
"version": "7.2.0",
|
"version": "7.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
||||||
@ -137,6 +146,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
|
||||||
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="
|
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="
|
||||||
},
|
},
|
||||||
|
"repl": {
|
||||||
|
"version": "0.1.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/repl/-/repl-0.1.3.tgz",
|
||||||
|
"integrity": "sha1-LwXUKwyItD0FzL2hDtFK7/Vpm2A="
|
||||||
|
},
|
||||||
"supports-color": {
|
"supports-color": {
|
||||||
"version": "7.2.0",
|
"version": "7.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
"homepage": "https://github.com/qnkhuat/TConsole#readme",
|
"homepage": "https://github.com/qnkhuat/TConsole#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^4.1.2",
|
"chalk": "^4.1.2",
|
||||||
|
"repl": "^0.1.3",
|
||||||
"ws": "^8.2.0"
|
"ws": "^8.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user