From c95d417e0936b415ac9063c12ca2973856e35b71 Mon Sep 17 00:00:00 2001 From: Brandon Jordan Date: Mon, 29 Apr 2024 18:41:52 -0400 Subject: [PATCH] Improve and fix logging --- src/main.ts | 23 +++++++++++++++++------ src/render.ts | 29 +++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/main.ts b/src/main.ts index 7e8daaa..fe4e5a1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,7 +16,7 @@ export let container: HTMLElement; export let containers: HTMLElement[] = []; export let containerIndex: number = 0; -export const dev = import.meta.env.DEV; +export const dev = process.env.NODE_ENV ?? import.meta.env.DEV ?? false; export function resetContainers() { containers = []; @@ -80,6 +80,9 @@ export const Log = { error(...message: string[]) { Log.log('err', ...message); }, + fail(...message: string[]) { + Log.log('fail', ...message); + }, warn(...message: string[]) { Log.log('warn', ...message); }, @@ -95,6 +98,8 @@ export const Log = { case 'err': console.error(prefix, ...message); break; + case 'fail': + throw new Error([prefix, ...message].join(' ')); case 'warn': console.warn(prefix, ...message); break; @@ -118,6 +123,10 @@ export class ShortcutPreview { framework7: Framework7 constructor(options: PreviewOptions) { + if (dev) { + Log.debug('Running in development mode.'); + } + this.selector = options.selector ?? '#shortcut-preview'; this.name = options.name ?? null; this.url = options.url ?? null; @@ -129,7 +138,7 @@ export class ShortcutPreview { }); if (options.framework7) { - Log.debug('[preview-shortcut] ✅ Inherited Framework7 instance.'); + Log.debug('Inherited Framework7 instance.'); } if (this.data) { @@ -140,7 +149,7 @@ export class ShortcutPreview { this.loadURL(); return; } - throw new Error('[preview-shortcut] Missing `data` or `url` option.'); + Log.fail('Missing `data` or `url` option.'); } load(data: string | ShortcutData) { @@ -163,13 +172,14 @@ export class ShortcutPreview { } fetch(this.url).then(response => { if (response.status !== 200) { - throw new Error(`Unable to load shortcut (${response.status}): ${this.url}`); + Log.fail(`Unable to load shortcut (${response.status}): ${this.url}`); } + return response.text(); }).then(response => { this.load(response); }).catch(error => { - console.error(`[preview-shortcut] ${error}`); + Log.error(error.message, error); }); } @@ -177,7 +187,8 @@ export class ShortcutPreview { preview = document.querySelector(this.selector); preview?.classList.add('sp-preview'); if (!preview) { - throw new Error(`[preview-shortcut] Selector '${this.selector}' selects nothing.`); + Log.fail(`[preview-shortcut] Selector '${this.selector}' selects nothing.`); + return; } preview.innerHTML = ''; diff --git a/src/render.ts b/src/render.ts index 021500c..4edb927 100644 --- a/src/render.ts +++ b/src/render.ts @@ -2,6 +2,7 @@ import { ActionData, container, containerIndex, + dev, newContainer, prevContainer, resetContainers, @@ -24,7 +25,9 @@ let actionIndex = 0; let actionTotal = 0; export function renderShortcut(shortcutActions: Array) { - console.group('Render Shortcut'); + if (dev) { + console.group('Render Shortcut'); + } actionTotal = shortcutActions.length; for (const action of shortcutActions) { actionIndex++; @@ -51,7 +54,9 @@ export function renderShortcut(shortcutActions: Array) { newContainer(); } } - console.groupEnd(); + if (dev) { + console.groupEnd(); + } } function renderCardContent(element: HTMLElement) { @@ -65,7 +70,9 @@ function renderCardContent(element: HTMLElement) { } function renderAction(identifier: string, action: ActionData): Node { - console.group(`(${actionIndex}/${actionTotal}) Render ${action.WFWorkflowActionIdentifier}`); + if (dev) { + console.group(`(${actionIndex}/${actionTotal}) Render ${action.WFWorkflowActionIdentifier}`); + } const card = document.createElement('div'); card.className = 'card'; @@ -74,7 +81,9 @@ function renderAction(identifier: string, action: ActionData): Node { let actionData = null; if (actions[identifier]) { - console.log('Found definition.'); + if (dev) { + console.log('Found definition.'); + } actionData = actions[identifier]; if (actionData.title) { identifier = actionData.title; @@ -83,8 +92,10 @@ function renderAction(identifier: string, action: ActionData): Node { if (actionData && actionData.render) { card.innerHTML = renderCardContent(actionData.render(card, action.WFWorkflowActionParameters ?? [])).outerHTML; - console.log('Rendered by definition function.'); - console.groupEnd(); + if (dev) { + console.log('Rendered by definition function.'); + console.groupEnd(); + } return card; } @@ -96,8 +107,10 @@ function renderAction(identifier: string, action: ActionData): Node { } card.appendChild(renderCardContent(auto)); - console.log('Automatically rendered.'); - console.groupEnd(); + if (dev) { + console.log('Automatically rendered.'); + console.groupEnd(); + } return card; }