Skip to content

Commit ba164dd

Browse files
committed
a
1 parent 6e54a46 commit ba164dd

13 files changed

+119
-68
lines changed

src/main/config.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import fs from 'fs';
22
import { app } from 'electron';
33

4-
const isDev: boolean = process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';
4+
const isDev: boolean =
5+
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';
56

67
function getProductNameFromPackageJson() {
7-
let data = fs.readFileSync('./package.json', { encoding: "utf-8" });
8-
data = JSON.parse(data);
9-
return data['productName'] ?? data['name'];
8+
let data = fs.readFileSync('./package.json', { encoding: 'utf-8' });
9+
data = JSON.parse(data);
10+
return data['productName'] ?? data['name'];
1011
}
1112

1213
export const config = {
13-
productName: app ? app.getName() : getProductNameFromPackageJson(),
14-
downloadsPath: app ? app.getPath('downloads') : process.cwd(),
15-
userDataPath: app ? app.getPath('userData') : process.cwd(),
16-
protocol: 'todo-my-app-name',
17-
isDev: isDev,
18-
}
14+
productName: app ? app.getName() : getProductNameFromPackageJson(),
15+
downloadsPath: app ? app.getPath('downloads') : process.cwd(),
16+
userDataPath: app ? app.getPath('userData') : process.cwd(),
17+
protocol: 'todo-my-app-name',
18+
isDev: isDev,
19+
};
1920

20-
global.ELECTRON_CONFIG = config
21+
global.ELECTRON_CONFIG = config;

src/main/menu.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { BrowserWindow, Menu, MenuItemConstructorOptions } from 'electron';
22
import { isDev } from 'botasaurus-server/env';
3-
import { buildMacTemplate, buildDefaultTemplate,MacMenuItemConstructorOptions } from './utils/menu-utils';
3+
import {
4+
buildMacTemplate,
5+
buildDefaultTemplate,
6+
MacMenuItemConstructorOptions,
7+
} from './utils/menu-utils';
48
import { config } from './config';
59
import { getSupportSubMenu } from './support-sub-menu';
610

@@ -22,11 +26,10 @@ export default class MenuBuilder {
2226
this.setupDevelopmentEnvironment();
2327
}
2428

25-
const isMac = process.platform === 'darwin'
26-
const template =
27-
isMac
28-
? buildMacTemplate(getIsDev(), config.productName, this.mainWindow)
29-
: buildDefaultTemplate(getIsDev(), this.mainWindow);
29+
const isMac = process.platform === 'darwin';
30+
const template = isMac
31+
? buildMacTemplate(getIsDev(), config.productName, this.mainWindow)
32+
: buildDefaultTemplate(getIsDev(), this.mainWindow);
3033

3134
const subMenuHelp: MacMenuItemConstructorOptions = {
3235
label: 'Support',
@@ -55,4 +58,4 @@ export default class MenuBuilder {
5558
]).popup({ window: this.mainWindow });
5659
});
5760
}
58-
}
61+
}

src/main/preload.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
// Disable no-unused-vars, broken for spread args
22
/* eslint no-unused-vars: off */
3-
import { contextBridge, ipcRenderer, IpcRendererEvent , webUtils} from 'electron';
4-
3+
import {
4+
contextBridge,
5+
ipcRenderer,
6+
IpcRendererEvent,
7+
webUtils,
8+
} from 'electron';
59

