OSDN Git Service

update the vapor run
[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 vapordLog = logger.create('vapord')
12 const Settings = require('./modules/settings')
13
14 const net = require('net')
15
16 let win, vapordInit, vapordNode
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     vapord()
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(vapordInit){
95       vapordInit.kill('SIGINT')
96       log.info('Kill vapord Init command...')
97     }
98     if(vapordNode){
99       vapordNode.kill('SIGINT')
100       const killTimeout = setTimeout(() => {
101         vapordNode.kill('SIGKILL')
102       }, 8000 /* 8 seconds */)
103
104       vapordNode.once('close', () => {
105         clearTimeout(killTimeout)
106         vapordNode = null
107       })
108
109       log.info('Kill vapord Mining command...')
110     }
111   })
112 }
113
114 function setVaporNode(event) {
115   vapordNode = spawn( `${Settings.vapordPath}`, ['node', '--web.closed'] )
116
117   vapordNode.stdout.on('data', function(data) {
118     vapordLog.info(`vapord node: ${data}`)
119   })
120
121   vapordNode.stderr.on('data', function(data) {
122     vapordLog.info(`vapord node: ${data}`)
123     if (data.includes('msg="start node')) {
124       if (event) {
125         event.sender.send('ConfiguredNetwork', 'startNode')
126       }
127       else {
128         startnode = true
129         win.webContents.send('ConfiguredNetwork', 'startNode')
130       }
131     }
132
133     vapordNode.on('exit', function (code) {
134       vapordLog.info('vapor Node exited with code ' + code)
135       app.quit()
136     })
137   })
138 }
139
140 function setVaporInit(event, vaporNetwork) {
141   // Init vapord
142   vapordInit = spawn(`${Settings.vapordPath}`, ['init', '--chain_id',  `${vaporNetwork}`] )
143
144   vapordInit.stdout.on('data', function(data) {
145     vapordLog.info(`vapord init: ${data}`)
146   })
147
148   vapordInit.stderr.on('data', function(data) {
149     vapordLog.info(`vapord init: ${data}`)
150   })
151
152   vapordInit.on('exit', function (code) {
153     event.sender.send('ConfiguredNetwork','init')
154     setVaporNode(event)
155     vapordLog.info('vapor init exited with code ' + code)
156   })
157
158   vapordInit.once('close', () => {
159     vapordInit = null
160   })
161 }
162
163 function vapord(){
164   const filePath = path.join(`${Settings.vapordDataPath}/config.toml`)
165   if (fs.existsSync(filePath)) {
166     log.info('Vapord Network has been inited')
167     global.fileExist = true
168     setVaporNode()
169   }else {
170     log.info('Init Vapord Network...')
171     ipcMain.on('vapordInitNetwork', (event, arg) => {
172       setVaporInit( event,  arg )
173     })
174   }
175 }
176
177 // Require each JS file in the main-process dir
178 function loadMenu () {
179   const files = glob.sync(path.join(__dirname, 'modules/menus/*.js'))
180   files.forEach((file) => { require(file) })
181 }
182
183 function setupConfigure(){
184   const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
185   const loggerOptions = Object.assign(logFolder)
186   logger.setup(loggerOptions)
187 }
188
189
190 // Handle Squirrel on Windows startup events
191 switch (process.argv[1]) {
192   case '--squirrel-install':
193   case '--squirrel-uninstall':
194   case '--squirrel-obsolete':
195   case '--squirrel-updated':
196     app.quit()
197     break
198   default:
199     initialize()
200 }
201