From cff3fdae6e371abe5d7aa2e0c5db598da15bb50a Mon Sep 17 00:00:00 2001 From: Dick Choi Date: Mon, 22 Aug 2016 11:46:22 +0900 Subject: [PATCH] refactor main-app process clean unnecessary codes --- index.js | 77 +++++++++++++++++++++- lib/finder-window.js | 8 ++- lib/main-app.js | 149 +++++++++---------------------------------- lib/main-menu.js | 6 +- lib/main-window.js | 5 ++ 5 files changed, 120 insertions(+), 125 deletions(-) diff --git a/index.js b/index.js index 7a1fdf7b..5a34df9f 100644 --- a/index.js +++ b/index.js @@ -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() } diff --git a/lib/finder-window.js b/lib/finder-window.js index e75bab16..e1321eb0 100644 --- a/lib/finder-window.js +++ b/lib/finder-window.js @@ -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 diff --git a/lib/main-app.js b/lib/main-app.js index d402b946..f41c3ffc 100644 --- a/lib/main-app.js +++ b/lib/main-app.js @@ -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() } +}) + +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) + + app.on('before-quit', function () { + finderProcess.kill() + }) } app.on('ready', function () { + mainWindow = require('./main-window') + 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() - } - }) - - ipc.on('quit-app-confirm', function () { - mainWindow.removeAllListeners() - app.quit() - }) - - checkUpdate() - - mainWindow = require('./main-window') - if (process.platform === 'win32' || process.platform === 'linux') { - mainWindow.setMenu(menu) - } - 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') }) diff --git a/lib/main-menu.js b/lib/main-menu.js index 4a5486f3..1bcc9b0f 100644 --- a/lib/main-menu.js +++ b/lib/main-menu.js @@ -36,11 +36,7 @@ var boost = OSX type: 'separator' }, { - label: 'Quit', - accelerator: 'Command+Q', - click: function () { - mainWindow.webContents.send('quit-app', {}) - } + role: 'quit' } ] } diff --git a/lib/main-window.js b/lib/main-window.js index f952e928..21242f74 100644 --- a/lib/main-window.js +++ b/lib/main-window.js @@ -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