需求:利用eletron-builder打包程序時發(fā)現(xiàn)涕滋,每次安裝完后需要手動打開安裝目錄,找到配置文件修改配置參數(shù)宾肺,比較麻煩,也不夠帥氣丰刊,能否在安裝時就提示用戶輸入?yún)?shù)呢增拥。
nsis(一個開源的 Windows 系統(tǒng)下安裝程序制作程序),electron-builder提供了windows下nsis參數(shù)配置:https://www.electron.build/configuration/nsis.html#custom-nsis-script
package.json配置如下:
image.png
image.png
installer.nsh編寫自定義圖形界面腳本:
!include nsDialogs.nsh
!include LogicLib.nsh
#OutFile nsDialogs.exe
#RequestExecutionLevel user
#ShowInstDetails show
Var Dialog
Var name
Var entry
Var website
Var openMode
Page custom pgPageCreate pgPageLeave
Function pgPageCreate
nsDialogs::Create 1018
Pop $Dialog
${If} $Dialog == error
Abort
${EndIf}
${NSD_CreateGroupBox} 10% 10u 80% 100u "Settings"
Pop $0
${NSD_CreateLabel} 20% 26u 20% 10u "name:"
Pop $0
${NSD_CreateText} 40% 24u 40% 12u "electron"
Pop $name
${NSD_CreateLabel} 20% 40u 20% 10u "entry:"
Pop $0
${NSD_CreateText} 40% 38u 40% 12u "https://www.baidu.com"
Pop $entry
${NSD_CreateLabel} 20% 54u 20% 10u "website:"
Pop $0
${NSD_CreateText} 40% 52u 40% 12u "https://www.baidu.com"
Pop $website
${NSD_CreateLabel} 20% 68u 20% 10u "openMode:"
Pop $0
${NSD_CreateText} 40% 66u 40% 12u "false"
Pop $openMode
nsDialogs::Show
FunctionEnd
Function PgPageLeave
${NSD_GetText} $name $0
${NSD_GetText} $entry $1
${NSD_GetText} $website $2
${NSD_GetText} $openMode $3
FileOpen $9 $DESKTOP\.electron w
FileWrite $9 "{name:$0,entry:$1,website:$2,openMode:$3}"
FileClose $9
SetFileAttributes $DESKTOP\.electron NORMAL
FunctionEnd
Section
SectionEnd
效果如下:
image.png
在pageLeave的時候猾封,將用戶輸入的參數(shù)寫入到配置文件(.electron)中,electron啟動后逾苫,在主進(jìn)程中讀取.electron枚钓,獲取參數(shù)。