-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor main package (#1042)
- Loading branch information
Showing
23 changed files
with
418 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
|
||
|
||
|
||
node_modules | ||
.DS_Store | ||
dist | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type {ModuleContext} from './ModuleContext.js'; | ||
|
||
export interface AppModule { | ||
enable(context: ModuleContext): Promise<void>|void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ModuleContext = { | ||
readonly app: Electron.App; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {AppModule} from './AppModule.js'; | ||
import {ModuleContext} from './ModuleContext.js'; | ||
import {app} from 'electron'; | ||
|
||
class ModuleRunner implements PromiseLike<void> { | ||
#promise: Promise<void>; | ||
|
||
constructor() { | ||
this.#promise = Promise.resolve(); | ||
} | ||
|
||
then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2> { | ||
return this.#promise.then(onfulfilled, onrejected); | ||
} | ||
|
||
init(module: AppModule) { | ||
const p = module.enable(this.#createModuleContext()); | ||
|
||
if (p instanceof Promise) { | ||
this.#promise = this.#promise.then(() => p); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
#createModuleContext(): ModuleContext { | ||
return { | ||
app, | ||
}; | ||
} | ||
} | ||
|
||
export function createModuleRunner() { | ||
return new ModuleRunner(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,46 @@ | ||
import {app} from 'electron'; | ||
import './security-restrictions'; | ||
import {platform} from 'node:process'; | ||
import type {AppInitConfig} from './AppInitConfig.js'; | ||
import {createWindowManager} from './createWindowManager.js'; | ||
import {runAutoUpdater} from './auto-updater.js'; | ||
|
||
// Used in packages/entry-point.js | ||
export function initApp(initConfig: AppInitConfig) { | ||
const {restoreOrCreateWindow} = createWindowManager({ | ||
preload: initConfig.preload, | ||
renderer: initConfig.renderer, | ||
}); | ||
|
||
/** | ||
* Prevent electron from running multiple instances. | ||
*/ | ||
const isSingleInstance = app.requestSingleInstanceLock(); | ||
if (!isSingleInstance) { | ||
app.quit(); | ||
process.exit(0); | ||
} | ||
app.on('second-instance', restoreOrCreateWindow); | ||
|
||
/** | ||
* Disable Hardware Acceleration to save more system resources. | ||
*/ | ||
app.disableHardwareAcceleration(); | ||
|
||
/** | ||
* Shout down background process if all windows was closed | ||
*/ | ||
app.on('window-all-closed', () => { | ||
if (platform !== 'darwin') { | ||
app.quit(); | ||
} | ||
}); | ||
|
||
/** | ||
* @see https://www.electronjs.org/docs/latest/api/app#event-activate-macos Event: 'activate'. | ||
*/ | ||
app.on('activate', restoreOrCreateWindow); | ||
|
||
/** | ||
* Create the application window when the background process is ready. | ||
*/ | ||
app | ||
.whenReady() | ||
.then(restoreOrCreateWindow) | ||
.catch(e => console.error('Failed create window:', e)); | ||
|
||
/** | ||
* Install any extension in development mode only. | ||
* Note: You must install `electron-devtools-installer` manually | ||
*/ | ||
// if (import.meta.env.DEV) { | ||
// app | ||
// .whenReady() | ||
// .then(() => import('electron-devtools-installer')) | ||
// .then(module => { | ||
// const {default: installExtension, VUEJS_DEVTOOLS} = | ||
// //@ts-expect-error Hotfix for https://github.com/cawa-93/vite-electron-builder/issues/915 | ||
// typeof module.default === 'function' ? module : (module.default as typeof module); | ||
// | ||
// return installExtension(VUEJS_DEVTOOLS, { | ||
// loadExtensionOptions: { | ||
// allowFileAccess: true, | ||
// }, | ||
// }); | ||
// }) | ||
// .catch(e => console.error('Failed install extension:', e)); | ||
// } | ||
|
||
runAutoUpdater(); | ||
import {createModuleRunner} from './ModuleRunner.js'; | ||
import {disallowMultipleAppInstance} from './modules/SingleInstanceApp.js'; | ||
import {createWindowManagerModule} from './modules/WindowManager.js'; | ||
import {terminateAppOnLastWindowClose} from './modules/ApplicationTerminatorOnLastWindowClose.js'; | ||
import {hardwareAccelerationMode} from './modules/HardwareAccelerationModule.js'; | ||
import {autoUpdater} from './modules/AutoUpdater.js'; | ||
import {allowInternalOrigins} from './modules/BlockNotAllowdOrigins.js'; | ||
import {allowExternalUrls} from './modules/ExternalUrls.js'; | ||
|
||
|
||
export async function initApp(initConfig: AppInitConfig) { | ||
const moduleRunner = createModuleRunner() | ||
.init(createWindowManagerModule({initConfig, openDevTools: import.meta.env.DEV})) | ||
.init(disallowMultipleAppInstance()) | ||
.init(terminateAppOnLastWindowClose()) | ||
.init(hardwareAccelerationMode({enable: false})) | ||
.init(autoUpdater()) | ||
|
||
// Install DevTools extension if needed | ||
// .init(chromeDevToolsExtension({extension: 'VUEJS3_DEVTOOLS'})) | ||
|
||
// Security | ||
.init(allowInternalOrigins( | ||
new Set(initConfig.renderer instanceof URL ? [initConfig.renderer.origin] : []), | ||
)) | ||
.init(allowExternalUrls( | ||
new Set( | ||
initConfig.renderer instanceof URL | ||
? [ | ||
'https://vite.dev', | ||
'https://developer.mozilla.org', | ||
'https://solidjs.com', | ||
'https://qwik.dev', | ||
'https://lit.dev', | ||
'https://react.dev', | ||
'https://preactjs.com', | ||
'https://www.typescriptlang.org', | ||
'https://vuejs.org', | ||
] | ||
: [], | ||
)), | ||
); | ||
|
||
await moduleRunner; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {AppModule} from '../AppModule.js'; | ||
import {ModuleContext} from '../ModuleContext.js'; | ||
|
||
export abstract class AbstractSecurityRule implements AppModule { | ||
enable({app}: ModuleContext): Promise<void> | void { | ||
app.on('web-contents-created', (_, contents) => this.applyRule(contents)) | ||
} | ||
|
||
abstract applyRule(contents: Electron.WebContents): Promise<void> | void; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/main/src/modules/ApplicationTerminatorOnLastWindowClose.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {AppModule} from '../AppModule.js'; | ||
import {ModuleContext} from '../ModuleContext.js'; | ||
|
||
class ApplicationTerminatorOnLastWindowClose implements AppModule { | ||
enable({app}: ModuleContext): Promise<void> | void { | ||
app.on('window-all-closed', () => app.quit()); | ||
} | ||
} | ||
|
||
|
||
export function terminateAppOnLastWindowClose(...args: ConstructorParameters<typeof ApplicationTerminatorOnLastWindowClose>) { | ||
return new ApplicationTerminatorOnLastWindowClose(...args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {AppModule} from '../AppModule.js'; | ||
import electronUpdater, {type AppUpdater, type Logger} from 'electron-updater'; | ||
|
||
type DownloadNotification = Parameters<AppUpdater['checkForUpdatesAndNotify']>[0]; | ||
|
||
export class AutoUpdater implements AppModule { | ||
|
||
readonly #logger: Logger | null; | ||
readonly #notification: DownloadNotification; | ||
|
||
constructor( | ||
{ | ||
logger = null, | ||
downloadNotification = undefined, | ||
}: | ||
{ | ||
logger?: Logger | null | undefined, | ||
downloadNotification?: DownloadNotification | ||
} = {}, | ||
) { | ||
this.#logger = logger; | ||
this.#notification = downloadNotification; | ||
} | ||
|
||
async enable(): Promise<void> { | ||
await this.runAutoUpdater(); | ||
} | ||
|
||
getAutoUpdater(): AppUpdater { | ||
// Using destructuring to access autoUpdater due to the CommonJS module of 'electron-updater'. | ||
// It is a workaround for ESM compatibility issues, see https://github.com/electron-userland/electron-builder/issues/7976. | ||
const {autoUpdater} = electronUpdater; | ||
return autoUpdater; | ||
} | ||
|
||
async runAutoUpdater() { | ||
const updater = this.getAutoUpdater(); | ||
try { | ||
updater.logger = this.#logger || null; | ||
updater.fullChangelog = true; | ||
|
||
return await updater.checkForUpdatesAndNotify(this.#notification); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
if (error.message.includes('No published versions')) { | ||
return null; | ||
} | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
} | ||
|
||
|
||
export function autoUpdater(...args: ConstructorParameters<typeof AutoUpdater>) { | ||
return new AutoUpdater(...args); | ||
} |
Oops, something went wrong.