@@ -11,137 +11,179 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
12
12
return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
13
13
} ;
14
+ const electron_1 = require ( "electron" ) ;
14
15
const electron_updater_1 = require ( "electron-updater" ) ;
15
16
const state_1 = __importDefault ( require ( "./server/state" ) ) ;
16
17
const utils_1 = require ( "@electron-toolkit/utils" ) ;
17
18
const server_1 = require ( "./server" ) ;
18
19
const utils_2 = require ( "./server/utils" ) ;
19
- const electron_1 = require ( "electron" ) ;
20
20
const path_1 = require ( "path" ) ;
21
21
const 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
- } ;
39
22
class NativePHP {
23
+ constructor ( ) {
24
+ this . processes = [ ] ;
25
+ this . schedulerInterval = undefined ;
26
+ }
40
27
bootstrap ( app , icon , phpBinary , cert ) {
41
- require ( ' @electron/remote/main' ) . initialize ( ) ;
28
+ require ( " @electron/remote/main" ) . initialize ( ) ;
42
29
state_1 . default . icon = icon ;
43
30
state_1 . default . php = phpBinary ;
44
31
state_1 . default . caCert = cert ;
45
32
this . bootstrapApp ( app ) ;
46
33
this . addEventListeners ( app ) ;
47
- this . addTerminateListeners ( app ) ;
48
34
}
49
35
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 ] ,
54
40
} ) ;
55
41
} ) ;
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 ] ,
60
46
} ) ;
61
47
} ) ;
62
- app . on ( ' window-all-closed' , ( ) => {
63
- if ( process . platform !== ' darwin' ) {
48
+ app . on ( " window-all-closed" , ( ) => {
49
+ if ( process . platform !== " darwin" ) {
64
50
app . quit ( ) ;
65
51
}
66
52
} ) ;
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" ) ;
72
65
}
73
- killChildProcesses ( ) ;
66
+ event . preventDefault ( ) ;
74
67
} ) ;
75
68
}
76
69
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 = { } ;
79
89
try {
80
- nativePHPConfig = JSON . parse ( result . stdout ) ;
90
+ const result = yield ( 0 , server_1 . retrieveNativePHPConfig ) ( ) ;
91
+ config = JSON . parse ( result . stdout ) ;
81
92
}
82
- catch ( e ) {
83
- console . error ( e ) ;
93
+ catch ( error ) {
94
+ console . error ( error ) ;
84
95
}
85
- } ) . catch ( ( err ) => {
86
- console . error ( err ) ;
87
- } ) . finally ( ( ) => {
88
- this . setupApp ( nativePHPConfig ) ;
96
+ return config ;
89
97
} ) ;
90
98
}
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
+ }
96
117
}
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 = { } ;
101
139
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 ) ;
104
142
}
105
- catch ( e ) {
106
- console . error ( e ) ;
143
+ catch ( error ) {
144
+ console . error ( error ) ;
107
145
}
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 ) ;
119
182
}
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 ) ;
127
185
}
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
+ } ) ;
145
187
}
146
188
}
147
189
module . exports = new NativePHP ( ) ;
0 commit comments