610
const electronHandler = {
11+
ipcRenderer: {
712
send(channel: string, value: any = null) {
8-
ipcRenderer.send(channel, value)
13+
ipcRenderer.send(channel, value);
914
},
1015
on(channel: string, callback: (...args: unknown[]) => void) {
1116
const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
12-
callback(...args)
13-
ipcRenderer.on(channel, subscription)
14-
17+
callback(...args);
18+
ipcRenderer.on(channel, subscription);
19+
1520
return () => {
16-
ipcRenderer.removeListener(channel, subscription)
17-
}
21+
ipcRenderer.removeListener(channel, subscription);
22+
};
1823
},
1924
off(channel: string, callback: (...args: unknown[]) => void) {
2025
ipcRenderer.off(channel, callback);
@@ -25,9 +30,10 @@ const electronHandler = {
2530
once(channel: string, func: (...args: unknown[]) => void) {
2631
ipcRenderer.once(channel, (_event, ...args) => func(...args));
2732
},
28-
getPathForFile: webUtils.getPathForFile,
33+
},
34+
getPathForFile: webUtils.getPathForFile,
2935
};
3036

31-
contextBridge.exposeInMainWorld('ipc', electronHandler);
37+
contextBridge.exposeInMainWorld('electron', electronHandler);
3238

3339
export type ElectronHandler = typeof electronHandler;

src/main/support-sub-menu.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { removeSync } from 'botasaurus/utils';
99
import { AppUpdater } from './utils/AppUpdater';
1010
import { getBotasaurusStorage } from 'botasaurus/botasaurus-storage';
11-
import { MainHandler } from './utils/main-handler';
11+
import { ipcMain } from './utils/ipc-main';
1212
import { Server } from 'botasaurus-server/server';
1313

1414
export function getSupportSubMenu(): MenuItemConstructorOptions[] {
@@ -58,8 +58,8 @@ export function getSupportSubMenu(): MenuItemConstructorOptions[] {
5858
{
5959
label: 'Factory Reset App and Clear All Data',
6060
click: () => {
61-
MainHandler.send('show-reset-prompt');
62-
MainHandler.once('perform-reset', performFactoryReset);
61+
ipcMain.send('show-reset-prompt');
62+
ipcMain.once('perform-reset', performFactoryReset);
6363
},
6464
},
6565
);

src/main/utils/AppUpdater.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { autoUpdater } from 'electron-updater';
2-
import { MainHandler } from './main-handler'
2+
import { ipcMain } from './ipc-main'
33
import { sleep } from 'botasaurus/utils';
44
import { getOs } from 'botasaurus/env';
55

@@ -46,25 +46,25 @@ class _AppUpdater {
4646
async listenEvents(){
4747

4848
// autoUpdater.on('checking-for-update', () => {
49-
// MainHandler.log('Checking for updates...');
49+
// ipcMain.log('Checking for updates...');
5050
// });
5151

5252
autoUpdater.on('update-available', (info) => {
53-
MainHandler.log('Update available:', info);
53+
ipcMain.log('Update available:', info);
5454
// Only Download the Latest Version
5555
autoUpdater.downloadUpdate();
5656
});
5757

5858
// autoUpdater.on('update-not-available', (info) => {
59-
// MainHandler.log('Update not available:', info);
59+
// ipcMain.log('Update not available:', info);
6060
// });
6161

6262
autoUpdater.on('error', (err) => {
63-
MainHandler.log('Error in auto-updater:', err);
63+
ipcMain.log('Error in auto-updater:', err);
6464
});
6565

6666
autoUpdater.on('update-downloaded', (info) => {
67-
MainHandler.log('Update downloaded:', info);
67+
ipcMain.log('Update downloaded:', info);
6868
});
6969
}
7070

@@ -76,7 +76,7 @@ class _AppUpdater {
7676
}
7777

7878
try {
79-
MainHandler.send('checkingForUpdates')
79+
ipcMain.send('checkingForUpdates')
8080
const startTime = Date.now();
8181
const result = await autoUpdater.checkForUpdates();
8282
const elapsedTime = Date.now() - startTime;
@@ -85,21 +85,21 @@ class _AppUpdater {
8585
await sleep(2)
8686
}
8787
if (canUpdate(result)) {
88-
MainHandler.send('downloadingUpdates')
88+
ipcMain.send('downloadingUpdates')
8989
await autoUpdater.downloadUpdate(); // Start downloading the update
90-
MainHandler.send('quittingToApplyUpdates')
90+
ipcMain.send('quittingToApplyUpdates')
9191
// wait for 10 seconds
9292
await sleep(10)
93-
MainHandler.send('hideAll')
93+
ipcMain.send('hideAll')
9494

9595
autoUpdater.quitAndInstall(false, true); // Quit and install the update
96-
MainHandler.log('Updated')
96+
ipcMain.log('Updated')
9797
} else {
98-
MainHandler.send('success', getCurrentVersion())
98+
ipcMain.send('success', getCurrentVersion())
9999
}
100100
} catch (error) {
101101
console.error(error)
102-
MainHandler.send('hideAll')
102+
ipcMain.send('hideAll')
103103
}
104104
}
105105
}

src/main/utils/main-handler.ts renamed to src/main/utils/ipc-main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IpcRendererEvent } from 'electron'
44
import { ipcMain, IpcMainInvokeEvent } from 'electron'
55
import { getWindow, } from './window'
66

