1 const {app, BrowserWindow, ipcMain, shell} = require('electron')
2 const spawn = require('child_process').spawn
3 const glob = require('glob')
4 const url = require('url')
5 const path = require('path')
6 const fs = require('fs')
7 const logger = require('./main-process/logger')
8 const log = logger.create('main')
9 const bytomdLog = logger.create('bytomd')
11 let win, bytomdInit, bytomdMining
13 global.fileExist = false
14 global.mining = {isMining: false}
17 function initialize () {
19 function createWindow() {
20 // Create browser Window
22 const icon_path = path.join(__dirname, '/static/images/app-icon/png/app.png')
23 win = new BrowserWindow({
27 'webSecurity': !process.env.DEV_URL,
28 'preload': path.join(__dirname, '/main-process/preload.js')
33 const startUrl = process.env.DEV_URL ||
35 pathname: path.join(__dirname, '/public/index.html'),
42 win.webContents.openDevTools()
45 win.webContents.on('new-window', function(e, url) {
47 shell.openExternal(url)
50 win.webContents.on('did-finish-load', function () {
52 win.webContents.send('ConfiguredNetwork', 'startNode')
56 win.on('closed', () => {
62 app.on('ready', () => {
74 app.on('window-all-closed', () => {
75 if (process.platform !== 'darwin') {
80 app.on('activate', () => {
86 app.on('before-quit', () => {
88 bytomdInit.kill('SIGINT')
89 log.info('Kill bytomd Init command...')
92 bytomdMining.kill('SIGINT')
93 const killTimeout = setTimeout(() => {
94 bytomdMining.kill('SIGKILL')
95 }, 8000 /* 8 seconds */)
97 bytomdMining.once('close', () => {
98 clearTimeout(killTimeout)
102 log.info('Kill bytomd Mining command...')
106 const bytomdPath = process.env.DEV?
107 path.join(__dirname, '/bytomd/bytomd-darwin_amd64'):
108 glob.sync( path.join(__dirname, '../bytomd/bytomd*'))
111 switch (process.platform){
113 bytomdDataPath = `${app.getPath('appData')}/Bytom`
116 bytomdDataPath = `${app.getPath('home')}/Library/Bytom`
119 bytomdDataPath = `${app.getPath('home')}/.bytom`
122 function setBytomMining(event) {
123 bytomdMining = spawn( `${bytomdPath}`, ['node', '--web.closed'] )
125 bytomdMining.stdout.on('data', function(data) {
126 bytomdLog.info(`bytomd mining: ${data}`)
129 bytomdMining.stderr.on('data', function(data) {
130 bytomdLog.info(`bytomd mining: ${data}`)
131 if(data.includes('msg="Start node')) {
133 event.sender.send('ConfiguredNetwork', 'startNode')
137 win.webContents.send('ConfiguredNetwork', 'startNode')
142 bytomdMining.on('exit', function (code) {
143 bytomdLog.info('bytom Mining exited with code ' + code)
148 function setBytomInit(event, bytomNetwork) {
150 bytomdInit = spawn(`${bytomdPath}`, ['init', '--chain_id', `${bytomNetwork}`] )
152 bytomdInit.stdout.on('data', function(data) {
153 bytomdLog.info(`bytomd init: ${data}`)
156 bytomdInit.stderr.on('data', function(data) {
157 bytomdLog.info(`bytomd init: ${data}`)
160 bytomdInit.on('exit', function (code) {
161 event.sender.send('ConfiguredNetwork','init')
162 setBytomMining(event)
163 bytomdLog.info('bytom init exited with code ' + code)
166 bytomdInit.once('close', () => {
172 const filePath = path.join(`${bytomdDataPath}/config.toml`)
173 if (fs.existsSync(filePath)) {
174 log.info('Bytomd Network has been inited')
175 global.fileExist = true
178 log.info('Init Bytomd Network...')
179 ipcMain.on('bytomdInitNetwork', (event, arg) => {
180 setBytomInit( event, arg )
185 // Require each JS file in the main-process dir
186 function loadMenu () {
187 const files = glob.sync(path.join(__dirname, 'main-process/menus/*.js'))
188 files.forEach((file) => { require(file) })
191 function setupConfigure(){
192 const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
193 const loggerOptions = Object.assign(logFolder)
194 logger.setup(loggerOptions)
197 // Handle Squirrel on Windows startup events
198 switch (process.argv[1]) {
199 case '--squirrel-install':
200 case '--squirrel-uninstall':
201 case '--squirrel-obsolete':
202 case '--squirrel-updated':