Electron incremental update

Posted by tjmbc on Mon, 20 Dec 2021 09:32:26 +0100

Since writing the electron series, many students have asked some questions, mainly for the incremental update of electron. I wrote it before Incremental update (II) However, the Windows mode requires the administrator permission package, which is not very elegant. Moreover, if you want to drag the function, due to the permission problem, the drag of the software after authorization will be prohibited (there will be a prohibition icon when dragging external files into the software window). In this issue, I will give a more detailed description of incremental update and provide a scheme for temporary authorization update for your reference.

Mac update

Some students asked how the system under the Mac platform was incrementally updated. In fact, the asar file of Electron under the Mac can be directly replaced, so after downloading and replacing, just start a timer and restart it.

Windows

In fact, most of our desktop software do not need administrator permission (that is, the software is equipped with a shield). We found that many software will have a pop-up window when it needs to be updated. After you click it, you will update it temporarily. The goal of this issue is to realize the incremental update of this temporary right lifting function.

Update problem

In fact, we have two difficulties in incremental update. To achieve this, we have to overcome these two main problems:

  1. When do you need uac to raise the right?
  2. How to deal with the app after the Electron application starts Is ASAR occupied and locked?

Update description

  1. Windows system has different user permission levels. For example, I am an ordinary user. When modifying files in some folders on Disk c, there will be a pop-up window for you to create or modify with administrator permission. When the software is installed on Disk c, we need administrator permission to replace the app ASAR file.
  2. As for the locking problem, it has been explained in increment 2. The processing method is to close our Electron application and then conduct app Replacement of ASAR files.

Update steps

  1. Write a bat script and close the Electron application in the script, app Replace the ASAR file, restart the Electron application, and then package the bat script into an exe file.
  2. Download update ASAR (updated version of app.asar) determines whether the user software is installed on disk c. It is: use the sudo prompt package to temporarily raise the right to execute the above exe, not: you can directly use node's exec to execute the above exe without raising the right.

Update preparation

The first two updates of simulation request have been said. Click here Incremental update (II) , take a look at the steps, and there will be no more explanation here.

Build exe

@echo off
taskkill /f /im %3
timeout /T 1 /NOBREAK
del /f /q /a %1\app.asar
move %2\update.asar %1
ren %1\update.asar app.asar
explorer.exe %4

For a brief explanation, the parameters passed in by% 1 and% 2 for running the script, such as update Bat aaa bbb, then% 1 is aaa and% 2 is bbb, which is passed in when we execute exe above,
That is,% 1 is the resourcesPath (that is, the directory where our app.asar is located),% 2 is the directory where update.asar is downloaded,% 3 is the process name of the software (which can be viewed in the task manager),% 4 is the startup exe of the software.
The logic here is the Electron application, pause for 1 second, and then delete the app ASAR, update ASAR moves to app Under ASAR directory, rename it app ASAR, start the Electron application.
Download Bat To Exe Converter to update Bat to update Exe, please refer to the detailed description of the previous two issues for detailed operation.

Compared with the previous issue, we found that instead of using the start command, we used explorer Exe, which is the program manager of the Windows system. The processing here is that the packaged Electron application does not have administrator privileges. However, if we execute this exe to start the Electron application after raising the right, we will find that the Electron application also has administrator privileges, so we need to use explorer Exe is started with reduced power.

sudo-prompt

Install sudo prompt, NPM I sudo prompt and sudo prompt. The package is similar to the exec command of node. When executing, a permission raising pop-up window will appear. After confirmation, the user will execute the command with administrator permission to create sudoprompt js:

var sudo = require('sudo-prompt')
var options = {
  name: 'Electron',
}

export default (shell) => {
  return new Promise((resolve, reject) => {
    sudo.exec(shell, options, function(error, stdout, stderr) {
      if (error) {
        reject(error)
        console.log('error:' + error)
        return
      }
      resolve(stdout)
      console.log('stdout: ' + stdout)
    })
  })
}
// npm i https://github.com/xuxingeren/sudo-prompt
"sudo-prompt": "git+https://github.com/xuxingeren/sudo-prompt.git",

Note: sudo prompt has not been maintained for a long time. There are still some problems with the latest package. The main problem is that there will be errors with the Chinese path. The original author has closed it. I can't mention pr. I've handled it here. If you use it, you can fork my branch to build it, address

Main process update processing

The preparation before the update and the rendering process processing are omitted here. See the previous two issues

import downloadFile from './downloadFile'
import sudoPrompt from './sudoPrompt'
import { app } from 'electron'
const fse = require('fs-extra')
const path = require('path')
const AdmZip = require('adm-zip')
import log from '../config/log.js'

export default async (data) => {
  const resourcesPath = process.resourcesPath
  // Download the update exe packaged above. I put it in the app Under getpath ('userdata '), other locations can also be used
  if (!fse.pathExistsSync(path.join(app.getPath('userData'), './update.exe'))) {
    await downloadFile({
      url: data.upDateExe,
      targetPath: app.getPath('userData')
    }).catch((err) => {
      console.log(err)
      log.info(err)
    })
  }
  // The proposal for raising rights is abbreviated here
  const downloads = app.getPath('downloads')
  // Download update ASAR, unzip (in fact, you can also unzip) in the download directory of the system
  downloadFile({ url: data.upDateUrl, targetPath: downloads })
    .then(async (filePath) => {
      const zip = new AdmZip(filePath)
      zip.extractAllToAsync(downloads, true, (err) => {
        if (err) {
          console.error(err)
          return
        }
        fse.removeSync(filePath)
        // You can add judgment here. If the software is installed on Disk c, use sudoPrompt to upgrade and execute update Exe. If not, you can directly execute update.exe exe
        // Temporarily lift the authority to run exe, close the main process in exe, and replace asar installed on Disk c (lift the authority to handle disk c. If you install other disks, you can directly run exe with node.exec to replace it)
        // Because the EXE after authorization is raised opens electron, it will also have administrator permission after startup, so it needs to be started by lowering the permission, explorer exe
        sudoPrompt(
          `"${path.join(
            app.getPath('userData'),
            './update.exe'
          )}" "${resourcesPath}" ${downloads} "${
            process.env.VUE_APP_PRODUCTNAME
          }.exe" "${app.getPath('exe')}"`
        )
      })
    })
    .catch((err) => {
      log.info(err)
      console.log(err)
    })
}

For manual debugging, you can execute update in cmd Exe XXX passes in the corresponding updated address to see whether the execution is successful.

This series of updates can only be sorted out on weekends and after work time. If there are more contents, the update will be slow. I hope it can be helpful to you. Please give more star or like collection support

Address: link
github address: link

Topics: Front-end Vue Vue-cli Electron