From ba4c933650fed74a6ebd2a4a3af2945220240ca6 Mon Sep 17 00:00:00 2001 From: Gabriel Diaz Date: Thu, 11 Jul 2024 16:25:35 -0300 Subject: [PATCH] feat: Add log lib --- packages/main/src/ipc.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/main/src/ipc.ts b/packages/main/src/ipc.ts index ebd9dd2..f0cec54 100644 --- a/packages/main/src/ipc.ts +++ b/packages/main/src/ipc.ts @@ -3,6 +3,7 @@ import fs from 'node:fs'; import {spawn} from 'child_process'; import {app, BrowserWindow} from 'electron'; import {CancelError, download} from 'electron-dl'; +import log from 'electron-log'; import {IPC_EVENTS, IPC_EVENT_DATA_TYPE} from '#shared'; import { getAppBasePath, @@ -13,6 +14,8 @@ import { PLATFORM, } from './helpers'; +log.initialize(); + export const EXPLORER_PATH = join(getAppBasePath(), 'Explorer'); export const EXPLORER_DOWNLOADS_PATH = join(EXPLORER_PATH, 'downloads'); export const EXPLORER_VERSION_PATH = join(EXPLORER_PATH, 'version.json'); @@ -24,13 +27,13 @@ export async function downloadApp(event: Electron.IpcMainInvokeEvent, url: strin const win = BrowserWindow.getFocusedWindow(); if (!win) return; - console.log('[Main Window] Downloading', url); + log.debug('[Main Window] Downloading', url); const versionPattern = /https:\/\/github.com\/decentraland\/.+\/releases\/download\/(v?\d+\.\d+\.\d+-?\w+)\/(\w+.zip)/; const version = url.match(versionPattern)?.[1]; if (!version) { - console.error('[Main Window] No version provided'); + log.error('[Main Window] No version provided'); event.sender.send(IPC_EVENTS.DOWNLOAD_STATE, { type: IPC_EVENT_DATA_TYPE.ERROR, error: 'No version provided', @@ -46,7 +49,7 @@ export async function downloadApp(event: Electron.IpcMainInvokeEvent, url: strin : null; if (versionData && versionData.version === version) { - console.log('[Main Window] This version is already installed'); + log.debug('[Main Window] This version is already installed'); event.sender.send(IPC_EVENTS.DOWNLOAD_STATE, {type: IPC_EVENT_DATA_TYPE.CANCELLED}); return; } @@ -83,6 +86,10 @@ export async function downloadApp(event: Electron.IpcMainInvokeEvent, url: strin } } else if (getOSName() === PLATFORM.WINDOWS) { // TODO: Implement permissions for Windows + const explorerBinPath = join(branchPath, EXPLORER_WIN_BIN_PATH); + if (fs.existsSync(explorerBinPath)) { + // TODO: Implement permissions for Windows + } } const versionData = { @@ -93,14 +100,14 @@ export async function downloadApp(event: Electron.IpcMainInvokeEvent, url: strin event.sender.send(IPC_EVENTS.INSTALL_STATE, {type: IPC_EVENT_DATA_TYPE.COMPLETED}); } catch (error) { - console.error('Failed to install app:', error); + log.error('Failed to install app:', error); event.sender.send(IPC_EVENTS.INSTALL_STATE, {type: IPC_EVENT_DATA_TYPE.ERROR, error}); } }, }); return JSON.stringify(resp); } else { - console.error('[Main Window] No URL provided'); + log.error('[Main Window] No URL provided'); event.sender.send(IPC_EVENTS.DOWNLOAD_STATE, { type: IPC_EVENT_DATA_TYPE.ERROR, error: 'No URL provided', @@ -108,9 +115,9 @@ export async function downloadApp(event: Electron.IpcMainInvokeEvent, url: strin } } catch (error) { if (error instanceof CancelError) { - console.info('[Main Window] item.cancel() was called'); + log.info('[Main Window] item.cancel() was called'); } else { - console.error('[Main Window] Error Downloading', url, error); + log.error('[Main Window] Error Downloading', url, error); } event.sender.send(IPC_EVENTS.DOWNLOAD_STATE, {type: IPC_EVENT_DATA_TYPE.ERROR, error}); } @@ -128,6 +135,8 @@ export function isExplorerUpdated(_event: Electron.IpcMainInvokeEvent, version: export function openApp(event: Electron.IpcMainInvokeEvent, _app: string) { try { + log.debug('[Main Window] Opening App'); + const versionData = fs.existsSync(EXPLORER_VERSION_PATH) ? JSON.parse(fs.readFileSync(EXPLORER_VERSION_PATH, 'utf8')) : null; @@ -135,8 +144,10 @@ export function openApp(event: Electron.IpcMainInvokeEvent, _app: string) { if (!!versionData && !!versionData.version) { let explorerBinPath = ''; if (getOSName() === 'mac') { + log.debug('[Main Window] Mac OS Version'); explorerBinPath = join(EXPLORER_PATH, versionData.version, EXPLORER_MAC_BIN_PATH); } else if (getOSName() === 'win32') { + log.debug('[Main Window] Windows OS Version'); explorerBinPath = join(EXPLORER_PATH, versionData.version, EXPLORER_WIN_BIN_PATH); } @@ -149,7 +160,7 @@ export function openApp(event: Electron.IpcMainInvokeEvent, _app: string) { app.quit(); }) .on('error', error => { - console.error('Failed to open app:', error); + log.error('[Main window] Failed to open app:', error); event.sender.send(IPC_EVENTS.OPEN_APP, {type: IPC_EVENT_DATA_TYPE.ERROR, error}); }); } @@ -160,7 +171,7 @@ export function openApp(event: Electron.IpcMainInvokeEvent, _app: string) { }); } } catch (error) { - console.error('Failed to open app:', error); + log.error('[Main Window] Failed to open app:', error); event.sender.send(IPC_EVENTS.OPEN_APP, {type: IPC_EVENT_DATA_TYPE.ERROR, error}); } }