OSDN Git Service

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