OSDN Git Service

Load the bytomd in the electron program
authorZhiting Lin <zlin035@uottawa.ca>
Thu, 22 Mar 2018 11:36:38 +0000 (19:36 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Thu, 22 Mar 2018 11:36:38 +0000 (19:36 +0800)
main.js

diff --git a/main.js b/main.js
index 3f5bbe6..0e2a618 100644 (file)
--- 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