ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Electron增量更新(兼容win7)

2021-07-11 12:01:34  阅读:233  来源: 互联网

标签:localresourcePath fs const app win7 send 兼容 Electron webContents


增量更新(兼容win7)

  • 服务器端

-latest.yml
-resources/app的文件夹打包app.zip

  • app.text -> 版本号记录,放在pulic文件夹下
1.1.9
  • module ->手动安装解压依赖,防止依赖冲突以及安装失败
  • unzipper https://gitee.com/mirrors_silverwind/node-unzipper.git
  • iconv-lite https://gitee.com/mirrors_addons/iconv-lite.git
  • 增量更新函数,background.js
import axios from 'axios'
/** 版本对比*/
const upDateUrl = '服务器地址'
const upDate = () => {
  // const currentVersion = app.getVersion()
  const fs = require('fs')
  // eslint-disable-next-line space-before-function-paren
  // eslint-disable-next-line no-undef
  const currentVersion = fs.readFileSync(`${__static}/app.txt`, 'utf-8')
  axios({
    url: upDateUrl + '/latest.yml',
    method: 'POST'
  }).then(res => {
    const remoteVersion = JSON.stringify(res.data).split('\\n')[0].split(' ')[1]
    const remoteVersionArr = remoteVersion.split('.')
    const currentVersionArr = currentVersion.split('.')
    win.webContents.send('clg', `客户端版本${currentVersion}`)
    win.webContents.send('clg', `服务器版本${remoteVersion}`)
    if (Number(remoteVersionArr[0]) > Number(currentVersionArr[0])) {
      // 开启全量更新
      console.log('开启全量更新')
      updateHandle()
    } else if (Number(remoteVersionArr[2]) > Number(currentVersionArr[2]) || Number(remoteVersionArr[1]) > Number(currentVersionArr[1])) {
      // 开启增量更新
      console.log('开启增量更新')
      win.webContents.send('clg', `开启增量更新`)
      win.webContents.send('incrementalUpDate', '')
      incrementalUpDate(remoteVersion)
    } else {
      console.log('无版本变动,不更新')
    }
  })
}
/** 开启增量更新*/
/**只兼容win10可放开注释*/
const incrementalUpDate = (remoteVersion) => {
  axios({
    url: upDateUrl + '/app.zip',
    method: 'POST',
    responseType: 'stream'
  }).then(res => {
    // eslint-disable-next-line no-undef
    const fs = require('fs')
    const path = require('path')
    const localresourcePath = path.join(__dirname, '../')
    win.webContents.send('clg', `本地地址${localresourcePath}`)
    try {
      // if (fs.existsSync(localresourcePath + 'app.back')) { // 删除旧备份
      //   deleteFolder(localresourcePath + 'app.back')
      // }
      // if (fs.existsSync(localresourcePath + 'app')) {
      //   fs.renameSync(localresourcePath + 'app', localresourcePath + 'app.back') // 备份目录
      // }
      const writeStream = fs.createWriteStream(localresourcePath + 'app.zip')
      res.data.pipe(writeStream)
      writeStream.on('close', () => {
        win.webContents.send('clg', `资源包写入成功`)
        try {
          const unzipper = require('../module/node-unzipper')
          fs.createReadStream(localresourcePath + 'app.zip')
            .pipe(unzipper.Extract({ path: localresourcePath }))
          win.webContents.send('clg', `资源包解压成功`)
          // eslint-disable-next-line no-undef
          fs.writeFileSync(`${__static}/app.txt`, remoteVersion)
          setTimeout(() => {
            app.relaunch() // 重启
            app.exit(0)
          }, 1800)
        } catch (error) {
          win.webContents.send('clg', `资源包解压失败, ${error}`)
          // if (fs.existsSync(localresourcePath + 'app.back')) {
          //   fs.renameSync(localresourcePath + 'app.back', localresourcePath + 'app')
          // }
        }
      })
    } catch (error) {
      win.webContents.send('clg', `资源包写入错误, ${error}`)
      // if (fs.existsSync(localresourcePath + 'app.back')) {
      //   fs.renameSync(localresourcePath + 'app.back', localresourcePath + 'app')
      // }
    }
  })
}

/**记录fs删除方法*/
 const deleteFolder = (path) => {
   var files = []
   const fs = require('fs')
   if (fs.existsSync(path)) {
     files = fs.readdirSync(path)
     // eslint-disable-next-line space-before-function-paren
     files.forEach(function (file, index) {
       var curPath = path + '/' + file
       if (fs.statSync(curPath).isDirectory()) { // recurse
         deleteFolder(curPath)
       } else { // delete file
         fs.unlinkSync(curPath)
      }
     })
     fs.rmdirSync(path)
   }
 }

标签:localresourcePath,fs,const,app,win7,send,兼容,Electron,webContents
来源: https://blog.csdn.net/fxg1997/article/details/118652382

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有