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 tcpPortUsed = require('tcp-port-used');
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({
35 titleBarStyle: 'hidden',
37 'webSecurity': !process.env.DEV_URL,
38 'preload': path.join(__dirname, '/modules/preload.js')
43 const startUrl = process.env.DEV_URL ||
45 pathname: path.join(__dirname, '/public/index.html'),
52 win.webContents.openDevTools()
55 win.webContents.on('new-window', function(e, url) {
57 shell.openExternal(url)
60 win.webContents.on('did-finish-load', function () {
62 win.webContents.send('ConfiguredNetwork', 'startNode')
66 win.on('closed', () => {
72 app.on('ready', () => {
84 app.on('window-all-closed', () => {
85 if (process.platform !== 'darwin') {
90 app.on('activate', () => {
96 app.on('before-quit', () => {
98 bytomdInit.kill('SIGINT')
99 log.info('Kill bytomd Init command...')
102 bytomdNode.kill('SIGINT')
103 const killTimeout = setTimeout(() => {
104 bytomdNode.kill('SIGKILL')
105 }, 8000 /* 8 seconds */)
107 bytomdNode.once('close', () => {
108 clearTimeout(killTimeout)
112 log.info('Kill bytomd Mining command...')
117 function setBytomNode(event) {
118 bytomdNode = spawn( `${Settings.bytomdPath}`, ['node', '--web.closed', '--home', Settings.bytomdDataPath] )
120 bytomdNode.stdout.on('data', function(data) {
121 bytomdLog.info(`bytomd node: ${data}`)
124 bytomdNode.stderr.on('data', function(data) {
125 bytomdNode.on('exit', function (code) {
126 bytomdLog.info('bytom Node exited with code ' + code)
131 tcpPortUsed.waitUntilUsed(9888, 500, 20000)
134 event.sender.send('ConfiguredNetwork', 'startNode')
138 win.webContents.send('ConfiguredNetwork', 'startNode')
141 bytomdLog.info('Error:', err.message);
145 function setBytomInit(event, bytomNetwork) {
147 bytomdInit = spawn(`${Settings.bytomdPath}`, ['init', '--chain_id', `${bytomNetwork}`, '--home', Settings.bytomdDataPath] )
149 bytomdInit.stdout.on('data', function(data) {
150 bytomdLog.info(`bytomd init: ${data}`)
153 bytomdInit.stderr.on('data', function(data) {
154 bytomdLog.info(`bytomd init: ${data}`)
157 bytomdInit.on('exit', function (code) {
158 event.sender.send('ConfiguredNetwork','init')
160 bytomdLog.info('bytom init exited with code ' + code)
163 bytomdInit.once('close', () => {
169 const filePath = path.join(`${Settings.bytomdDataPath}/config.toml`)
170 if (fs.existsSync(filePath)) {
171 log.info('Bytomd Network has been inited')
172 global.fileExist = true
175 log.info('Init Bytomd Network...')
176 ipcMain.on('bytomdInitNetwork', (event, arg) => {
177 setBytomInit( event, arg )
182 // Require each JS file in the main-process dir
183 function loadMenu () {
184 const files = glob.sync(path.join(__dirname, 'modules/menus/*.js'))
185 files.forEach((file) => { require(file) })
188 function setupConfigure(){
189 const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
190 const loggerOptions = Object.assign(logFolder)
191 logger.setup(loggerOptions)
195 // Handle Squirrel on Windows startup events
196 switch (process.argv[1]) {
197 case '--squirrel-install':
198 case '--squirrel-uninstall':
199 case '--squirrel-obsolete':
200 case '--squirrel-updated':