下载完成并安装应用程序后,我的应用程序需要很长时间才能再次启动。应用程序本身只需要 2 秒即可打开,但更新后,即运行
autoUpdater.quitAndInstall(true,true);
后,应用程序将关闭,然后需要大约 50 秒才能再次打开。对于良好的用户体验而言,这太长了。
将 Angular 与带有 Windows 和 S3 的 NSIS 目标结合使用。
package.json
{
"name": "MyApp",
"version": "1.0.0",
"description": "My application",
"author": "Author",
"license": "ISC",
"productName": "MyApp",
"main": "main.js",
"build": {
"appId": "com.MyApp.DesktopApp",
"asar": true,
"win": {
"target": [
{
"target": "nsis",
"arch": [
"x64"
]
}
],
"icon": "assets/desktop-app-logo.ico"
},
"nsis": {
"installerIcon": "assets/logo-accent.ico",
"uninstallerIcon": "assets/logo-accent.ico",
"uninstallDisplayName": "MyApp",
"oneClick": false,
"artifactName": "MyApp-Setup-${version}.${ext}",
"allowToChangeInstallationDirectory": true,
"deleteAppDataOnUninstall": true
},
"files": [
"*.*",
"**/*",
"node_modules/electron-updater/**/*",
"node_modules/builder-util-runtime/**/*",
"node_modules/graceful-fs/**/*",
"node_modules/lazy-val/**/*",
"node_modules/lodash.escaperegexp/**/*",
"node_modules/lodash.isequal/**/*",
"node_modules/sax/**/*",
"node_modules/argparse/**/*",
"node_modules/debug/**/*",
"node_modules/fs-extra/**/*",
"node_modules/jsonfile/**/*",
"node_modules/js-yaml/**/*",
"node_modules/lodash.escaperegexp/**/*",
"node_modules/lodash.isequal/**/*",
"node_modules/lru-cache/**/*",
"node_modules/ms/**/*",
"node_modules/rxjs/**/*",
"node_modules/semver/**/*",
"node_modules/tslib/**/*",
"node_modules/typed-emitter/**/*",
"node_modules/universalify/**/*",
"node_modules/yallist/**/*",
"node_modules/electron-log/**/*",
"!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme}",
"!**/node_modules/*/{test,__tests__,tests,powered-test,example,examples}",
"!**/node_modules/*.d.ts"
],
"directories": {
"output": "../installer"
},
"publish": {
"provider": "s3",
"bucket": "myBucket",
"path": "desktop-repo",
"region": "eu-west-1",
"endpoint": "https://s3.eu-west-1.amazonaws.com"
}
},
"dependencies": {
"electron-updater": "6.1.1",
"electron-log": "4.4.8"
},
"devDependencies": {
"electron": "22.0.0",
"electron-builder": "24.6.3"
},
"scripts": {
"electron-installer-build": "electron-builder",
"electron-installer-publish": "electron-builder -p always"
}
}
注意:files 参数中的文件列表很长的原因是,Electron 构建器及其所有依赖项都会被复制到用于打包 Electron 应用程序的目录中。
main.js 中的更新程序代码
autoUpdater.on('error', (error) => {
dialog.showErrorBox('Error: ', error == null ? "unknown" : (error.stack || error).toString())
})
autoUpdater.on('update-available', () => {
dialog.showMessageBox({
type: 'info',
title: 'Found Updates',
message: 'Found updates, do you want update now?',
buttons: ['Sure', 'No']
}).then((buttonIndex) => {
if (buttonIndex === 0) {
autoUpdater.downloadUpdate()
}
else {
updater.enabled = true
updater = null
}
})
})
autoUpdater.on('update-not-available', () => {
dialog.showMessageBox({
title: 'No Updates',
message: 'Current version is up-to-date.'
})
updater.enabled = true
updater = null
})
autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({
title: 'Install Updates',
message: 'Updates downloaded, application will be quit for update...'
}).then(() => {
autoUpdater.quitAndInstall(true, true)
})
})
因此,运行
autoUpdater.quitAndInstall(true, true)
后,应用程序将关闭、卸载、安装新更新并成功重新启动。然而,完成这一切所需的时间太长了。如果我要手动卸载该应用程序,重新安装并自己启动该应用程序,则不会花费那么长时间。我可以做些什么来减少时间吗?
对于 MonsterWriter 我是这样“解决”的:
alert("The app will close now and restart automatically after the update is installed successfully. Don't be concerned; this process may take up to 1 minute.");
mw.installAndRestart();
代码在渲染进程内,mw.installAndRestart向主进程发送信号来调用执行重启:
ipcMain.handle("installAndRestart", () => {
setImmediate(() => {
autoUpdater.quitAndInstall(true, true);
});
});