+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('./main-process/logger')
+const logger = require('./modules/logger')
const log = logger.create('main')
const bytomdLog = logger.create('bytomd')
+const Settings = require('./modules/settings')
-let win, bytomdInit, bytomdMining
+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() {
height: 768,
'webPreferences': {
'webSecurity': !process.env.DEV_URL,
- 'preload': path.join(__dirname, '/main-process/preload.js')
+ 'preload': path.join(__dirname, '/modules/preload.js')
},
icon: icon_path
})
bytomdInit.kill('SIGINT')
log.info('Kill bytomd Init command...')
}
- if(bytomdMining){
- bytomdMining.kill('SIGINT')
+ if(bytomdNode){
+ bytomdNode.kill('SIGINT')
const killTimeout = setTimeout(() => {
- bytomdMining.kill('SIGKILL')
+ bytomdNode.kill('SIGKILL')
}, 8000 /* 8 seconds */)
- bytomdMining.once('close', () => {
+ bytomdNode.once('close', () => {
clearTimeout(killTimeout)
- bytomdMining = null
+ bytomdNode = null
})
log.info('Kill bytomd Mining command...')
}
})
}
-const bytomdPath = process.env.DEV?
- path.join(__dirname, '/bytomd/bytomd-darwin_amd64'):
- glob.sync( path.join(__dirname, '../bytomd/bytomd*'))
-
-let bytomdDataPath
-switch (process.platform){
- case 'win32':
- bytomdDataPath = `${app.getPath('appData')}/Bytom`
- break
- case 'darwin':
- bytomdDataPath = `${app.getPath('home')}/Library/Bytom`
- break
- case 'linux':
- bytomdDataPath = `${app.getPath('home')}/.bytom`
-}
-function setBytomMining(event) {
- bytomdMining = spawn( `${bytomdPath}`, ['node', '--web.closed'] )
+function setBytomNode(event) {
+ bytomdNode = spawn( `${Settings.bytomdPath}`, ['node', '--web.closed'] )
- bytomdMining.stdout.on('data', function(data) {
- bytomdLog.info(`bytomd mining: ${data}`)
+ bytomdNode.stdout.on('data', function(data) {
+ bytomdLog.info(`bytomd node: ${data}`)
})
- bytomdMining.stderr.on('data', function(data) {
- bytomdLog.info(`bytomd mining: ${data}`)
- if(data.includes('msg="Start node')) {
- if(event){
+ 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')
}
- }
- })
-
- bytomdMining.on('exit', function (code) {
- bytomdLog.info('bytom Mining exited with code ' + code)
- app.quit()
- })
+ }, function(err) {
+ bytomdLog.info('Error:', err.message);
+ });
}
function setBytomInit(event, bytomNetwork) {
// Init bytomd
- bytomdInit = spawn(`${bytomdPath}`, ['init', '--chain_id', `${bytomNetwork}`] )
+ bytomdInit = spawn(`${Settings.bytomdPath}`, ['init', '--chain_id', `${bytomNetwork}`] )
bytomdInit.stdout.on('data', function(data) {
bytomdLog.info(`bytomd init: ${data}`)
bytomdInit.on('exit', function (code) {
event.sender.send('ConfiguredNetwork','init')
- setBytomMining(event)
+ setBytomNode(event)
bytomdLog.info('bytom init exited with code ' + code)
})
}
function bytomd(){
- const filePath = path.join(`${bytomdDataPath}/config.toml`)
+ const filePath = path.join(`${Settings.bytomdDataPath}/config.toml`)
if (fs.existsSync(filePath)) {
log.info('Bytomd Network has been inited')
global.fileExist = true
- setBytomMining()
+ setBytomNode()
}else {
log.info('Init Bytomd Network...')
ipcMain.on('bytomdInitNetwork', (event, arg) => {
// Require each JS file in the main-process dir
function loadMenu () {
- const files = glob.sync(path.join(__dirname, 'main-process/menus/*.js'))
+ const files = glob.sync(path.join(__dirname, 'modules/menus/*.js'))
files.forEach((file) => { require(file) })
}
logger.setup(loggerOptions)
}
+
// Handle Squirrel on Windows startup events
switch (process.argv[1]) {
case '--squirrel-install':