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)
   }
 }
上一篇:麒麟常见问题


下一篇:Electron集成到现有项目的实战