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 vapordLog = logger.create('vapord')
12 const Settings = require('./modules/settings')
14 const net = require('net')
16 let win, vapordInit, vapordNode
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 vapordInit.kill('SIGINT')
96 log.info('Kill vapord Init command...')
99 vapordNode.kill('SIGINT')
100 const killTimeout = setTimeout(() => {
101 vapordNode.kill('SIGKILL')
102 }, 8000 /* 8 seconds */)
104 vapordNode.once('close', () => {
105 clearTimeout(killTimeout)
109 log.info('Kill vapord Mining command...')
114 function setVaporNode(event) {
115 vapordNode = spawn( `${Settings.vapordPath}`, ['node', '--web.closed'] )
117 vapordNode.stdout.on('data', function(data) {
118 vapordLog.info(`vapord node: ${data}`)
121 vapordNode.stderr.on('data', function(data) {
122 vapordLog.info(`vapord node: ${data}`)
123 if (data.includes('msg="start node')) {
125 event.sender.send('ConfiguredNetwork', 'startNode')
129 win.webContents.send('ConfiguredNetwork', 'startNode')
133 vapordNode.on('exit', function (code) {
134 vapordLog.info('vapor Node exited with code ' + code)
140 function setVaporInit(event, vaporNetwork) {
142 vapordInit = spawn(`${Settings.vapordPath}`, ['init', '--chain_id', `${vaporNetwork}`] )
144 vapordInit.stdout.on('data', function(data) {
145 vapordLog.info(`vapord init: ${data}`)
148 vapordInit.stderr.on('data', function(data) {
149 vapordLog.info(`vapord init: ${data}`)
152 vapordInit.on('exit', function (code) {
153 event.sender.send('ConfiguredNetwork','init')
155 vapordLog.info('vapor init exited with code ' + code)
158 vapordInit.once('close', () => {
164 const filePath = path.join(`${Settings.vapordDataPath}/config.toml`)
165 if (fs.existsSync(filePath)) {
166 log.info('Vapord Network has been inited')
167 global.fileExist = true
170 log.info('Init Vapord Network...')
171 ipcMain.on('vapordInitNetwork', (event, arg) => {
172 setVaporInit( event, arg )
177 // Require each JS file in the main-process dir
178 function loadMenu () {
179 const files = glob.sync(path.join(__dirname, 'modules/menus/*.js'))
180 files.forEach((file) => { require(file) })
183 function setupConfigure(){
184 const logFolder = {logFolder: path.join(app.getPath('userData'), 'logs')}
185 const loggerOptions = Object.assign(logFolder)
186 logger.setup(loggerOptions)
190 // Handle Squirrel on Windows startup events
191 switch (process.argv[1]) {
192 case '--squirrel-install':
193 case '--squirrel-uninstall':
194 case '--squirrel-obsolete':
195 case '--squirrel-updated':