自定义安装卸载需要的了解NSIS脚本,electron-builder使用的就是NSIS.文档可查 https://nsis.sourceforge.io/Docs/
配置打包,在vue.config.js的pluginOptions中配置打包项include
electronBuilder: {
builderOptions: {
nsis: {
include: path.resolve(__dirname, 'src/custom.nsh'),
warningsAsErrors: false
},
}
},
electron-builder提供了一些生命周期方便自定义安装卸载,文档 https://www.electron.build/configuration/nsis#custom-nsis-script
customHeader, preInit, customInit, customUnInit, customInstall, customUnInstall, customRemoveFiles, customInstallMode, customWelcomePage.
在生命周期中添加nsis脚本内容,这是我自定义卸载的脚本,会在用户卸载删除文件的时候触发
!macro customRemoveFiles
# 提示窗
MessageBox MB_OKCANCEL "是否删除用户数据?(注意,删除用户数据后,软件需要重新激活,请谨慎选择!)" IDOK label_ok IDCANCEL label_cancel
label_ok:
# 删除已经下载的更新包
RMDir /r $PROFILE\\AppData\\Local\\bd3-command-system-updater\\*.*
# 删除安装目录
RMDir /r $INSTDIR\\*.*
Goto end
label_cancel:
RMDir /r $INSTDIR\\locales
RMDir /r $INSTDIR\\swiftshader
RMDir /r $INSTDIR\\resources\\extraResources\\server\\bat
RMDir /r $INSTDIR\\resources\\extraResources\\server\\bat
RMDir /r $INSTDIR\\resources\\extraResources\\server\\jre8
RMDir /r $INSTDIR\\resources\\extraResources\\server\\redis
RMDir /r $INSTDIR\\resources\\extraResources\\server\\system
Delete $INSTDIR\\resources\\extraResources\\server\\start.bat
Delete $INSTDIR\\resources\\extraResources\\server\\stop.bat
Delete $INSTDIR\\resources\\extraResources\\server\\端口9002-9003-9004.txt
Delete $INSTDIR\\resources\\app.asar
Delete $INSTDIR\\resources\\elevate.exe
Delete $INSTDIR\\resources\\app-update.yml
Delete "$INSTDIR\\*.pak"
Delete "$INSTDIR\\*.dll"
Delete "$INSTDIR\\*.bin"
Delete "$INSTDIR\\*.dat"
Delete "$INSTDIR\\*.exe"
Delete "$INSTDIR\\*.json"
Delete "$INSTDIR\\*.html"
Delete "$INSTDIR\\*.txt"
Goto end
end:
!macroend