@@ -2,12 +2,16 @@ import { app, BrowserWindow, Menu, nativeImage, Tray } from 'electron';
22
33import { join } from 'path' ;
44
5- import { productName , protocols } from '../electron-builder.json ' ;
5+ import { globImport } from './utils/import ' ;
66
7- export type MyAppType = InstanceType < typeof MyApp > ;
8- export type ModuleFunction = ( app : MyAppType ) => void | Promise < void > ;
7+ export type AppContextType = InstanceType < typeof AppContext > ;
8+ export type ModuleFunction = ( context : AppContextType ) => void | Promise < void > ;
99
10- class MyApp {
10+ const { productName, protocols } = require ( app . isPackaged
11+ ? './app.json'
12+ : '../electron-builder.json' ) ;
13+
14+ class AppContext {
1115 // deep link protocol
1216 readonly PROTOCOL = protocols . name ;
1317
@@ -18,7 +22,7 @@ class MyApp {
1822 readonly DEV_URL = `http://localhost:3000/#` ;
1923
2024 // production mode - load file
21- readonly PROD_LOAD_FILE_PATH = join ( __dirname , '../index.html' ) ;
25+ readonly PROD_LOAD_FILE_PATH = join ( __dirname , '../dist/ index.html' ) ;
2226 readonly PROD_LOAD_FILE_HASH = '#' ;
2327
2428 // resources directory
@@ -37,6 +41,7 @@ class MyApp {
3741 async bootstrap ( ) {
3842 await this . initliazeElectron ( ) ;
3943 await this . autoload ( ) ;
44+ await this . createWindow ( ) ;
4045 }
4146
4247 async initliazeElectron ( ) {
@@ -58,7 +63,6 @@ class MyApp {
5863 } ) ;
5964
6065 await app . whenReady ( ) ;
61- await this . createWindow ( ) ;
6266 await this . createTray ( ) ;
6367 }
6468
@@ -88,11 +92,9 @@ class MyApp {
8892 this . window . loadFile ( this . PROD_LOAD_FILE_PATH , {
8993 hash : this . PROD_LOAD_FILE_HASH ,
9094 } ) ;
91-
92- this . window . webContents . openDevTools ( ) ; // FIXME: Remove this line
9395 } else {
94- this . window . loadURL ( this . DEV_URL ) ;
95- this . window . webContents . openDevTools ( ) ; // FIXME: Remove this line
96+ await this . window . loadURL ( this . DEV_URL ) ;
97+ this . window . webContents . openDevTools ( ) ;
9698 }
9799
98100 this . window . on ( 'ready-to-show' , ( ) => {
@@ -119,14 +121,10 @@ class MyApp {
119121 }
120122
121123 async autoload ( ) {
122- const modules = import . meta. glob < { default : ModuleFunction } > ( './modules/**/index.ts' , {
123- eager : true ,
124- } ) ;
124+ const modules = await globImport ( './modules/**/index.js' , { cwd : __dirname } ) ;
125125
126- for ( const module of Object . values ( modules ) ) {
127- await this . register ( module . default ) ;
128- }
126+ await Promise . all ( modules . map ( ( { default : module } ) => this . register ( module ) ) ) ;
129127 }
130128}
131129
132- export default MyApp ;
130+ export default AppContext ;
0 commit comments