7-
const MainHandler = {
7+
const _ipcMain = {
88
send: (channel: string, ...args: any[]) => {
99
const window = getWindow()
1010
window?.webContents.send(channel, ...args)
@@ -23,7 +23,7 @@ const MainHandler = {
2323
log: (...args: any[]) => {
2424
// @ts-ignore
2525
try {
26-
MainHandler.send("log", ...args)
26+
_ipcMain.send("log", ...args)
2727
} catch (error:any) {
2828
// can't console error as it is modified in set-up-error-forwarding-to-renderer.ts
2929
console.log(error?.message)
@@ -34,4 +34,4 @@ const MainHandler = {
3434

3535
}
3636

37-
export { MainHandler }
37+
export { _ipcMain as ipcMain }

src/main/utils/set-up-error-forwarding-to-renderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { MainHandler } from './main-handler';
1+
import { ipcMain } from './ipc-main';
22

33
const sendErrorToRenderer = (error) => {
44
const errorMessage = error instanceof Error ? error.message : String(error);
55
const errorStack =
66
error instanceof Error ? error.stack : 'No stack available';
77

8-
MainHandler.log({
8+
ipcMain.log({
99
error: errorMessage,
1010
stack: errorStack,
1111
timestamp: new Date().toISOString(),

src/main/utils/set-up-renderer-to-server-call.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { shell } from 'electron';
22
import run from 'botasaurus-server/run';
3-
import { MainHandler } from './main-handler';
3+
import { ipcMain } from './ipc-main';
44
import * as routes from 'botasaurus-server/task-routes';
55
import setUpErrorForwardingToRenderer from './set-up-error-forwarding-to-renderer';
66

@@ -14,18 +14,18 @@ export function setUpRendererToServerCall() {
1414
setUpErrorForwardingToRenderer();
1515

1616
for (const [key, value] of Object.entries(routes)) {
17-
MainHandler.handle(key, async (_, ...data) => {
17+
ipcMain.handle(key, async (_, ...data) => {
1818
// @ts-ignore
1919
const result = await value(...data);
2020
// writeTempJson(result)
2121
return result;
2222
});
2323
}
24-
MainHandler.handle('openInFolder', async (_, ...data) => {
24+
ipcMain.handle('openInFolder', async (_, ...data) => {
2525
// @ts-ignore
2626
return shell.showItemInFolder(...data);
2727
});
28-
MainHandler.handle('openExternal', async (_, ...data) => {
28+
ipcMain.handle('openExternal', async (_, ...data) => {
2929
// @ts-ignore
3030
return shell.openExternal(...data);
3131
});

src/renderer/app/components/FactoryResetModal.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { EuiModalHeader } from '@elastic/eui/optimize/es/components/modal/modal_
88
import { EuiModalHeaderTitle } from '@elastic/eui/optimize/es/components/modal/modal_header_title';
99
import { useEffect, useState } from 'react';
1010
import ClickOutside from './ClickOutside/ClickOutside';
11-
import { handler } from '../utils/handler';
11+
import { ipcRenderer } from '../utils/electron';
1212

1313
export default function FactoryResetModal() {
1414
const [isModalVisible, setIsModalVisible] = useState(false);
@@ -19,9 +19,9 @@ export default function FactoryResetModal() {
1919

2020
useEffect(() => {
2121
const showModal = () => setIsModalVisible(true);
22-
handler.on('show-reset-prompt', showModal);
22+
ipcRenderer.on('show-reset-prompt', showModal);
2323
return () => {
24-
handler.off('show-reset-prompt', showModal);
24+
ipcRenderer.off('show-reset-prompt', showModal);
2525
};
2626
}, []);
2727

@@ -47,7 +47,7 @@ export default function FactoryResetModal() {
4747
<EuiButtonEmpty onClick={closeModal}>Cancel</EuiButtonEmpty>
4848
<EuiButton
4949
onClick={() => {
50-
handler.send('perform-reset');
50+
ipcRenderer.send('perform-reset');
5151
closeModal();
5252
}}
5353
>

src/renderer/app/components/inputs/DragDropFileUploader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EuiFilePicker } from '@elastic/eui/optimize/es/components/form/file_picker/file_picker';
2-
import { handler } from '../../utils/handler'
2+
import { getPathForFile } from '../../utils/electron'
33

44
export default function DragDropFileUploader({ filePickerRef, onChange, multiple = true, acceptedFileTypes = [], ...props }) {
55
const handleFileChange = (files) => {
@@ -8,7 +8,7 @@ export default function DragDropFileUploader({ filePickerRef, onChange, multiple
88
const file = files[i];
99
const item = {
1010
name: file.name,
11-
path: handler.getPathForFile(file),
11+
path: getPathForFile(file),
1212
size: file.size,
1313
last_modified: file.lastModified
1414
}

0 commit comments

Comments
 (0)