Skip to content

Commit

Permalink
Implement updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Rokt33r committed Dec 11, 2019
1 parent 37297a4 commit f5afafd
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 15 deletions.
14 changes: 1 addition & 13 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const electron = require('electron')
const path = require('path')
// import { format as formatUrl } from 'url'
require('./menu')

const { app, BrowserWindow } = electron
const isDevelopment = process.env.NODE_ENV !== 'production'
Expand All @@ -15,18 +15,6 @@ function createMainWindow() {

window.loadFile(path.join(__dirname, '../compiled/index.html'))

// if (isDevelopment) {
// } else {
// window.loadURL(`http://localhost:3000/app`)
// // window.loadURL(
// // formatUrl({
// // pathname: path.join(__dirname, 'index.html'),
// // protocol: 'file',
// // slashes: true
// // })
// // )
// }

window.on('closed', () => {
mainWindow = null
})
Expand Down
105 changes: 105 additions & 0 deletions app/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const { app, Menu } = require('electron')
const { checkForUpdates } = require('./updater')

const isMac = process.platform === 'darwin'

const template = [
// { role: 'appMenu' }
...(isMac
? [
{
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
}
]
: []),
// { role: 'fileMenu' }
{
label: 'File',
submenu: [isMac ? { role: 'close' } : { role: 'quit' }]
},
// { role: 'editMenu' }
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
...(isMac
? [
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ role: 'selectAll' },
{ type: 'separator' },
{
label: 'Speech',
submenu: [{ role: 'startspeaking' }, { role: 'stopspeaking' }]
}
]
: [{ role: 'delete' }, { type: 'separator' }, { role: 'selectAll' }])
]
},
// { role: 'viewMenu' }
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
// { role: 'windowMenu' }
{
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
...(isMac
? [
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'window' }
]
: [{ role: 'close' }])
]
},
{
role: 'help',
submenu: [
{
label: 'Check For Updates',
click: checkForUpdates
},
{
label: 'Learn More',
click: async () => {
const { shell } = require('electron')
await shell.openExternal('https://boostnote.io')
}
}
]
}
]

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
69 changes: 69 additions & 0 deletions app/updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* updater.js
*
* Please use manual update only when it is really required, otherwise please use recommended non-intrusive auto update.
*
* Import steps:
* 1. create `updater.js` for the code snippet
* 2. require `updater.js` for menu implementation, and set `checkForUpdates` callback from `updater` for the click property of `Check Updates...` MenuItem.
*/
const { dialog } = require('electron')
const { autoUpdater } = require('electron-updater')

let updater
autoUpdater.autoDownload = false

autoUpdater.on('error', error => {
dialog.showErrorBox(
'Error: ',
error == null ? 'unknown' : (error.stack || error).toString()
)
})

autoUpdater.on('update-available', () => {
dialog.showMessageBox(
{
type: 'info',
title: 'Found Updates',
message: 'Found updates, do you want update now?',
buttons: ['Sure', 'No']
},
buttonIndex => {
if (buttonIndex === 0) {
autoUpdater.downloadUpdate()
} else {
updater.enabled = true
updater = null
}
}
)
})

autoUpdater.on('update-not-available', () => {
dialog.showMessageBox({
title: 'No Updates',
message: 'Current version is up-to-date.'
})
updater.enabled = true
updater = null
})

autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox(
{
title: 'Install Updates',
message: 'Updates downloaded, application will be quit for update...'
},
() => {
setImmediate(() => autoUpdater.quitAndInstall())
}
)
})

// export this to MenuItem click callback
function checkForUpdates(menuItem, focusedWindow, event) {
updater = menuItem
updater.enabled = false
autoUpdater.checkForUpdates()
}
module.exports.checkForUpdates = checkForUpdates
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "app/index.js",
"repository": {
"type": "git",
"url": "https://github.com/BoostIO/boost.git"
"url": "https://github.com/BoostIO/Boostnote.next.git"
},
"scripts": {
"dev": "env-cmd cross-env TS_NODE_PROJECT=\"tsconfig-webpack.json\" webpack-dev-server --mode development --open-page \"app\"",
Expand Down Expand Up @@ -152,6 +152,7 @@
"asar": false,
"linux": {
"target": "deb"
}
},
"publish": "github"
}
}

0 comments on commit f5afafd

Please sign in to comment.