@@ -7,7 +7,8 @@ const autoUpdater = electron.autoUpdater
77const jetpack = require ( 'fs-jetpack' )
88const path = require ( 'path' )
99const ChildProcess = require ( 'child_process' )
10- electron . crashReporter . start ( )
10+ const _ = require ( 'lodash' )
11+ // electron.crashReporter.start()
1112
1213var mainWindow = null
1314var finderProcess
@@ -61,6 +62,44 @@ autoUpdater
6162 }
6263 } )
6364
65+
66+ const nodeIpc = require ( 'node-ipc' )
67+ var isNodeIpcReady = false
68+ nodeIpc . config . id = 'node'
69+ nodeIpc . config . retry = 1500
70+ nodeIpc . config . silent = true
71+
72+ nodeIpc . serve (
73+ path . join ( app . getPath ( 'userData' ) , 'boost.service' ) ,
74+ function ( ) {
75+ isNodeIpcReady = true
76+ nodeIpc . server . on (
77+ 'message' ,
78+ function ( data , socket ) {
79+ console . log ( '>>' , data )
80+ format ( data )
81+ }
82+ )
83+ }
84+ )
85+
86+
87+ function format ( payload ) {
88+ switch ( payload . type ) {
89+ case 'show-main-window' :
90+ mainWindow . minimize ( )
91+ mainWindow . restore ( )
92+ break
93+ case 'copy-finder' :
94+ mainWindow . webContents . send ( 'copy-finder' )
95+ break
96+ case 'quit-app' :
97+ appQuit = true
98+ app . quit ( )
99+ break
100+ }
101+ }
102+
64103app . on ( 'ready' , function ( ) {
65104 app . on ( 'before-quit' , function ( ) {
66105 if ( finderProcess ) finderProcess . kill ( )
@@ -93,14 +132,26 @@ app.on('ready', function () {
93132 e . preventDefault ( )
94133 mainWindow . hide ( )
95134 } )
135+
136+ function emitToFinder ( type , data ) {
137+ if ( ! isNodeIpcReady ) {
138+ console . log ( 'server is not ready' )
139+ }
140+ var payload = {
141+ type : type ,
142+ data : data
143+ }
144+ nodeIpc . server . broadcast ( 'message' , payload )
145+ }
146+
96147 mainWindow . webContents . on ( 'did-finish-load' , function ( ) {
97148 if ( finderProcess == null ) {
149+ var finderArgv = [ path . resolve ( __dirname , 'finder.js' ) , '--finder' ]
150+ if ( _ . find ( process . argv , a => a === '--hot' ) ) finderArgv . push ( '--hot' )
98151 finderProcess = ChildProcess
99- . execFile ( process . execPath , [ path . resolve ( __dirname , 'finder.js' ) , '--finder' ] )
100- finderProcess . stdout . setEncoding ( 'utf8' )
101- finderProcess . stderr . setEncoding ( 'utf8' )
102- finderProcess . stdout . on ( 'data' , format )
103- finderProcess . stderr . on ( 'data' , errorFormat )
152+ . execFile ( process . execPath , finderArgv )
153+
154+ nodeIpc . server . start ( )
104155 }
105156
106157 if ( update != null ) {
@@ -115,48 +166,6 @@ app.on('ready', function () {
115166 mainWindow . show ( )
116167 } )
117168
118- function format ( payload ) {
119- // console.log('from finder >> ', payload)
120- try {
121- payload = JSON . parse ( payload )
122- } catch ( e ) {
123- console . log ( 'Not parsable payload : ' , payload )
124- return
125- }
126- switch ( payload . type ) {
127- case 'log' :
128- console . log ( 'FINDER(stdout): ' + payload . data )
129- break
130- case 'show-main-window' :
131- mainWindow . show ( )
132- break
133- case 'copy-finder' :
134- mainWindow . webContents . send ( 'copy-finder' )
135- break
136- case 'request-data' :
137- mainWindow . webContents . send ( 'request-data' )
138- break
139- case 'quit-app' :
140- appQuit = true
141- app . quit ( )
142- break
143- }
144- }
145- function errorFormat ( output ) {
146- console . error ( 'FINDER(stderr):' + output )
147- }
148-
149- function emitToFinder ( type , data ) {
150- if ( ! finderProcess ) {
151- console . log ( 'finder process is not ready' )
152- return
153- }
154- var payload = {
155- type : type ,
156- data : data
157- }
158- finderProcess . stdin . write ( JSON . stringify ( payload ) , 'utf-8' )
159- }
160169
161170 var userDataPath = app . getPath ( 'userData' )
162171 if ( ! jetpack . cwd ( userDataPath ) . exists ( 'keymap.json' ) ) {
0 commit comments