Electron-Builder 打包Nsis,安裝後自動運行程序

需求,想要的功能

  • 默認 Program Files 或 Program Files(x86)
  • 可以選擇安裝目錄
  • 安裝後自動運行程序

electorn-builder nsis配置,如下:

{
    oneClick: false,
    perMachine: true,
    allowElevation: true,
    allowToChangeInstallationDirectory: true,
}

問題

查看electron-builder源碼中的nsis腳本,設置oneClick 爲 false後,是不能自動運行的。
當electron-builder 提供了自定義腳本的方式,我們可以自已定義nsis腳本來處理。
文檔地址:https://www.electron.build/configuration/nsis#custom-nsis-script

腳本 (installer.nsh)

!macro customFinishPage
AutoCloseWindow true
Function StartApp
    ${if} ${isUpdated}
      StrCpy $1 "--updated"
    ${else}
      StrCpy $1 ""
    ${endif}
    ${StdUtils.ExecShellAsUser} $0 "$launchLink" "open" "$1"
FunctionEnd

Function .onInstSuccess
    Call StartApp
FunctionEnd

!macroend

把上面的腳本默認放到 build/install.nsh 中,重新打包,就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章