1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

refactor main-app process

clean unnecessary codes
This commit is contained in:
Dick Choi
2016-08-22 11:46:22 +09:00
parent 108e83a402
commit cff3fdae6e
5 changed files with 120 additions and 125 deletions

View File

@@ -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')
})