refactor platform check utils

This commit is contained in:
Abhinav 2022-12-03 10:11:45 +05:30
parent a4500609ab
commit f181865e64
7 changed files with 32 additions and 24 deletions

View file

@ -1,8 +1,8 @@
import { ipcRenderer } from 'electron/renderer'; import { ipcRenderer } from 'electron/renderer';
import { isPlatformMac } from '../utils/preload'; import { isPlatform } from '../utils/preload';
export async function convertHEIC(fileData: Uint8Array): Promise<Uint8Array> { export async function convertHEIC(fileData: Uint8Array): Promise<Uint8Array> {
if (!isPlatformMac()) { if (!isPlatform('mac')) {
throw Error('native heic conversion only supported on mac'); throw Error('native heic conversion only supported on mac');
} }
const convertedFileData = await ipcRenderer.invoke( const convertedFileData = await ipcRenderer.invoke(

View file

@ -6,9 +6,9 @@ import semVerCmp from 'semver-compare';
import { AppUpdateInfo, GetFeatureFlagResponse } from '../types'; import { AppUpdateInfo, GetFeatureFlagResponse } from '../types';
import { getSkipAppVersion, setSkipAppVersion } from './userPreference'; import { getSkipAppVersion, setSkipAppVersion } from './userPreference';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import { isPlatformMac } from '../utils/main';
import { logErrorSentry } from './sentry'; import { logErrorSentry } from './sentry';
import ElectronLog from 'electron-log'; import ElectronLog from 'electron-log';
import { isPlatform } from 'utils/main';
const FIVE_MIN_IN_MICROSECOND = 5 * 60 * 1000; const FIVE_MIN_IN_MICROSECOND = 5 * 60 * 1000;
@ -43,7 +43,7 @@ export async function checkForUpdateAndNotify(mainWindow: BrowserWindow) {
const desktopCutoffVersion = await getDesktopCutoffVersion(); const desktopCutoffVersion = await getDesktopCutoffVersion();
if ( if (
desktopCutoffVersion && desktopCutoffVersion &&
isPlatformMac() && isPlatform('mac') &&
semVerCmp( semVerCmp(
updateCheckResult.updateInfo.version, updateCheckResult.updateInfo.version,
desktopCutoffVersion desktopCutoffVersion

View file

@ -1,12 +1,12 @@
import { isPlatform } from '../utils/main';
import { AutoLauncherClient } from '../types/autoLauncher'; import { AutoLauncherClient } from '../types/autoLauncher';
import { isPlatformWindows, isPlatformMac } from '../utils/main';
import linuxAutoLauncher from './autoLauncherClients/linuxAutoLauncher'; import linuxAutoLauncher from './autoLauncherClients/linuxAutoLauncher';
import macAndWindowsAutoLauncher from './autoLauncherClients/macAndWindowsAutoLauncher'; import macAndWindowsAutoLauncher from './autoLauncherClients/macAndWindowsAutoLauncher';
class AutoLauncher { class AutoLauncher {
private client: AutoLauncherClient; private client: AutoLauncherClient;
init() { init() {
if (isPlatformMac() || isPlatformWindows()) { if (isPlatform('mac') || isPlatform('windows')) {
this.client = macAndWindowsAutoLauncher; this.client = macAndWindowsAutoLauncher;
} else { } else {
this.client = linuxAutoLauncher; this.client = linuxAutoLauncher;

View file

@ -3,7 +3,7 @@ import * as path from 'path';
import { isDev } from './common'; import { isDev } from './common';
import { isAppQuitting } from '../main'; import { isAppQuitting } from '../main';
import { PROD_HOST_URL } from '../config'; import { PROD_HOST_URL } from '../config';
import { isPlatformMac } from './main'; import { isPlatform } from './main';
import { getHideDockIconPreference } from '../services/userPreference'; import { getHideDockIconPreference } from '../services/userPreference';
import autoLauncher from '../services/autoLauncher'; import autoLauncher from '../services/autoLauncher';
@ -83,12 +83,12 @@ export async function createWindow(): Promise<BrowserWindow> {
}); });
mainWindow.on('hide', () => { mainWindow.on('hide', () => {
const shouldHideDockIcon = getHideDockIconPreference(); const shouldHideDockIcon = getHideDockIconPreference();
if (isPlatformMac() && shouldHideDockIcon) { if (isPlatform('mac') && shouldHideDockIcon) {
app.dock.hide(); app.dock.hide();
} }
}); });
mainWindow.on('show', () => { mainWindow.on('show', () => {
if (isPlatformMac()) { if (isPlatform('mac')) {
app.dock.show(); app.dock.show();
} }
}); });

View file

@ -84,19 +84,23 @@ export function setupNextElectronServe() {
}); });
} }
export function isPlatformMac() { export function isPlatform(platform: 'mac' | 'windows' | 'linux') {
return process.platform === 'darwin'; if (process.platform === 'darwin') {
} return platform === 'mac';
} else if (process.platform === 'win32') {
export function isPlatformWindows() { return platform === 'windows';
return process.platform === 'win32'; } else if (process.platform === 'linux') {
return platform === 'linux';
} else {
return false;
}
} }
export async function handleDockIconHideOnAutoLaunch() { export async function handleDockIconHideOnAutoLaunch() {
const shouldHideDockIcon = getHideDockIconPreference(); const shouldHideDockIcon = getHideDockIconPreference();
const wasAutoLaunched = await autoLauncher.wasAutoLaunched(); const wasAutoLaunched = await autoLauncher.wasAutoLaunched();
if (isPlatformMac() && shouldHideDockIcon && wasAutoLaunched) { if (isPlatform('mac') && shouldHideDockIcon && wasAutoLaunched) {
app.dock.hide(); app.dock.hide();
} }
} }

View file

@ -11,7 +11,7 @@ import {
} from '../services/userPreference'; } from '../services/userPreference';
import { setIsAppQuitting } from '../main'; import { setIsAppQuitting } from '../main';
import autoLauncher from '../services/autoLauncher'; import autoLauncher from '../services/autoLauncher';
import { isPlatformMac } from './main'; import { isPlatform } from './main';
import ElectronLog from 'electron-log'; import ElectronLog from 'electron-log';
export function buildContextMenu( export function buildContextMenu(
@ -88,7 +88,7 @@ export function buildContextMenu(
export async function buildMenuBar(): Promise<Menu> { export async function buildMenuBar(): Promise<Menu> {
let isAutoLaunchEnabled = await autoLauncher.isEnabled(); let isAutoLaunchEnabled = await autoLauncher.isEnabled();
const isMac = isPlatformMac(); const isMac = isPlatform('mac');
let shouldHideDockIcon = getHideDockIconPreference(); let shouldHideDockIcon = getHideDockIconPreference();
const template: MenuItemConstructorOptions[] = [ const template: MenuItemConstructorOptions[] = [
{ {

View file

@ -15,10 +15,14 @@ export const fixHotReloadNext12 = () => {
});`); });`);
}; };
export function isPlatformMac() { export function isPlatform(platform: 'mac' | 'windows' | 'linux') {
return process.platform === 'darwin'; if (process.platform === 'darwin') {
} return platform === 'mac';
} else if (process.platform === 'win32') {
export function isPlatformWindows() { return platform === 'windows';
return process.platform === 'win32'; } else if (process.platform === 'linux') {
return platform === 'linux';
} else {
return false;
}
} }