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