Skip to content

Commit

Permalink
feat: Add log lib
Browse files Browse the repository at this point in the history
  • Loading branch information
cyaiox committed Jul 11, 2024
1 parent 3521cc7 commit ba4c933
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions packages/main/src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
Expand All @@ -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',
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 = {
Expand All @@ -93,24 +100,24 @@ 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',
});
}
} 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});
}
Expand All @@ -128,15 +135,19 @@ 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;

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);
}

Expand All @@ -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});
});
}
Expand All @@ -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});
}
}

0 comments on commit ba4c933

Please sign in to comment.