From 7612949b153ca25b8d36920732dd37defec7a581 Mon Sep 17 00:00:00 2001 From: Vijay Aravamudhan Date: Sat, 2 Oct 2021 05:47:36 +0530 Subject: refactor: remove 'electron-util' and 'electron-is-dev' as dependencies (#2008) (pre-requisite for electron v14) --- src/electron-util.ts | 30 +++++++++++++++++++++++++ src/enforce-macos-app-location.ts | 46 +++++++++++++++++++++++++++++++++++++++ src/environment-remote.ts | 2 +- src/index.js | 5 +++-- 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/electron-util.ts create mode 100644 src/enforce-macos-app-location.ts (limited to 'src') diff --git a/src/electron-util.ts b/src/electron-util.ts new file mode 100644 index 000000000..f4b26cb10 --- /dev/null +++ b/src/electron-util.ts @@ -0,0 +1,30 @@ +// Enhanced from: https://github.com/dertieran/electron-util/blob/replace-remote/source/api.js + +import electron from 'electron'; +import { initialize } from '@electron/remote/main'; + +export const initializeRemote = () => { + if (process.type !== 'browser') { + throw new Error('The remote api must be initialized from the main process.'); + } + + initialize(); +}; + +export const remote = new Proxy({}, { + get: (_target, property) => { + // eslint-disable-next-line global-require + const remote = require('@electron/remote'); + return remote[property]; + }, +}); + +export const api = new Proxy(electron, { + get: (target, property) => { + if (target[property]) { + return target[property]; + } + + return remote[property]; + }, +}); diff --git a/src/enforce-macos-app-location.ts b/src/enforce-macos-app-location.ts new file mode 100644 index 000000000..0f858013d --- /dev/null +++ b/src/enforce-macos-app-location.ts @@ -0,0 +1,46 @@ +// Enhanced from: https://github.com/dertieran/electron-util/blob/replace-remote/source/enforce-macos-app-location.js + +import { isMac } from './environment'; +import { isDevMode } from './environment-remote'; +import { api } from './electron-util'; + +export function enforceMacOSAppLocation() { + if (isDevMode || !isMac || api.app.isInApplicationsFolder()) { + return; + } + + const clickedButtonIndex = api.dialog.showMessageBoxSync({ + type: 'error', + message: 'Move to Applications folder?', + detail: 'Ferdi must live in the Applications folder to be able to run correctly.', + buttons: [ + 'Move to Applications folder', + 'Quit Ferdi', + ], + defaultId: 0, + cancelId: 1, + }); + + if (clickedButtonIndex === 1) { + api.app.quit(); + return; + } + + api.app.moveToApplicationsFolder({ + conflictHandler: conflict => { + if (conflict === 'existsAndRunning') { // Can't replace the active version of the app + api.dialog.showMessageBoxSync({ + type: 'error', + message: 'Another version of Ferdi is currently running. Quit it, then launch this version of the app again.', + buttons: [ + 'OK', + ], + }); + + api.app.quit(); + } + + return true; + }, + }); +} diff --git a/src/environment-remote.ts b/src/environment-remote.ts index 1ff019c5f..89926a428 100644 --- a/src/environment-remote.ts +++ b/src/environment-remote.ts @@ -1,6 +1,6 @@ import { join } from 'path'; import osName from 'os-name'; -import { api as electronApi } from 'electron-util'; +import { api as electronApi } from './electron-util'; import { LIVE_FERDI_API, DEV_FRANZ_API, diff --git a/src/index.js b/src/index.js index ed37134c9..87f45e7fc 100644 --- a/src/index.js +++ b/src/index.js @@ -5,10 +5,11 @@ import { app, BrowserWindow, ipcMain, session, dialog } from 'electron'; import { emptyDirSync, ensureFileSync } from 'fs-extra'; import { join } from 'path'; import windowStateKeeper from 'electron-window-state'; -import { enforceMacOSAppLocation } from 'electron-util'; import ms from 'ms'; +import { initializeRemote } from './electron-util'; +import { enforceMacOSAppLocation } from './enforce-macos-app-location'; -require('@electron/remote/main').initialize(); +initializeRemote(); import { DEFAULT_APP_SETTINGS, DEFAULT_WINDOW_OPTIONS } from './config'; -- cgit v1.2.3-54-g00ecf