@@ -11,137 +11,179 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
1111var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
1212 return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
1313} ;
14+ const electron_1 = require ( "electron" ) ;
1415const electron_updater_1 = require ( "electron-updater" ) ;
1516const state_1 = __importDefault ( require ( "./server/state" ) ) ;
1617const utils_1 = require ( "@electron-toolkit/utils" ) ;
1718const server_1 = require ( "./server" ) ;
1819const utils_2 = require ( "./server/utils" ) ;
19- const electron_1 = require ( "electron" ) ;
2020const path_1 = require ( "path" ) ;
2121const ps_node_1 = __importDefault ( require ( "ps-node" ) ) ;
22- let phpProcesses = [ ] ;
23- let websocketProcess ;
24- let schedulerInterval ;
25- const killChildProcesses = ( ) => {
26- let processes = [
27- ...phpProcesses ,
28- websocketProcess ,
29- ] . filter ( ( p ) => p !== undefined ) ;
30- processes . forEach ( ( process ) => {
31- try {
32- ps_node_1 . default . kill ( process . pid ) ;
33- }
34- catch ( err ) {
35- console . error ( err ) ;
36- }
37- } ) ;
38- } ;
3922class NativePHP {
23+ constructor ( ) {
24+ this . processes = [ ] ;
25+ this . schedulerInterval = undefined ;
26+ }
4027 bootstrap ( app , icon , phpBinary , cert ) {
41- require ( ' @electron/remote/main' ) . initialize ( ) ;
28+ require ( " @electron/remote/main" ) . initialize ( ) ;
4229 state_1 . default . icon = icon ;
4330 state_1 . default . php = phpBinary ;
4431 state_1 . default . caCert = cert ;
4532 this . bootstrapApp ( app ) ;
4633 this . addEventListeners ( app ) ;
47- this . addTerminateListeners ( app ) ;
4834 }
4935 addEventListeners ( app ) {
50- app . on ( ' open-url' , ( event , url ) => {
51- ( 0 , utils_2 . notifyLaravel ) ( ' events' , {
52- event : ' \\Native\\Laravel\\Events\\App\\OpenedFromURL' ,
53- payload : [ url ]
36+ app . on ( " open-url" , ( event , url ) => {
37+ ( 0 , utils_2 . notifyLaravel ) ( " events" , {
38+ event : " \\Native\\Laravel\\Events\\App\\OpenedFromURL" ,
39+ payload : [ url ] ,
5440 } ) ;
5541 } ) ;
56- app . on ( ' open-file' , ( event , path ) => {
57- ( 0 , utils_2 . notifyLaravel ) ( ' events' , {
58- event : ' \\Native\\Laravel\\Events\\App\\OpenFile' ,
59- payload : [ path ]
42+ app . on ( " open-file" , ( event , path ) => {
43+ ( 0 , utils_2 . notifyLaravel ) ( " events" , {
44+ event : " \\Native\\Laravel\\Events\\App\\OpenFile" ,
45+ payload : [ path ] ,
6046 } ) ;
6147 } ) ;
62- app . on ( ' window-all-closed' , ( ) => {
63- if ( process . platform !== ' darwin' ) {
48+ app . on ( " window-all-closed" , ( ) => {
49+ if ( process . platform !== " darwin" ) {
6450 app . quit ( ) ;
6551 }
6652 } ) ;
67- }
68- addTerminateListeners ( app ) {
69- app . on ( 'before-quit' , ( e ) => {
70- if ( schedulerInterval ) {
71- clearInterval ( schedulerInterval ) ;
53+ app . on ( "before-quit" , ( ) => {
54+ if ( this . schedulerInterval ) {
55+ clearInterval ( this . schedulerInterval ) ;
56+ }
57+ this . killChildProcesses ( ) ;
58+ } ) ;
59+ app . on ( "browser-window-created" , ( _ , window ) => {
60+ utils_1 . optimizer . watchWindowShortcuts ( window ) ;
61+ } ) ;
62+ app . on ( "activate" , function ( event , hasVisibleWindows ) {
63+ if ( ! hasVisibleWindows ) {
64+ ( 0 , utils_2 . notifyLaravel ) ( "booted" ) ;
7265 }
73- killChildProcesses ( ) ;
66+ event . preventDefault ( ) ;
7467 } ) ;
7568 }
7669 bootstrapApp ( app ) {
77- let nativePHPConfig = { } ;
78- ( 0 , server_1 . retrieveNativePHPConfig ) ( ) . then ( ( result ) => {
70+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
71+ yield app . whenReady ( ) ;
72+ const config = yield this . loadConfig ( ) ;
73+ this . setDockIcon ( ) ;
74+ this . setAppUserModelId ( config ) ;
75+ this . setDeepLinkHandler ( config ) ;
76+ this . startAutoUpdater ( config ) ;
77+ yield this . startElectronApi ( ) ;
78+ state_1 . default . phpIni = yield this . loadPhpIni ( ) ;
79+ yield this . startPhpApp ( ) ;
80+ yield this . startQueueWorker ( ) ;
81+ yield this . startWebsockets ( ) ;
82+ this . startScheduler ( ) ;
83+ yield ( 0 , utils_2 . notifyLaravel ) ( "booted" ) ;
84+ } ) ;
85+ }
86+ loadConfig ( ) {
87+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
88+ let config = { } ;
7989 try {
80- nativePHPConfig = JSON . parse ( result . stdout ) ;
90+ const result = yield ( 0 , server_1 . retrieveNativePHPConfig ) ( ) ;
91+ config = JSON . parse ( result . stdout ) ;
8192 }
82- catch ( e ) {
83- console . error ( e ) ;
93+ catch ( error ) {
94+ console . error ( error ) ;
8495 }
85- } ) . catch ( ( err ) => {
86- console . error ( err ) ;
87- } ) . finally ( ( ) => {
88- this . setupApp ( nativePHPConfig ) ;
96+ return config ;
8997 } ) ;
9098 }
91- setupApp ( nativePHPConfig ) {
92- electron_1 . app . whenReady ( ) . then ( ( ) => __awaiter ( this , void 0 , void 0 , function * ( ) {
93- var _a ;
94- if ( process . platform === 'darwin' && process . env . NODE_ENV === 'development' ) {
95- electron_1 . app . dock . setIcon ( state_1 . default . icon ) ;
99+ setDockIcon ( ) {
100+ if ( process . platform === "darwin" &&
101+ process . env . NODE_ENV === "development" ) {
102+ electron_1 . app . dock . setIcon ( state_1 . default . icon ) ;
103+ }
104+ }
105+ setAppUserModelId ( config ) {
106+ utils_1 . electronApp . setAppUserModelId ( config === null || config === void 0 ? void 0 : config . app_id ) ;
107+ }
108+ setDeepLinkHandler ( config ) {
109+ const deepLinkProtocol = config === null || config === void 0 ? void 0 : config . deeplink_scheme ;
110+ if ( deepLinkProtocol ) {
111+ if ( process . defaultApp ) {
112+ if ( process . argv . length >= 2 ) {
113+ electron_1 . app . setAsDefaultProtocolClient ( deepLinkProtocol , process . execPath , [
114+ ( 0 , path_1 . resolve ) ( process . argv [ 1 ] ) ,
115+ ] ) ;
116+ }
96117 }
97- electron_1 . app . on ( 'browser-window-created' , ( _ , window ) => {
98- utils_1 . optimizer . watchWindowShortcuts ( window ) ;
99- } ) ;
100- let phpIniSettings = { } ;
118+ else {
119+ electron_1 . app . setAsDefaultProtocolClient ( deepLinkProtocol ) ;
120+ }
121+ }
122+ }
123+ startAutoUpdater ( config ) {
124+ var _a ;
125+ if ( ( ( _a = config === null || config === void 0 ? void 0 : config . updater ) === null || _a === void 0 ? void 0 : _a . enabled ) === true ) {
126+ electron_updater_1 . autoUpdater . checkForUpdatesAndNotify ( ) ;
127+ }
128+ }
129+ startElectronApi ( ) {
130+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
131+ const electronApi = yield ( 0 , server_1 . startAPI ) ( ) ;
132+ state_1 . default . electronApiPort = electronApi . port ;
133+ console . log ( "Electron API server started on port" , electronApi . port ) ;
134+ } ) ;
135+ }
136+ loadPhpIni ( ) {
137+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
138+ let config = { } ;
101139 try {
102- let { stdout } = yield ( 0 , server_1 . retrievePhpIniSettings ) ( ) ;
103- phpIniSettings = JSON . parse ( stdout ) ;
140+ const result = yield ( 0 , server_1 . retrievePhpIniSettings ) ( ) ;
141+ config = JSON . parse ( result . stdout ) ;
104142 }
105- catch ( e ) {
106- console . error ( e ) ;
143+ catch ( error ) {
144+ console . error ( error ) ;
107145 }
108- utils_1 . electronApp . setAppUserModelId ( nativePHPConfig === null || nativePHPConfig === void 0 ? void 0 : nativePHPConfig . app_id ) ;
109- const deepLinkProtocol = nativePHPConfig === null || nativePHPConfig === void 0 ? void 0 : nativePHPConfig . deeplink_scheme ;
110- if ( deepLinkProtocol ) {
111- if ( process . defaultApp ) {
112- if ( process . argv . length >= 2 ) {
113- electron_1 . app . setAsDefaultProtocolClient ( deepLinkProtocol , process . execPath , [ ( 0 , path_1 . resolve ) ( process . argv [ 1 ] ) ] ) ;
114- }
115- }
116- else {
117- electron_1 . app . setAsDefaultProtocolClient ( deepLinkProtocol ) ;
118- }
146+ return config ;
147+ } ) ;
148+ }
149+ startPhpApp ( ) {
150+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
151+ this . processes . push ( yield ( 0 , server_1 . startPhpApp ) ( ) ) ;
152+ } ) ;
153+ }
154+ startQueueWorker ( ) {
155+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
156+ this . processes . push ( yield ( 0 , server_1 . startQueue ) ( ) ) ;
157+ } ) ;
158+ }
159+ startWebsockets ( ) {
160+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
161+ this . processes . push ( yield ( 0 , server_1 . startWebsockets ) ( ) ) ;
162+ } ) ;
163+ }
164+ startScheduler ( ) {
165+ const now = new Date ( ) ;
166+ const delay = ( 60 - now . getSeconds ( ) ) * 1000 + ( 1000 - now . getMilliseconds ( ) ) ;
167+ setTimeout ( ( ) => {
168+ console . log ( "Running scheduler..." ) ;
169+ ( 0 , server_1 . runScheduler ) ( ) ;
170+ this . schedulerInterval = setInterval ( ( ) => {
171+ console . log ( "Running scheduler..." ) ;
172+ ( 0 , server_1 . runScheduler ) ( ) ;
173+ } , 60 * 1000 ) ;
174+ } , delay ) ;
175+ }
176+ killChildProcesses ( ) {
177+ this . processes
178+ . filter ( ( p ) => p !== undefined )
179+ . forEach ( ( process ) => {
180+ try {
181+ ps_node_1 . default . kill ( process . pid ) ;
119182 }
120- const apiPort = yield ( 0 , server_1 . startAPI ) ( ) ;
121- console . log ( 'API server started on port' , apiPort . port ) ;
122- phpProcesses = yield ( 0 , server_1 . servePhpApp ) ( apiPort . port , phpIniSettings ) ;
123- websocketProcess = ( 0 , server_1 . serveWebsockets ) ( ) ;
124- yield ( 0 , utils_2 . notifyLaravel ) ( 'booted' ) ;
125- if ( ( ( _a = nativePHPConfig === null || nativePHPConfig === void 0 ? void 0 : nativePHPConfig . updater ) === null || _a === void 0 ? void 0 : _a . enabled ) === true ) {
126- electron_updater_1 . autoUpdater . checkForUpdatesAndNotify ( ) ;
183+ catch ( err ) {
184+ console . error ( err ) ;
127185 }
128- let now = new Date ( ) ;
129- let delay = ( 60 - now . getSeconds ( ) ) * 1000 + ( 1000 - now . getMilliseconds ( ) ) ;
130- setTimeout ( ( ) => {
131- console . log ( "Running scheduler..." ) ;
132- ( 0 , server_1 . runScheduler ) ( apiPort . port , phpIniSettings ) ;
133- schedulerInterval = setInterval ( ( ) => {
134- console . log ( "Running scheduler..." ) ;
135- ( 0 , server_1 . runScheduler ) ( apiPort . port , phpIniSettings ) ;
136- } , 60 * 1000 ) ;
137- } , delay ) ;
138- electron_1 . app . on ( 'activate' , function ( event , hasVisibleWindows ) {
139- if ( ! hasVisibleWindows ) {
140- ( 0 , utils_2 . notifyLaravel ) ( 'booted' ) ;
141- }
142- event . preventDefault ( ) ;
143- } ) ;
144- } ) ) ;
186+ } ) ;
145187 }
146188}
147189module . exports = new NativePHP ( ) ;
0 commit comments