1
Ola I’m trying to make a system of automatic updates using the autoupdater of Electron with Vue JS, a while ago I made this program the same way and it worked, nowadays it doesn’t work anymore.
Code of my component in Vue:
<template>
<div class="hello">
<h1>Ola mundo</h1>
<span>Versão: </span>{{versao}}
<div id="notification" class="hidden">
<p id="message"></p>
<button id="close-button" @click="closeNotification()">
Fechar
</button>
<button id="restart-button" @click="restartApp()" class="hidden">
Reiniciar
</button>
</div>
</div>
</template>
<script>
const { ipcRenderer } = window.require('electron')
export default {
props: {
msg: String
},
data: function(){
return{
versao: "0.0.1"
}
},
methods:{
restartApp(){
ipcRenderer.send('restart_app');
},
closeNotification(){
const notification = document.getElementById('notification');
notification.classList.add('hidden');
}
},
mounted(){
const message = document.getElementById('message');
const restartButton = document.getElementById('restart-button');
const notification = document.getElementById('notification');
ipcRenderer.send('app_version');
ipcRenderer.on('app_version', (event, arg) => {
ipcRenderer.removeAllListeners('app_version');
this.versao = arg.version;
});
ipcRenderer.on('update_available', () => {
ipcRenderer.removeAllListeners('update_available');
message.innerText = 'Nova atualização disponivel. Realizando download...';
notification.classList.remove('hidden');
});
ipcRenderer.on('update_downloaded', () => {
ipcRenderer.removeAllListeners('update_downloaded');
message.innerText = 'Atualização baixada. Para instalar reinicie o aplicativo. Reiniciar agora?';
restartButton.classList.remove('hidden');
notification.classList.remove('hidden');
});
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
#notification {
position: fixed;
bottom: 20px;
left: 20px;
width: 200px;
padding: 20px;
border-radius: 5px;
background-color: white !important;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
color: black !important;
}
.hidden {
display: none;
}
</style>
the background.js of Electron:
'use strict'
import { app, protocol, BrowserWindow, ipcMain } from 'electron'
import {autoUpdater} from 'electron-updater'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
const isDevelopment = process.env.NODE_ENV !== 'production'
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])
async function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
createProtocol('app')
// Load the index.html when not in development
win.loadURL('app://./index.html')
}
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
ipcMain.on('app_version', (event) => {
event.sender.send('app_version', { version: app.getVersion() });
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installExtension(VUEJS_DEVTOOLS)
} catch (e) {
console.error('Vue Devtools failed to install:', e.toString())
}
}
createWindow()
autoUpdater.checkForUpdatesAndNotify();
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
})
}
}
autoUpdater.on('update-available', () => {
console.log("Atualização disponivel")
win.webContents.send('update_available');
});
autoUpdater.on('update-downloaded', () => {
console.log("Atualização baixada")
win.webContents.send('update_downloaded');
});
ipcMain.on('restart_app', () => {
autoUpdater.quitAndInstall();
});
I added Electron’s background.js in ready
autoUpdater.checkForUpdatesAndNotify();
And these other lines to communicate with the VUE
autoUpdater.on('update-available', () => {
console.log("Atualização disponivel")
win.webContents.send('update_available');
});
autoUpdater.on('update-downloaded', () => {
console.log("Atualização baixada")
win.webContents.send('update_downloaded');
});
ipcMain.on('restart_app', () => {
autoUpdater.quitAndInstall();
});
in my package.json I have a script to send the release to my github
"electron:deploy": "vue-cli-service electron:build --win --publish always",
So I can send the files through the script to Github everything straight, but when you open the program and it will get the updates, nothing happens. I ran the program by cmd to see what was returning and he returned it to me: