diff --git a/src/supabase/helpers/tables/logs.ts b/src/supabase/helpers/tables/logs.ts index 2a146e4..c36ccd8 100644 --- a/src/supabase/helpers/tables/logs.ts +++ b/src/supabase/helpers/tables/logs.ts @@ -52,55 +52,51 @@ export class Logs { return metadata; } - public ok(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null { + public ok(log: string, metadata?: Metadata): LogReturn | null { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.INFO, consoleLog: Logs.console.ok, logMessage: log, metadata, - postComment, type: "ok", }); } - public info(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null { + public info(log: string, metadata?: Metadata): LogReturn | null { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.INFO, consoleLog: Logs.console.info, logMessage: log, metadata, - postComment, type: "info", }); } - public error(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null { + public error(log: string, metadata?: Metadata): LogReturn | null { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.ERROR, consoleLog: Logs.console.error, logMessage: log, metadata, - postComment, type: "error", }); } - public debug(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null { + public debug(log: string, metadata?: Metadata): LogReturn | null { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.DEBUG, consoleLog: Logs.console.debug, logMessage: log, metadata, - postComment, type: "debug", }); } - public fatal(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null { + public fatal(log: string, metadata?: Metadata): LogReturn | null { if (!metadata) { metadata = Logs.convertErrorsIntoObjects(new Error(log)) as Metadata; const stack = metadata.stack as string[]; @@ -121,19 +117,17 @@ export class Logs { consoleLog: Logs.console.fatal, logMessage: log, metadata, - postComment, type: "fatal", }); } - public verbose(log: string, metadata?: Metadata, postComment?: boolean): LogReturn | null { + public verbose(log: string, metadata?: Metadata): LogReturn | null { metadata = this._addDiagnosticInformation(metadata); return this._log({ level: LOG_LEVEL.VERBOSE, consoleLog: Logs.console.verbose, logMessage: log, metadata, - postComment, type: "verbose", }); } diff --git a/src/supabase/helpers/tables/pretty-logs.ts b/src/supabase/helpers/tables/pretty-logs.ts index 2af7988..736cf6a 100644 --- a/src/supabase/helpers/tables/pretty-logs.ts +++ b/src/supabase/helpers/tables/pretty-logs.ts @@ -1,4 +1,3 @@ -import util from "util"; import { Colors, Metadata, PrettyLogsWithOk } from "../../types/log-types"; import { COLORS, LOG_LEVEL } from "../../constants"; @@ -109,7 +108,7 @@ export class PrettyLogs { const symbol = defaultSymbols[type]; // Formatting the message - const messageFormatted = typeof message === "string" ? message : util.inspect(message, { showHidden: true, depth: null, breakLength: Infinity }); + const messageFormatted = typeof message === "string" ? message : JSON.stringify(message, null, 2); // const messageFormatted = // typeof message === "string" ? message : JSON.stringify(Logs.convertErrorsIntoObjects(message)); @@ -135,8 +134,11 @@ export class PrettyLogs { }; const _console = console[colorMap[type][0] as keyof typeof console] as (...args: string[]) => void; - if (typeof _console === "function") { + if (typeof _console === "function" && fullLogString.length > 12) { _console(this._colorizeText(fullLogString, colorMap[type][1])); + } else if (fullLogString.length <= 12) { + // removing empty logs which only contain the symbol + return; } else { throw new Error(fullLogString); } diff --git a/src/supabase/types/log-types.ts b/src/supabase/types/log-types.ts index 996b45b..60f14d2 100644 --- a/src/supabase/types/log-types.ts +++ b/src/supabase/types/log-types.ts @@ -13,7 +13,6 @@ export type LogParams = { consoleLog: LogFunction; logMessage: string; metadata?: Metadata; - postComment?: boolean; type: PrettyLogsWithOk; }; diff --git a/src/supabase/utils.ts b/src/supabase/utils.ts new file mode 100644 index 0000000..210fdf0 --- /dev/null +++ b/src/supabase/utils.ts @@ -0,0 +1,32 @@ +const ansiEscapeCodes = /\x1b\[\d+m|\s/g; + +function cleanLogs( + spy: jest.SpiedFunction<{ + (...data: string[]): void; + (message?: string, ...optionalParams: string[]): void; + }> +) { + const strs = spy.mock.calls.map((call) => call.map((str) => str?.toString()).join(" ")); + return strs.flat().map((str) => cleanLogString(str)); +} + +export function cleanLogString(logString: string) { + return logString.replaceAll(ansiEscapeCodes, "").replaceAll(/\n/g, "").replaceAll(/\r/g, "").replaceAll(/\t/g, "").trim(); +} + +export function cleanSpyLogs( + spy: jest.SpiedFunction<{ + (...data: string[]): void; + (message?: string, ...optionalParams: string[]): void; + }> +): string[] { + return cleanLogs(spy); +} + +export function tryError() { + try { + throw new Error("This is an error"); + } catch (e) { + return e as Error; + } +} diff --git a/static/fonts/ubiquity-nova-standard.eot b/static/fonts/ubiquity-nova-standard.eot deleted file mode 100644 index 1e46099..0000000 Binary files a/static/fonts/ubiquity-nova-standard.eot and /dev/null differ diff --git a/static/fonts/ubiquity-nova-standard.ttf b/static/fonts/ubiquity-nova-standard.ttf deleted file mode 100644 index d953486..0000000 Binary files a/static/fonts/ubiquity-nova-standard.ttf and /dev/null differ diff --git a/static/fonts/ubiquity-nova-standard.woff b/static/fonts/ubiquity-nova-standard.woff deleted file mode 100644 index 0c2691e..0000000 Binary files a/static/fonts/ubiquity-nova-standard.woff and /dev/null differ diff --git a/static/index.html b/static/index.html deleted file mode 100644 index dbae5f4..0000000 --- a/static/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - Ubiquity TypeScript Template - - - -

Ubiquity TypeScript Template

- - - diff --git a/static/main.ts b/static/main.ts deleted file mode 100644 index b19bfa7..0000000 --- a/static/main.ts +++ /dev/null @@ -1,10 +0,0 @@ -export async function mainModule() { - console.log(`Hello from mainModule`); -} -mainModule() - .then(() => { - console.log("mainModule loaded"); - }) - .catch((error) => { - console.error(error); - }); diff --git a/static/style.css b/static/style.css deleted file mode 100644 index 3bbb3f0..0000000 --- a/static/style.css +++ /dev/null @@ -1,16 +0,0 @@ -body { - font-family: "Proxima Nova", "Ubiquity Nova", sans-serif; - background-color: #06061aff; - color: #fff; - background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAABYWlDQ1BrQ0dDb2xvclNwYWNlRGlzcGxheVAzAAAokWNgYFJJLCjIYWFgYMjNKykKcndSiIiMUmB/yMAOhLwMYgwKicnFBY4BAT5AJQwwGhV8u8bACKIv64LMOiU1tUm1XsDXYqbw1YuvRJsw1aMArpTU4mQg/QeIU5MLikoYGBhTgGzl8pICELsDyBYpAjoKyJ4DYqdD2BtA7CQI+whYTUiQM5B9A8hWSM5IBJrB+API1klCEk9HYkPtBQFul8zigpzESoUAYwKuJQOUpFaUgGjn/ILKosz0jBIFR2AopSp45iXr6SgYGRiaMzCAwhyi+nMgOCwZxc4gxJrvMzDY7v////9uhJjXfgaGjUCdXDsRYhoWDAyC3AwMJ3YWJBYlgoWYgZgpLY2B4dNyBgbeSAYG4QtAPdHFacZGYHlGHicGBtZ7//9/VmNgYJ/MwPB3wv//vxf9//93MVDzHQaGA3kAFSFl7jXH0fsAAAA4ZVhJZk1NACoAAAAIAAGHaQAEAAAAAQAAABoAAAAAAAKgAgAEAAAAAQAAABigAwAEAAAAAQAAABgAAAAAwf1XlwAAACNJREFUSA3t0IEAAAAMBKFHm7/UTaQQWnXDgAEDBgwYMGDgAXaJAz4RVVHYAAAAAElFTkSuQmCC"); -} -@font-face { - font-family: "Ubiquity Nova"; - font-style: normal; - font-weight: 400; - src: url(./fonts/ubiquity-nova-standard.eot); - src: - url(./fonts/ubiquity-nova-standard.eot#iefix) format("embedded-opentype"), - url(./fonts/ubiquity-nova-standard.woff) format("woff"), - url(./fonts/ubiquity-nova-standard.ttf) format("truetype"); -} diff --git a/tests/logs.test.ts b/tests/logs.test.ts index b8979d6..0977746 100644 --- a/tests/logs.test.ts +++ b/tests/logs.test.ts @@ -1,5 +1,6 @@ import { LOG_LEVEL } from "../src/supabase/constants"; import { Logs } from "../src/supabase/helpers/tables/logs"; +import { LogReturn } from "../src/supabase/types/log-types"; describe("Logs", () => { let logs: Logs; @@ -37,4 +38,13 @@ describe("Logs", () => { const logReturn = logs.verbose("This is a VERBOSE message"); expect(logReturn).not.toBeNull(); }); + + it("should return a LogReturn object", () => { + const logReturn: LogReturn | null = logs.ok("This is an OK message"); + expect(logReturn).toBeInstanceOf(LogReturn); + const msg = logReturn?.logMessage; + const metadata = logReturn?.metadata; + expect(msg).toHaveProperty("diff"); + expect(metadata).toHaveProperty("caller"); + }); }); diff --git a/tests/pretty-logs.test.ts b/tests/pretty-logs.test.ts index d159cdd..0e15824 100644 --- a/tests/pretty-logs.test.ts +++ b/tests/pretty-logs.test.ts @@ -1,20 +1,5 @@ import { PrettyLogs } from "../src/supabase/helpers/tables/pretty-logs"; - -const ansiEscapeCodes = /\x1b\[\d+m|\s/g; - -function cleanSpyLogs( - spy: jest.SpiedFunction<{ - (...data: any[]): void; - (message?: any, ...optionalParams: any[]): void; - }> -) { - const strs = spy.mock.calls.map((call) => call.map((str) => str.toString()).join(" ")); - return strs.flat().map((str) => cleanLogString(str)); -} - -function cleanLogString(logString: string) { - return logString.replace(ansiEscapeCodes, "").replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, ""); -} +import { cleanLogString, cleanSpyLogs, tryError } from "../src/supabase/utils"; describe("PrettyLogs", () => { let logs: PrettyLogs; @@ -56,7 +41,7 @@ describe("PrettyLogs", () => { const logReturn = logs.debug("This is a DEBUG message"); expect(logReturn).toBeUndefined(); const cleanLogStrings = cleanSpyLogs(logSpy); - expect(cleanLogStrings).toContain(cleanLogString(" ›› This is a DEBUG message")) + expect(cleanLogStrings).toContain(cleanLogString(" ›› This is a DEBUG message")); }); it("should log a 'fatal' message", () => { @@ -72,6 +57,40 @@ describe("PrettyLogs", () => { const logReturn = logs.verbose("This is a VERBOSE message"); expect(logReturn).toBeUndefined(); const cleanLogStrings = cleanSpyLogs(logSpy); - expect(cleanLogStrings).toContain(cleanLogString((" 💬 This is a VERBOSE message"))); + expect(cleanLogStrings).toContain(cleanLogString(" 💬 This is a VERBOSE message")); + }); + + it("should log metadata", () => { + const logSpy = jest.spyOn(console, "debug").mockImplementation(); + const logReturn = logs.debug("This is a METADATA message", { thisIsMetadata: true, stuff: Array(5).fill("stuff"), moreStuff: { a: "a", b: "b" } }); + expect(logReturn).toBeUndefined(); + const cleanLogStrings = cleanSpyLogs(logSpy); + expect(cleanLogStrings).toEqual([ + cleanLogString(" ›› This is a METADATA message"), + cleanLogString(` ›› ${JSON.stringify({ thisIsMetadata: true, stuff: ["stuff", "stuff", "stuff", "stuff", "stuff"], moreStuff: { a: "a", b: "b" } })}`), + ]); + }); + + it("should log metadata as a string", () => { + const logSpy = jest.spyOn(console, "debug").mockImplementation(); + const logReturn = logs.debug("This is a METADATA message", "This is metadata as a string"); + expect(logReturn).toBeUndefined(); + const cleanLogStrings = cleanSpyLogs(logSpy); + expect(cleanLogStrings).toEqual([cleanLogString(" ›› This is a METADATA message"), cleanLogString(" ›› This is metadata as a string")]); + }); + + it("should log an error and stack", () => { + const logSpy = jest.spyOn(console, "debug").mockImplementation(); + const logReturn = logs.debug("This is a METADATA message", { error: tryError() }); + expect(logReturn).toBeUndefined(); + const cleanLogStrings = cleanSpyLogs(logSpy); + + const errorRegex = /↳tryError\(.+\)↳Object.\(/; + + expect(cleanLogStrings).toEqual([ + cleanLogString(" ›› This is a METADATA message"), + cleanLogString(` ›› ${JSON.stringify({ error: {} })}`), + expect.stringMatching(errorRegex), + ]); }); -}); \ No newline at end of file +});