Since writing the Electron series, many students have asked some questions, mainly about the incremental update. I have written about incremental update (2) before, but the Windows mode requires the administrator permission package, which is not very elegant, and if you want the drag and drop function, due to permissions issues, The software after lifting the right will prohibit dragging (dragging external files into the software window will have a forbidden icon), so this issue I will give a more detailed explanation of incremental update and provide temporary lifting of the right update scheme for your reference.
Mac update
Some students asked how the system under the Mac platform can be incrementally updated. In fact, the Electron ASar file under the Mac can be directly replaced, so after downloading and replacing, you just need to restart the timer.
Windows
In fact, most of our desktop software does not need administrator rights (that is, the software with shield), we found that many software are in need of update when there is a pop-up window, you click it will be temporarily lift the weight of the update, the goal of this issue is to achieve this temporary lift weight function incremental update.
Update problems
In fact, we have two difficulties with incremental updates, and these two main problems need to be overcome in order to achieve them:
- When do YOU need uAC to raise rights?
- What do I do after Electron starts
app.asar
Occupied lock problem?
Updated instructions
- Windows system has different levels of user permissions. For example, when I am an ordinary user and modify some files in disk C, there will be a popup window for you to create or modify files with administrator permissions. When the software is installed in disk C, we need administrator permissions to replace it
app.asar
File. - The locking problem, as explained in Increment 2, is handled by shutting down our Electron application and proceeding again
app.asar
File replacement.
Update steps
- Write the BAT script, execute the Electron application shutdown in the script,
app.asar
File replacement, restart Electron application, and then package the BAT script into an EXE file. - Download update.asar (the updated version of app.asar) and check whether the user software is installed on drive C. If yes, use the software
sudo-prompt
This package temporarily raises the option to execute the above exe, not: you can use node exec to execute the above exe without the option.
Update to
Simulate request for what the previous two updates have said, click here incremental update (2), look at the steps, there is 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
Copy the code
Simple explanation, % 1 and % 2 to run the script of the incoming parameters, such as update. Bat aaa, BBB, aaa for % 1 and % 2 for BBB, we execute exe incoming above, namely % 1 for resourcesPath (namely our app. Asar directory). %2 is the directory where update.asar is downloaded, %3 is the process name of the software (which can be viewed in task Manager), and %4 is the startup exe of the software. The logic here is Electron app, pause for 1 second, then delete app.asar, move update.asar to the app.asar directory, rename it app.asar, and start Electron app. Exe To Converter. Convert update. Bat To update. Exe.
Compared with the previous issue, we found that the start command was not used, but the explorer. Exe, which is the program manager of Windows system, is processed here. The packaged Electron application does not have administrator permission, but if we execute this exe after lifting the right to start the Electron application, We will find that the Electron application has administrator rights at this time, so you need to use the explorer.
sudo-prompt
NPM I sudo-prompt, sudo-prompt, sudo-prompt, sudo-prompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt, sudoPrompt
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)
})
})
}
Copy the code
// npm i https://github.com/xuxingeren/sudo-prompt
"sudo-prompt": "git+https://github.com/xuxingeren/sudo-prompt.git".Copy the code
Note: the sudo-prompt package has not been maintained for a long time, now there are some problems with the latest package, mainly the error with Chinese path, the original author has been closed and cannot mention pr, I have dealt with it, if you use it, you can fork my branch to build.address
Main process update processing
The preparation for the update and the rendering process are skipped here, see the previous two installments
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 we packed above, I put it under app.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 option of raising rights is abbreviated here
const downloads = app.getPath('downloads')
// Download update.asar and unzip it in your system's download directory
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)
If the software is installed on disk C, use sudoPrompt to execute update.exe, if not, you can directly execute update.exe
// Stop the main process in the exe and replace the ASAR installed on disk C.
// After the weights are lifted, exe will also have administrator rights after startup. Therefore, you need to lower the rights to start 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)
})
}
Copy the code
For manual debugging, you can run update.exe XXX XXX XXX XXX in CMD to pass in the updated address and check whether the execution is successful.
This series of updates can only be arranged during weekends and off-duty hours. If there are too many contents, the update will be slow. I hope it will be helpful to you
Github address: Link