Skip to content

Commit

Permalink
Automatic updater [DON'T MERGE] (#84)
Browse files Browse the repository at this point in the history
Check updates and log
---------

Signed-off-by: marcopiraccini <[email protected]>
  • Loading branch information
marcopiraccini authored Dec 12, 2023
1 parent 0b8f463 commit 1f09f3f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
4 changes: 1 addition & 3 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,4 @@ appImage:
artifactName: ${productName}-${version}.${ext}
npmRebuild: false
publish:
provider: github
private: true

provider: github
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@platformatic/ui-components": "^0.1.126",
"autoprefixer": "^10.4.16",
"boring-name-generator": "^1.0.3",
"electron-log": "^5.0.1",
"electron-updater": "^6.1.7",
"execa": "^5.1.1",
"mkdirp": "^3.0.1",
Expand Down
16 changes: 11 additions & 5 deletions src/main/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import icon from '../../resources/icon.png?asset'
import setupMenu from './menu.mjs'
import { getTemplates, getPlugins } from './client.mjs'
import { prepareFolder, createApp } from './generate.mjs'
import log from 'electron-log'

log.transports.file.level = 'info'
log.info('App starting...')

const generate = require('boring-name-generator')

// eslint-disable-next-line no-unused-vars
Expand All @@ -28,7 +33,8 @@ const elaborateLine = (...args) => {
})
return line
}
const logger = {}

const uiLogger = {}
function createWindow () {
const mainWindow = new BrowserWindow({
minWidth: 1440,
Expand Down Expand Up @@ -60,10 +66,10 @@ function createWindow () {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}

logger.error = function (args) {
uiLogger.error = function (args) {
mainWindow.webContents.send('log', { level: 'error', message: elaborateLine(args) })
}
logger.info = function (...args) {
uiLogger.info = function (...args) {
mainWindow.webContents.send('log', { level: 'info', message: elaborateLine(...args) })
}

Expand Down Expand Up @@ -107,11 +113,11 @@ app.whenReady().then(() => {
})

ipcMain.handle('prepare-folder', async (_, path, templates, appName) => {
return prepareFolder(path, templates, logger, appName)
return prepareFolder(path, templates, uiLogger, appName)
})

ipcMain.handle('create-app', async (_, path, project) => {
return createApp(path, project, logger)
return createApp(path, project, uiLogger)
})

ipcMain.handle('quit-app', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/main/menu.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app, Menu } from 'electron'
import { checkForUpdates } from './updater.mjs'

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

Expand Down Expand Up @@ -63,6 +64,10 @@ const template = [
{
role: 'help',
submenu: [
{
label: 'Check for Updates',
click: checkForUpdates
},
{
label: 'Learn More',
click: async () => {
Expand Down
58 changes: 58 additions & 0 deletions src/main/updater.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { dialog } from 'electron'
import { autoUpdater } from 'electron-updater'
import log from 'electron-log'

let updater
autoUpdater.autoDownload = false
log.transports.console.level = 'info'
autoUpdater.logger = log

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

autoUpdater.on('update-available', async () => {
log.info('Update available')
const { response } = await dialog.showMessageBox({
type: 'info',
title: 'Found Meraki Update',
message: 'Found update, do you want update now?',
buttons: ['Sure', 'No']
})
if (response === 0) {
log.info('Downloading now...')
autoUpdater.downloadUpdate()
} else {
updater.enabled = true
updater = null
}
})

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

autoUpdater.on('update-downloaded', async () => {
log.info('Update downloaded')
await dialog.showMessageBox({
title: 'Install Updates',
message: 'Updates downloaded, application will quit to apply the update'
})
autoUpdater.quitAndInstall()
})

// export this to MenuItem click callback
export function checkForUpdates (menuItem, focusedWindow, event) {
updater = menuItem
updater.enabled = false
log.info('Checking for updates...')
autoUpdater.checkForUpdates()
log.info('Checking for updates...done')
}

0 comments on commit 1f09f3f

Please sign in to comment.