Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Oct 15, 2024
1 parent 840fd68 commit 80e1567
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dev-app-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
owner: rafaellehmkuhl
repo: cockpit-public
provider: github
81 changes: 81 additions & 0 deletions electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app, BrowserWindow, protocol, screen } from 'electron'
import electronUpdater, { type AppUpdater } from 'electron-updater'
import { join } from 'path'

export const ROOT_PATH = {
Expand Down Expand Up @@ -36,6 +37,24 @@ function createWindow(): void {
}
}

// Function to log messages to the testSpan element
/**
* Log a message to the testSpan element in the main window
* @param {string} message - The message to log
* @returns {void}
*/
function internalLog(message: string): void {
if (!mainWindow || mainWindow.isDestroyed()) {
console.log('Main window is not available.')
return
}

console.log(message)
mainWindow.webContents.executeJavaScript(`
console.log('${message}')
`)
}

app.on('window-all-closed', () => {
console.log('Closing application.')
mainWindow = null
Expand All @@ -62,6 +81,68 @@ protocol.registerSchemesAsPrivileged([

app.whenReady().then(createWindow)

/**
* Get auto updater instance
* @returns {AppUpdater}
* @see https://www.electron.build/auto-update
*/
function getAutoUpdater(): AppUpdater {
// Using destructuring to access autoUpdater due to the CommonJS module of 'electron-updater'.
// It is a workaround for ESM compatibility issues, see https://github.com/electron-userland/electron-builder/issues/7976.
const { autoUpdater } = electronUpdater
autoUpdater.logger = require('electron-log')
// @ts-ignore
autoUpdater.logger.transports.file.level = 'info'
return autoUpdater
}

app.whenReady().then(() => {
internalLog('Electron app is ready.')

// Check for software updates
internalLog('Will start update checking routine...')

const autoUpdater = getAutoUpdater()
autoUpdater.forceDevUpdateConfig = true

autoUpdater.on('checking-for-update', () => {
internalLog('Checking for update...')
})
autoUpdater.on('update-available', (info) => {
internalLog('Update available.')
internalLog(JSON.stringify(info))
})
autoUpdater.on('update-not-available', (info) => {
internalLog('Update not available.')
internalLog(JSON.stringify(info))
})
autoUpdater.on('error', (err) => {
internalLog('Error in auto-updater.')
internalLog(err.message)
})
autoUpdater.on('download-progress', (progressObj) => {
let log_message = 'Download speed: ' + progressObj.bytesPerSecond
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%'
log_message = log_message + ' (' + progressObj.transferred + '/' + progressObj.total + ')'
internalLog(log_message)
})
autoUpdater.on('update-downloaded', (info) => {
internalLog('Update downloaded')
internalLog(JSON.stringify(info))
})

autoUpdater
.checkForUpdates()
.then((e) => {
internalLog(e)
})
.catch((e) => {
internalLog(e)
})

internalLog(`Cockpit version: ${app.getVersion()}`)
})

app.on('before-quit', () => {
// @ts-ignore: import.meta.env does not exist in the types
if (import.meta.env.DEV) {
Expand Down

0 comments on commit 80e1567

Please sign in to comment.