mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
302 lines
6.6 KiB
JavaScript
302 lines
6.6 KiB
JavaScript
const electron = require('electron')
|
|
const app = electron.app
|
|
const Menu = electron.Menu
|
|
const ipc = electron.ipcMain
|
|
const autoUpdater = electron.autoUpdater
|
|
const path = require('path')
|
|
const ChildProcess = require('child_process')
|
|
const _ = require('lodash')
|
|
const nodeIpc = require('@rokt33r/node-ipc')
|
|
const GhReleases = require('electron-gh-releases')
|
|
// electron.crashReporter.start()
|
|
|
|
var mainWindow = null
|
|
var finderProcess = null
|
|
var finderWindow = null
|
|
var update = null
|
|
|
|
// app.on('window-all-closed', function () {
|
|
// if (process.platform !== 'darwin') app.quit()
|
|
// })
|
|
|
|
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
|
|
})
|
|
|
|
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) {
|
|
quitApp()
|
|
})
|
|
return true
|
|
case '--squirrel-updated':
|
|
quitApp()
|
|
return true
|
|
case '--squirrel-uninstall':
|
|
spawnUpdate(['--removeShortcut', exeName], function (err) {
|
|
quitApp()
|
|
})
|
|
quitApp()
|
|
return true
|
|
case '--squirrel-obsolete':
|
|
quitApp()
|
|
return true
|
|
}
|
|
}
|
|
|
|
if (handleStartupEvent()) {
|
|
return
|
|
}
|
|
|
|
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
|
|
if (mainWindow) {
|
|
if (process.platform === 'win32') {
|
|
mainWindow.minimize()
|
|
mainWindow.restore()
|
|
}
|
|
mainWindow.focus()
|
|
}
|
|
return true
|
|
})
|
|
|
|
if (shouldQuit) {
|
|
quitApp()
|
|
return
|
|
}
|
|
|
|
var appQuit = false
|
|
|
|
var version = app.getVersion()
|
|
var versionText = (version == null || version.length === 0) ? 'DEV version' : 'v' + version
|
|
var versionNotified = false
|
|
|
|
function notify (title, body) {
|
|
if (mainWindow != null) {
|
|
mainWindow.webContents.send('notify', {
|
|
title: title,
|
|
body: body
|
|
})
|
|
}
|
|
}
|
|
|
|
var isUpdateReady = false
|
|
|
|
var ghReleasesOpts = {
|
|
repo: 'BoostIO/boost-releases',
|
|
currentVersion: app.getVersion()
|
|
}
|
|
|
|
const updater = new GhReleases(ghReleasesOpts)
|
|
|
|
// Check for updates
|
|
// `status` returns true if there is a new update available
|
|
function checkUpdate () {
|
|
updater.check((err, status) => {
|
|
if (err) {
|
|
console.error(err)
|
|
if (!versionNotified) notify('Updater error!', message)
|
|
}
|
|
if (!err) {
|
|
if (status) {
|
|
notify('Update is available!', 'Download started.. wait for the update ready.')
|
|
updater.download()
|
|
} else {
|
|
if (!versionNotified) {
|
|
versionNotified = true
|
|
notify('Latest Build!! ' + versionText, 'Hope you to enjoy our app :D')
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
updater.on('update-downloaded', (info) => {
|
|
if (mainWindow != null) {
|
|
notify('Ready to Update!', 'Click update button on Main window.')
|
|
mainWindow.webContents.send('update-available', 'Update available!')
|
|
isUpdateReady = true
|
|
}
|
|
})
|
|
|
|
nodeIpc.config.id = 'node'
|
|
nodeIpc.config.retry = 1500
|
|
// nodeIpc.config.silent = true
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
nodeIpc.serve(
|
|
path.join(app.getPath('userData'), 'boost.service'),
|
|
function () {
|
|
nodeIpc.server.on(
|
|
'connect',
|
|
function (socket) {
|
|
socket.on('close', function () {
|
|
console.log('socket dead')
|
|
if (!appQuit) spawnFinder()
|
|
})
|
|
}
|
|
)
|
|
nodeIpc.server.on(
|
|
'message',
|
|
function (data, socket) {
|
|
console.log('>>', data)
|
|
format(data)
|
|
}
|
|
)
|
|
nodeIpc.server.on(
|
|
'error',
|
|
function (err) {
|
|
console.log('>>', err)
|
|
}
|
|
)
|
|
}
|
|
)
|
|
|
|
function format (payload) {
|
|
switch (payload.type) {
|
|
case 'show-main-window':
|
|
if (process.platform === 'darwin') {
|
|
mainWindow.show()
|
|
} else {
|
|
mainWindow.minimize()
|
|
mainWindow.restore()
|
|
}
|
|
break
|
|
case 'copy-finder':
|
|
mainWindow.webContents.send('copy-finder')
|
|
break
|
|
case 'quit-app':
|
|
quitApp()
|
|
break
|
|
}
|
|
}
|
|
|
|
function quitApp () {
|
|
appQuit = true
|
|
if (finderProcess) finderProcess.kill()
|
|
app.quit()
|
|
}
|
|
|
|
app.on('ready', function () {
|
|
app.on('before-quit', function () {
|
|
console.log('before quite')
|
|
appQuit = true
|
|
if (finderProcess) finderProcess.kill()
|
|
})
|
|
|
|
var template = require('./main-menu')
|
|
if (process.platform === 'win32') {
|
|
template.unshift({
|
|
label: 'Boostnote',
|
|
submenu: [
|
|
{
|
|
label: 'Quit',
|
|
accelerator: 'Control+Q',
|
|
click: function (e) {
|
|
quitApp()
|
|
}
|
|
}
|
|
]
|
|
})
|
|
}
|
|
var menu = Menu.buildFromTemplate(template)
|
|
if (process.platform === 'darwin') {
|
|
Menu.setApplicationMenu(menu)
|
|
}
|
|
|
|
setInterval(function () {
|
|
checkUpdate()
|
|
}, 1000 * 60 * 60 * 24)
|
|
|
|
ipc.on('check-update', function (event, msg) {
|
|
if (update == null) checkUpdate()
|
|
})
|
|
|
|
ipc.on('update-app', function (event, msg) {
|
|
if (isUpdateReady) {
|
|
appQuit = true
|
|
updater.install()
|
|
}
|
|
})
|
|
|
|
checkUpdate()
|
|
|
|
mainWindow = require('./main-window')
|
|
if (process.platform === 'win32') {
|
|
mainWindow.setMenu(menu)
|
|
}
|
|
mainWindow.on('close', function (e) {
|
|
if (appQuit) return true
|
|
e.preventDefault()
|
|
mainWindow.hide()
|
|
})
|
|
|
|
if (finderProcess == null && process.platform === 'darwin') {
|
|
console.log('fired only once ')
|
|
spawnFinder()
|
|
} else {
|
|
finderWindow = require('./finder-window')
|
|
|
|
finderWindow.on('close', function (e) {
|
|
if (appQuit) return true
|
|
e.preventDefault()
|
|
finderWindow.hide()
|
|
})
|
|
}
|
|
|
|
nodeIpc.server.start(function (err) {
|
|
if (err.code === 'EADDRINUSE') {
|
|
notify('Error occurs!', 'You have to kill other Boostnote processes.')
|
|
quitApp()
|
|
}
|
|
})
|
|
|
|
require('./hotkey')
|
|
})
|
|
|
|
module.exports = app
|
|
|