OSDN Git Service

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