Electron增量更新(兼容win7)

增量更新(兼容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)
   }
 }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卤鸽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值