当前位置:   article > 正文

Vue+Electron学习系列 (四) -- 自动更新进阶_enoent, dev-app-update.yml not

enoent, dev-app-update.yml not

Vue+Electron学习系列

1️⃣Vue+Electron学习系列 (一) -- 初识​​​​​​​

2️⃣ Vue+Electron学习系列 (二) -- 打包发布

3️⃣ Vue+Electron学习系列 (三) -- 自动更新

4️⃣ Vue+Electron学习系列 (四) -- 自动更新进阶



前言

说明:

此篇主要介绍并实现<kbd>Electron</kbd>通过渲染进程触发其更新操作流程。


一、主进程监听

  1. // background.js
  2. 'use strict'
  3. import { app, protocol, Menu, BrowserWindow, ipcMain } from 'electron'
  4. import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
  5. import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
  6. const isDevelopment = process.env.NODE_ENV !== 'production'
  7. const log = require('electron-log');
  8. const { autoUpdater } = require("electron-updater")
  9. const path = require('path')
  10. //-------------------------------------------------------------------
  11. // Logging
  12. // +++ 此处为新增
  13. // THIS SECTION IS NOT REQUIRED
  14. //-------------------------------------------------------------------
  15. autoUpdater.logger = log;
  16. autoUpdater.logger.transports.file.level = 'info';
  17. log.info('App starting...');
  18. let win, webContents;
  19. // Scheme must be registered before the app is ready
  20. protocol.registerSchemesAsPrivileged([
  21. { scheme: 'app', privileges: { secure: true, standard: true } }
  22. ])
  23. function createWindow() {
  24. // Create the browser window.
  25. win = new BrowserWindow({
  26. width: 800,
  27. height: 600,
  28. webPreferences: {
  29. nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
  30. // +++ 此处为新增 增加预加载脚本
  31. preload: path.join(__dirname, 'preload.js')
  32. },
  33. icon: `${__static}/icon.png`
  34. })
  35. if (process.env.WEBPACK_DEV_SERVER_URL) {
  36. // Load the url of the dev server if in development mode
  37. win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
  38. if (!process.env.IS_TEST) win.webContents.openDevTools()
  39. } else {
  40. createProtocol('app')
  41. // Load the index.html when not in development
  42. win.loadURL('app://./index.html')
  43. }
  44. // +++ 此处为新增
  45. webContents = win.webContents;
  46. win.on('closed', () => {
  47. win = null
  48. })
  49. }
  50. // Quit when all windows are closed.
  51. app.on('window-all-closed', () => {
  52. if (process.platform !== 'darwin') {
  53. app.quit()
  54. }
  55. })
  56. app.on('activate', () => {
  57. if (win === null) {
  58. createWindow()
  59. }
  60. })
  61. app.on('ready', async () => {
  62. if (isDevelopment && !process.env.IS_TEST) {
  63. // Install Vue Devtools
  64. try {
  65. await installExtension(VUEJS_DEVTOOLS)
  66. } catch (e) {
  67. console.error('Vue Devtools failed to install:', e.toString())
  68. }
  69. }
  70. createWindow()
  71. })
  72. // Exit cleanly on request from parent process in development mode.
  73. if (isDevelopment) {
  74. if (process.platform === 'win32') {
  75. process.on('message', (data) => {
  76. if (data === 'graceful-exit') {
  77. app.quit()
  78. }
  79. })
  80. } else {
  81. process.on('SIGTERM', () => {
  82. app.quit()
  83. })
  84. }
  85. }
  86. // +++ 以下皆为新增内容
  87. // 主进程监听渲染进程传来的信息
  88. ipcMain.on('update', (e, arg) => {
  89. console.log(arg);
  90. checkForUpdates();
  91. });
  92. let checkForUpdates = () => {
  93. // 配置安装包远端服务器
  94. // autoUpdater.setFeedURL(feedUrl);
  95. // 下面是自动更新的整个生命周期所发生的事件
  96. autoUpdater.on('error', function(message) {
  97. sendUpdateMessage('error', message);
  98. });
  99. autoUpdater.on('checking-for-update', function(message) {
  100. sendUpdateMessage('checking-for-update', message);
  101. });
  102. autoUpdater.on('update-available', function(message) {
  103. sendUpdateMessage('update-available', message);
  104. });
  105. autoUpdater.on('update-not-available', function(message) {
  106. sendUpdateMessage('update-not-available', message);
  107. });
  108. // 更新下载进度事件
  109. autoUpdater.on('download-progress', function(progressObj) {
  110. sendUpdateMessage('downloadProgress', progressObj);
  111. });
  112. // 更新下载完成事件
  113. autoUpdater.on('update-downloaded', function(event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
  114. sendUpdateMessage('isUpdateNow');
  115. ipcMain.on('updateNow', (e, arg) => {
  116. autoUpdater.quitAndInstall();
  117. });
  118. });
  119. //执行自动更新检查
  120. autoUpdater.checkForUpdates();
  121. };
  122. // 主进程主动发送消息给渲染进程函数
  123. function sendUpdateMessage(message, data) {
  124. webContents.send('message', { message, data });
  125. }

