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')
14 const net = require('net')
16 let win, bytomdInit, bytomdNode
18 global.fileExist = false
19 global.mining = {isMining: false}
24 function initialize () {
26 function createWindow() {
27 // Create browser Window
29 const icon_path = path.join(__dirname, '/static/images/app-icon/png/app.png')
30 win = new BrowserWindow({
34 'webSecurity': !process.env.DEV_URL,
35 'preload': path.join(__dirname, '/modules/preload.js')
40 const startUrl = process.env.DEV_URL ||
42 pathname: path.join(__dirname, '/public/index.html'),
49 win.webContents.openDevTools()
52 win.webContents.on('new-window', function(e, url) {
54 shell.openExternal(url)
57 win.webContents.on('did-finish-load', function () {
59 win.webContents.send('ConfiguredNetwork', 'startNode')
63 win.on('closed', () => {
69 app.on('ready', () => {
81 app.on('window-all-closed', () => {
82 if (process.platform !== 'darwin') {
87 app.on('activate', () => {
93 app.on('before-quit', () => {
95 bytomdInit.kill('SIGINT')
96 log.info('Kill bytomd Init command...')
99 bytomdNode.kill('SIGINT')
100 const killTimeout = setTimeout(() => {
101 bytomdNode.kill('SIGKILL')
102 }, 8000 /* 8 seconds */)
104 bytomdNode.once('close', () => {
105 clearTimeout(killTimeout)
109 log.info('Kill bytomd Mining command...')
114 function setBytomNode() {
115 bytomdNode = spawn( `${Settings.bytomdPath}`, ['node', '--web.closed'] )
117 function checkPort () {
118 setTimeout(function () {
119 portInUse(9888, function(returnValue) {
124 win.webContents.send('ConfiguredNetwork', 'startNode')
132 bytomdNode.stdout.on('data', function(data) {
133 bytomdLog.info(`bytomd node: ${data}`)
136 bytomdNode.stderr.on('data', function(data) {
137 bytomdLog.info(`bytomd node: ${data}`)
140 bytomdNode.on('exit', function (code) {
141 bytomdLog.info('bytom Node exited with code ' + code)
146 function setBytomInit(event, bytomNetwork) {
148 bytomdInit = spawn(`${Settings.bytomdPath}`, ['init', '--chain_id', `${bytomNetwork}`] )
150 bytomdInit.stdout.on('data', function(data) {
151 bytomdLog.info(`bytomd init: ${data}`)
154 bytomdInit.stderr.on('data', function(data) {
155 bytomdLog.info(`bytomd init: ${data}`)
158 bytomdInit.on('exit', function (code) {
159 event.sender.send('ConfiguredNetwork','init')
161 bytomdLog.info('bytom init exited with code ' + code)
164 bytomdInit.once('close', () => {
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
176 log.info('Init Bytomd Network...')
177 ipcMain.on('bytomdInitNetwork', (event, arg) => {
178 setBytomInit( event, arg )
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) })
189 function setupConfigure(){
190 const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
191 const loggerOptions = Object.assign(logFolder)
192 logger.setup(loggerOptions)
195 function portInUse(port, callback) {
196 const server = net.createServer(function(socket) {
197 socket.write('Echo server\r\n')
201 server.listen(port, '0.0.0.0')
202 server.on('error', function (e) {
205 server.on('listening', function (e) {
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':