From 314dd997f44c62c53a332a4c1a5a9a9181cf80e3 Mon Sep 17 00:00:00 2001 From: Zhiting Lin Date: Thu, 22 Mar 2018 19:36:38 +0800 Subject: [PATCH] Load the bytomd in the electron program --- main.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/main.js b/main.js index 3f5bbe6..0e2a618 100644 --- a/main.js +++ b/main.js @@ -1,11 +1,13 @@ const {app, BrowserWindow} = require('electron') const autoUpdater = require('./auto-updater') +const exec = require('child_process').exec const glob = require('glob') +const fs = require('fs') const i18n = require('./main-process/i18n.js') const url = require('url') const path = require('path') -let win +let win, bytomdInit, bytomdMining global.i18n = i18n @@ -13,7 +15,7 @@ function initialize () { // const shouldQuit = makeSingleInstance() // if (shouldQuit) return app.quit() - loadDemos() + loadMenu() function createWindow() { // 创建浏览器窗口。 @@ -21,37 +23,76 @@ function initialize () { width: 1024 + 208, height: 768, 'webPreferences': { - 'webSecurity': false, + 'webSecurity': !process.env.DEV_URL, 'preload': path.join(__dirname, '/main-process/preload.js') } }) + const startUrl = process.env.DEV_URL || url.format({ pathname: path.join(__dirname, '/public/index.html'), protocol: 'file:', slashes: true }) - // const startUrl = 'http://localhost:3000/' win.loadURL(startUrl) win.webContents.openDevTools() win.on('closed', () => { win = null + quitApp('closed') }) + } - // app.on('ready', createWindow) app.on('ready', () => { + fs.stat('$GOPATH/src/github.com/bytom/cmd/bytomd/.bytomd/genesis.json', function(err) { + if(err == null) { + console.log('File exists') + } else if(err.code == 'ENOENT') { + // file does not exist + bytomdInit = exec('cd $GOPATH/src/github.com/bytom/cmd/bytomd/ && ./bytomd init --chain_id mainnet' , + (error, stdout, stderr) => { + if (error) { + console.error(`bytomd init exec error: ${error}`) + return + } + console.log(`bytomd init stdout: ${stdout}`) + console.log(`bytomd init stderr: ${stderr}`) + }) + } else { + console.log('Some other error: ', err.code) + } + }) + + + bytomdMining = exec('cd $GOPATH/src/github.com/bytom/cmd/bytomd/ && ./bytomd node --mining' , + (error, stdout, stderr) => { + if (error) { + console.error(`bytomd mining exec error: ${error}`) + return + } + console.log(`bytomd mining stdout: ${stdout}`) + console.log(`bytomd mining stderr: ${stderr}`) + }) + createWindow() autoUpdater.initialize() }) + app.on('before-quit',() => { + if(bytomdInit != null){ + bytomdInit.kill() + } + bytomdMining.kill() + }) + + // 当全部窗口关闭时退出。 app.on('window-all-closed', () => { if (process.platform !== 'darwin') { - app.quit() + quitApp('window-all-closed') } }) @@ -59,6 +100,7 @@ function initialize () { if (win === null) { createWindow() } + }) } @@ -74,7 +116,7 @@ function makeSingleInstance () { } // Require each JS file in the main-process dir -function loadDemos () { +function loadMenu () { const files = glob.sync(path.join(__dirname, 'main-process/menus/*.js')) files.forEach((file) => { require(file) }) autoUpdater.updateMenu() @@ -95,3 +137,12 @@ switch (process.argv[1]) { default: initialize() } + + +function quitApp (type) { + if(bytomdInit != null){ + bytomdInit.kill() + } + bytomdMining.kill() + app.quit() +} \ No newline at end of file -- 2.11.0