二、预加载脚本

  1. // preload.js 如无此文件,手动创建即可
  2. // 此目的为在渲染进程中无需引入ipcRenderer即可直接使用
  3. import { ipcRenderer } from 'electron'
  4. window.ipcRenderer = ipcRenderer

三、应用参数设置

  1. // vue.config.js
  2. module.exports = {
  3. //vue配置
  4. publicPath: process.env.NODE_ENV ==='production' ? './' : '/',
  5. pluginOptions: {
  6. electronBuilder: {
  7. nodeIntegration: true,
  8. // 增加预加载配置
  9. preload: 'src/preload.js',
  10. builderOptions: {
  11. //...
  12. // 此处省略打包参数
  13. //...
  14. // 发布更新
  15. publish: [{
  16. provider: "generic",
  17. url: "http://localhost/releases/"
  18. }]
  19. }
  20. }
  21. }
  22. };

四、渲染进程触发更新检测

  1. // 本人新建一个单独vue,处理
  2. // version.vue
  3. <template>
  4. <div class="version">
  5. <h1>当前版本:{{ version }}</h1>
  6. <button @click="autoUpdate()">获取更新</button>
  7. <ol id="content">
  8. <li>生命周期过程展示</li>
  9. </ol>
  10. </div>
  11. </template>
  12. <script>
  13. import config from '../../package.json';
  14. // 因为已经设置预加载脚本,所以渲染端可直接使用window.ipcRenderer
  15. export default {
  16. data () {
  17. return {
  18. version: config.version
  19. }
  20. },
  21. mounted () {
  22. var ol = document.getElementById("content");
  23. window.ipcRenderer.on('message',(event,{message,data}) => {
  24. let li = document.createElement("li");
  25. li.innerHTML = message + " <br>data:" + JSON.stringify(data) +"<hr>";
  26. ol.appendChild(li);
  27. if (message === 'isUpdateNow') {
  28. if (confirm('是否现在更新?')) {
  29. window.ipcRenderer.send('updateNow', '立即更新');
  30. }
  31. }
  32. });
  33. },
  34. methods: {
  35. autoUpdate() {
  36. window.ipcRenderer.send('update', '更新...')
  37. }
  38. }
  39. }
  40. </script>
提示:以下是实现的最终效果

五、异常处理

提示:以下是在本地测试时遇到的一些问题,可供大家参考

1. Error: ENOENT, dev-app-update.yml not found in 

  1. // 主要是因为本地开发环境自动获取文件问题,可在主进程中增加如下判断即可:
  2. # background.js
  3. ...
  4. let checkForUpdates = () => {
  5. // 本地开发环境,改变dev-app-update.yml地址
  6. // 请注意build后生成的yml文件名
  7. if (process.env.NODE_ENV === 'development') {
  8. autoUpdater.updateConfigPath = path.join(__dirname, 'latest.yml')
  9. }
  10. ...

2. Error: Unsupported provider: undefined

  1. // vue.config.js
  2. pluginOptions: {
  3. electronBuilder: {
  4. nodeIntegration: true,
  5. preload: 'src/preload.js',
  6. builderOptions: {
  7. nsis: {
  8. + perMachine: true
  9. }
  10. }
  11. }
  12. }
  13. // packages.json
  14. {
  15. ...
  16. "author": "XXX"
  17. ...
  18. }
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号