Permalink
Browse files

refactor main-app process

clean unnecessary codes
  • Loading branch information...
1 parent 108e83a commit cff3fdae6e371abe5d7aa2e0c5db598da15bb50a @Rokt33r Rokt33r committed Aug 22, 2016
Showing with 119 additions and 124 deletions.
  1. +76 −1 index.js
  2. +7 −1 lib/finder-window.js
  3. +30 −117 lib/main-app.js
  4. +1 −5 lib/main-menu.js
  5. +5 −0 lib/main-window.js
View
@@ -1,10 +1,85 @@
+const { app } = require('electron')
+const ChildProcess = require('child_process')
+const path = require('path')
+
+var error = null
+
function isFinderCalled () {
var argv = process.argv.slice(1)
return argv.some((arg) => arg.match(/--finder/))
}
+function execMainApp () {
+ const appRootPath = path.join(process.execPath, '../..')
+ const updateDotExePath = path.join(appRootPath, 'Update.exe')
+ const exeName = path.basename(process.execPath)
+
+ function spawnUpdate (args, cb) {
+ var stdout = ''
+ var updateProcess = null
+ try {
+ updateProcess = ChildProcess.spawn(updateDotExePath, args)
+ } catch (e) {
+ process.nextTick(function () {
+ cb(e)
+ })
+ }
+
+ updateProcess.stdout.on('data', function (data) {
+ stdout += data
+ })
+
+ updateProcess.on('error', function (_error) {
+ error = _error
+ })
+ updateProcess.on('close', function (code, signal) {
+ if (code !== 0) {
+ error = new Error('Command failed: #{signal ? code}')
+ error.code = code
+ error.stdout = stdout
+ }
+
+ cb(error, stdout)
+ })
+ }
+
+ var handleStartupEvent = function () {
+ if (process.platform !== 'win32') {
+ return false
+ }
+
+ var squirrelCommand = process.argv[1]
+ switch (squirrelCommand) {
+ case '--squirrel-install':
+ spawnUpdate(['--createShortcut', exeName], function (err) {
+ if (err) console.error(err)
+ app.quit()
+ })
+ return true
+ case '--squirrel-updated':
+ app.quit()
+ return true
+ case '--squirrel-uninstall':
+ spawnUpdate(['--removeShortcut', exeName], function (err) {
+ if (err) console.error(err)
+ app.quit()
+ })
+ return true
+ case '--squirrel-obsolete':
+ app.quit()
+ return true
+ }
+ }
+
+ if (handleStartupEvent()) {
+ return
+ }
+
+ require('./lib/main-app')
+}
+
if (isFinderCalled()) {
require('./lib/finder-app')
} else {
- require('./lib/main-app')
+ execMainApp()
}
@@ -1,4 +1,5 @@
const electron = require('electron')
+const { app } = electron
const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu
const MenuItem = electron.MenuItem
@@ -44,7 +45,7 @@ finderWindow.on('close', function (e) {
})
var appIcon = new Tray(path.join(__dirname, '../resources/tray-icon.png'))
-appIcon.setToolTip('Boost')
+appIcon.setToolTip('Boostnote')
var trayMenu = new Menu()
trayMenu.append(new MenuItem({
@@ -84,4 +85,9 @@ function hideFinder () {
}
finderWindow.hide()
}
+
+app.on('before-quit', function (e) {
+ finderWindow.removeAllListeners()
+})
+
module.exports = finderWindow
View
@@ -8,76 +8,11 @@ const ChildProcess = require('child_process')
const _ = require('lodash')
const GhReleases = require('electron-gh-releases')
// electron.crashReporter.start()
+require('./ipc')
var mainWindow = null
-var finderProcess = null
var finderWindow = null
-const appRootPath = path.join(process.execPath, '../..')
-const updateDotExePath = path.join(appRootPath, 'Update.exe')
-const exeName = path.basename(process.execPath)
-
-// For windows app
-function spawnUpdate (args, cb) {
- var stdout = ''
- var updateProcess = null
- try {
- updateProcess = ChildProcess.spawn(updateDotExePath, args)
- } catch (e) {
- process.nextTick(function () {
- cb(e)
- })
- }
-
- updateProcess.stdout.on('data', function (data) {
- stdout += data
- })
-
- error = null
- updateProcess.on('error', function (_error) {
- error = _error
- })
- updateProcess.on('close', function (code, signal) {
- if (code !== 0) {
- error = new Error("Command failed: #{signal ? code}")
- error.code = code
- error.stdout = stdout
- }
-
- cb(error, stdout)
- })
-}
-
-var handleStartupEvent = function () {
- if (process.platform !== 'win32') {
- return false
- }
-
- var squirrelCommand = process.argv[1]
- switch (squirrelCommand) {
- case '--squirrel-install':
- spawnUpdate(['--createShortcut', exeName], function (err) {
- app.quit()
- })
- return true
- case '--squirrel-updated':
- app.quit()
- return true
- case '--squirrel-uninstall':
- spawnUpdate(['--removeShortcut', exeName], function (err) {
- app.quit()
- })
- return true
- case '--squirrel-obsolete':
- app.quit()
- return true
- }
-}
-
-if (handleStartupEvent()) {
- return
-}
-
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
if (mainWindow) {
if (process.platform === 'win32') {
@@ -90,7 +25,6 @@ var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory)
})
if (shouldQuit) {
- if (mainWindow != null) mainWindow.removeAllListeners()
app.quit()
return
}
@@ -117,15 +51,10 @@ function checkUpdate () {
if (err) {
var isLatest = err.message === 'There is no newer version.'
if (!isLatest) console.error('Updater error! %s', err.message)
+ return
}
- if (!err) {
- if (status) {
- // Download start
- mainWindow.webContents.send('update-found', 'Update found!')
- updater.download()
- } else {
- // Latest version
- }
+ if (status) {
+ updater.download()
}
})
}
@@ -137,69 +66,53 @@ updater.on('update-downloaded', (info) => {
}
})
-
-function spawnFinder() {
- if (process.platform === 'darwin') {
- var finderArgv = [path.join(__dirname, 'finder-app.js'), '--finder']
- if (_.find(process.argv, a => a === '--hot')) finderArgv.push('--hot')
- finderProcess = ChildProcess
- .execFile(process.execPath, finderArgv)
+ipc.on('update-app-confirm', function (event, msg) {
+ if (isUpdateReady) {
+ updater.install()
}
-}
-
-app.on('ready', function () {
- var template = require('./main-menu')
- var menu = Menu.buildFromTemplate(template)
- if (process.platform === 'darwin' || process.platform === 'linux') {
- Menu.setApplicationMenu(menu)
- }
-
- // Check update every 24 hours
- setInterval(function () {
- checkUpdate()
- }, 1000 * 60 * 60 * 24)
-
- ipc.on('check-update', function (event, msg) {
- checkUpdate()
- })
+})
- ipc.on('update-app-confirm', function (event, msg) {
- if (isUpdateReady) {
- mainWindow.removeAllListeners()
- updater.install()
- }
- })
+function spawnFinder () {
+ var finderArgv = [path.join(__dirname, 'finder-app.js'), '--finder']
+ if (_.find(process.argv, a => a === '--hot')) finderArgv.push('--hot')
+ var finderProcess = ChildProcess
+ .execFile(process.execPath, finderArgv)
- ipc.on('quit-app-confirm', function () {
- mainWindow.removeAllListeners()
- app.quit()
+ app.on('before-quit', function () {
+ finderProcess.kill()
})
+}
- checkUpdate()
-
+app.on('ready', function () {
mainWindow = require('./main-window')
- if (process.platform === 'win32' || process.platform === 'linux') {
- mainWindow.setMenu(menu)
- }
+ var template = require('./main-menu')
+ var menu = Menu.buildFromTemplate(template)
switch (process.platform) {
case 'darwin':
spawnFinder()
+ Menu.setApplicationMenu(menu)
break
case 'win32':
finderWindow = require('./finder-window')
- finderWindow.on('close', function (e) {
- e.preventDefault()
- finderWindow.hide()
- })
+ mainWindow.setMenu(menu)
break
case 'linux':
// Finder is available on cinnamon only.
if (process.env.DESKTOP_SESSION === 'cinnamon') {
finderWindow = require('./finder-window')
}
+ Menu.setApplicationMenu(menu)
+ mainWindow.setMenu(menu)
}
+ // Check update every hour
+ setInterval(function () {
+ checkUpdate()
+ }, 1000 * 60 * 60)
+
+ checkUpdate()
+
require('./hotkey')
})
View
@@ -36,11 +36,7 @@ var boost = OSX
type: 'separator'
},
{
- label: 'Quit',
- accelerator: 'Command+Q',
- click: function () {
- mainWindow.webContents.send('quit-app', {})
- }
+ role: 'quit'
}
]
}
View
@@ -2,6 +2,7 @@ const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const path = require('path')
+const ipc = require('./ipc')
var mainWindow = new BrowserWindow({
width: 1080,
@@ -46,4 +47,8 @@ app.on('activate', function () {
mainWindow.show()
})
+app.on('before-quit', function (e) {
+ mainWindow.removeAllListeners()
+})
+
module.exports = mainWindow

0 comments on commit cff3fda

Please sign in to comment.