OSDN Git Service

update config icon.
[bytom/bytom-electron.git] / main.js
1 require('babel-register')
2 require('events').EventEmitter.defaultMaxListeners = 100
3 const {app, BrowserWindow, ipcMain, shell} = require('electron')
4 const spawn = require('child_process').spawn
5 const glob = require('glob')
6 const url = require('url')
7 const path = require('path')
8 const fs = require('fs')
9 const logger = require('./modules/logger')
10 const log = logger.create('main')
11 const bytomdLog = logger.create('bytomd')
12 const Settings = require('./modules/settings')
13
14 const net = require('net')
15
16 let win, bytomdInit, bytomdNode
17
18 global.fileExist = false
19 global.mining = {isMining: false}
20 let startnode = false
21
22 Settings.init()
23
24 function initialize () {
25
26   function createWindow() {
27     // Create browser Window
28
29     const icon_path = path.join(__dirname, '/static/images/app-icon/png/app.png')
30     win = new BrowserWindow({
31       width: 1024 + 208,
32       height: 768,
33       'webPreferences': {
34         'webSecurity': !process.env.DEV_URL,
35         'preload': path.join(__dirname, '/modules/preload.js')
36       },
37       icon: icon_path
38     })
39
40     const startUrl = process.env.DEV_URL ||
41       url.format({
42         pathname: path.join(__dirname, '/public/index.html'),
43         protocol: 'file:',
44         slashes: true
45       })
46     win.loadURL(startUrl)
47
48     if(process.env.DEV){
49       win.webContents.openDevTools()
50     }
51
52     win.webContents.on('new-window', function(e, url) {
53       e.preventDefault()
54       shell.openExternal(url)
55     })
56
57     win.webContents.on('did-finish-load', function () {
58       if(startnode){
59         win.webContents.send('ConfiguredNetwork', 'startNode')
60       }
61     })
62
63     win.on('closed', () => {
64       win = null
65       app.quit()
66     })
67   }
68
69   app.on('ready', () => {
70
71     loadMenu()
72
73     setupConfigure()
74
75     bytomd()
76
77     createWindow()
78   })
79
80 //All window Closed
81   app.on('window-all-closed', () => {
82     if (process.platform !== 'darwin') {
83       app.quit()
84     }
85   })
86
87   app.on('activate', () => {
88     if (win === null) {
89       createWindow()
90     }
91   })
92
93   app.on('before-quit', () => {
94     if(bytomdInit){
95       bytomdInit.kill('SIGINT')
96       log.info('Kill bytomd Init command...')
97     }
98     if(bytomdNode){
99       bytomdNode.kill('SIGINT')
100       const killTimeout = setTimeout(() => {
101         bytomdNode.kill('SIGKILL')
102       }, 8000 /* 8 seconds */)
103
104       bytomdNode.once('close', () => {
105         clearTimeout(killTimeout)
106         bytomdNode = null
107       })
108
109       log.info('Kill bytomd Mining command...')
110     }
111   })
112 }
113
114 function setBytomNode() {
115   bytomdNode = spawn( `${Settings.bytomdPath}`, ['node', '--web.closed'] )
116
117   function checkPort () {
118     setTimeout(function () {
119       portInUse(9888, function(returnValue) {
120         if (!returnValue) {
121           checkPort()
122         }else{
123           startnode = true
124           win.webContents.send('ConfiguredNetwork', 'startNode')
125         }
126       })
127     }, 500)
128   }
129
130   checkPort()
131
132   bytomdNode.stdout.on('data', function(data) {
133     bytomdLog.info(`bytomd node: ${data}`)
134   })
135
136   bytomdNode.stderr.on('data', function(data) {
137     bytomdLog.info(`bytomd node: ${data}`)
138   })
139
140   bytomdNode.on('exit', function (code) {
141     bytomdLog.info('bytom Node exited with code ' + code)
142     app.quit()
143   })
144 }
145
146 function setBytomInit(event, bytomNetwork) {
147   // Init bytomd
148   bytomdInit = spawn(`${Settings.bytomdPath}`, ['init', '--chain_id',  `${bytomNetwork}`] )
149
150   bytomdInit.stdout.on('data', function(data) {
151     bytomdLog.info(`bytomd init: ${data}`)
152   })
153
154   bytomdInit.stderr.on('data', function(data) {
155     bytomdLog.info(`bytomd init: ${data}`)
156   })
157
158   bytomdInit.on('exit', function (code) {
159     event.sender.send('ConfiguredNetwork','init')
160     setBytomNode()
161     bytomdLog.info('bytom init exited with code ' + code)
162   })
163
164   bytomdInit.once('close', () => {
165     bytomdInit = null
166   })
167 }
168
169 function bytomd(){
170   const filePath = path.join(`${Settings.bytomdDataPath}/config.toml`)
171   if (fs.existsSync(filePath)) {
172     log.info('Bytomd Network has been inited')
173     global.fileExist = true
174     setBytomNode()
175   }else {
176     log.info('Init Bytomd Network...')
177     ipcMain.on('bytomdInitNetwork', (event, arg) => {
178       setBytomInit( event,  arg )
179     })
180   }
181 }
182
183 // Require each JS file in the main-process dir
184 function loadMenu () {
185   const files = glob.sync(path.join(__dirname, 'modules/menus/*.js'))
186   files.forEach((file) => { require(file) })
187 }
188
189 function setupConfigure(){
190   const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
191   const loggerOptions = Object.assign(logFolder)
192   logger.setup(loggerOptions)
193 }
194
195 function portInUse(port, callback) {
196   const server = net.createServer(function(socket) {
197     socket.write('Echo server\r\n')
198     socket.pipe(socket)
199   })
200
201   server.listen(port, '0.0.0.0')
202   server.on('error', function (e) {
203     callback(true)
204   })
205   server.on('listening', function (e) {
206     server.close()
207     callback(false)
208   })
209 }
210
211 // Handle Squirrel on Windows startup events
212 switch (process.argv[1]) {
213   case '--squirrel-install':
214   case '--squirrel-uninstall':
215   case '--squirrel-obsolete':
216   case '--squirrel-updated':
217     app.quit()
218     break
219   default:
220     initialize()
221 }
222