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')
15 let win, bytomdInit, bytomdMining
17 global.fileExist = false
18 global.mining = {isMining: false}
23 function initialize () {
25 function createWindow() {
26 // Create browser Window
28 const icon_path = path.join(__dirname, '/static/images/app-icon/png/app.png')
29 win = new BrowserWindow({
33 'webSecurity': !process.env.DEV_URL,
34 'preload': path.join(__dirname, '/modules/preload.js')
39 const startUrl = process.env.DEV_URL ||
41 pathname: path.join(__dirname, '/public/index.html'),
48 win.webContents.openDevTools()
51 win.webContents.on('new-window', function(e, url) {
53 shell.openExternal(url)
56 win.webContents.on('did-finish-load', function () {
58 win.webContents.send('ConfiguredNetwork', 'startNode')
62 win.on('closed', () => {
68 app.on('ready', () => {
80 app.on('window-all-closed', () => {
81 if (process.platform !== 'darwin') {
86 app.on('activate', () => {
92 app.on('before-quit', () => {
94 bytomdInit.kill('SIGINT')
95 log.info('Kill bytomd Init command...')
98 bytomdMining.kill('SIGINT')
99 const killTimeout = setTimeout(() => {
100 bytomdMining.kill('SIGKILL')
101 }, 8000 /* 8 seconds */)
103 bytomdMining.once('close', () => {
104 clearTimeout(killTimeout)
108 log.info('Kill bytomd Mining command...')
113 function setBytomMining(event) {
114 bytomdMining = spawn( `${Settings.bytomdPath}`, ['node', '--web.closed'] )
116 bytomdMining.stdout.on('data', function(data) {
117 bytomdLog.info(`bytomd mining: ${data}`)
120 bytomdMining.stderr.on('data', function(data) {
121 bytomdLog.info(`bytomd mining: ${data}`)
122 if(data.includes('msg="Start node')) {
124 event.sender.send('ConfiguredNetwork', 'startNode')
128 win.webContents.send('ConfiguredNetwork', 'startNode')
133 bytomdMining.on('exit', function (code) {
134 bytomdLog.info('bytom Mining exited with code ' + code)
139 function setBytomInit(event, bytomNetwork) {
141 bytomdInit = spawn(`${Settings.bytomdPath}`, ['init', '--chain_id', `${bytomNetwork}`] )
143 bytomdInit.stdout.on('data', function(data) {
144 bytomdLog.info(`bytomd init: ${data}`)
147 bytomdInit.stderr.on('data', function(data) {
148 bytomdLog.info(`bytomd init: ${data}`)
151 bytomdInit.on('exit', function (code) {
152 event.sender.send('ConfiguredNetwork','init')
153 setBytomMining(event)
154 bytomdLog.info('bytom init exited with code ' + code)
157 bytomdInit.once('close', () => {
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
169 log.info('Init Bytomd Network...')
170 ipcMain.on('bytomdInitNetwork', (event, arg) => {
171 setBytomInit( event, arg )
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) })
182 function setupConfigure(){
183 const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
184 const loggerOptions = Object.assign(logFolder)
185 logger.setup(loggerOptions)
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':