diff --git a/README.md b/README.md index 3c8820a..1c4ec69 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # TConsole Console log to terminal + + +# https://stackoverflow.com/questions/65822409/logging-to-terminal-console-instead-of-browser-console-in-react-js diff --git a/cli.js b/cli.js index 07c9196..12875f8 100644 --- a/cli.js +++ b/cli.js @@ -1,35 +1,58 @@ const WebSocket = require('ws'); 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) => { - return data.map((it) => JSON.stringify(it)).join(' '); -}; +const out = (text, color) => { + 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) { - ws.on('message', function incoming(message) { +server.on('connection', (ws) => { + ws.on('message', (message) => { const event = JSON.parse(message); const { type, data } = event; switch (type) { case 'log': - log('>>', logParse(data)); + data.forEach((text) => out(text, "white")); break; case 'error': - log('>>', chalk.red(logParse(data))); + data.forEach((text) => out(text, "red")); break; case 'warn': - log('>>', chalk.yellow(logParse(data))); + data.forEach((text) => out(text, "yellow")); break; case 'debug': - log('>>', chalk.blue(logParse(data))); + data.forEach((text) => out(text, "blue")); break; default: - log('>>', logParse(data)); + data.forEach((text) => out(text, "white")); } }); - - ws.send('something'); }); + +const r = repl.start('> '); diff --git a/index.d.ts b/index.d.ts index 0dab9d8..d7f170e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,2 +1,2 @@ export default tconsole; -declare function tconsole(options: any): void; +declare function tconsole(options: any = {}): void; diff --git a/index.js b/index.js index 5118289..a378964 100755 --- a/index.js +++ b/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) => { + // Closing or closed + if (ws.readyState === 2 || ws.readyState === 3) return; + + // try sending setTimeout(() => { if (ws.readyState === 1) { ws.send(msg); @@ -10,45 +80,4 @@ const sendWhenConnected = (ws, msg, n = 0, maxTries = 100) => { }, 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; diff --git a/package-lock.json b/package-lock.json index 0d6cdb5..51c60da 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,10 +10,11 @@ "license": "ISC", "dependencies": { "chalk": "^4.1.2", + "repl": "^0.1.3", "ws": "^8.2.0" }, "bin": { - "tconsole": "server.js" + "tconsole": "cli.js" } }, "node_modules/ansi-styles": { @@ -69,6 +70,14 @@ "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": { "version": "7.2.0", "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", "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": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", diff --git a/package.json b/package.json index a8bfa23..101218d 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "homepage": "https://github.com/qnkhuat/TConsole#readme", "dependencies": { "chalk": "^4.1.2", + "repl": "^0.1.3", "ws": "^8.2.0" } }