Skip to content

Commit

Permalink
Improve and fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
electrikmilk committed Apr 29, 2024
1 parent a45b9f1 commit c95d417
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
23 changes: 17 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
},
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -163,21 +172,23 @@ 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);
});
}

preview() {
preview = document.querySelector<HTMLDivElement>(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 = '';

Expand Down
29 changes: 21 additions & 8 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ActionData,
container,
containerIndex,
dev,
newContainer,
prevContainer,
resetContainers,
Expand All @@ -24,7 +25,9 @@ let actionIndex = 0;
let actionTotal = 0;

export function renderShortcut(shortcutActions: Array<ActionData>) {
console.group('Render Shortcut');
if (dev) {
console.group('Render Shortcut');
}
actionTotal = shortcutActions.length;
for (const action of shortcutActions) {
actionIndex++;
Expand All @@ -51,7 +54,9 @@ export function renderShortcut(shortcutActions: Array<ActionData>) {
newContainer();
}
}
console.groupEnd();
if (dev) {
console.groupEnd();
}
}

function renderCardContent(element: HTMLElement) {
Expand All @@ -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';
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit c95d417

Please sign in to comment.