3
votes

With electron-builder NSIS installer we're able to create an executable installer which launches the installed electron app immediately after the install is finished. My question is is there a way to pass any command line parameters the installer itself was launched with to the installed app on this first launch?

I have seen some NSIS custom scripts which suggest that an executable can be launched with Exec, and the installer parameters can be retrieved with GetParameters. Is that a recommended direction, or is there some configuration option either in electron-builder or NSIS?

EDIT:

Here is a possible solution:

  • set nsis.runAfterFinish electron-builder option to false (true is the default);
  • implement customInstall event handler to customize the normal electron-builder provided template:

    !macro StartAppWithParameters
        Var /GLOBAL startAppWithParametersArgs
        ${if} ${isUpdated}
            StrCpy $startAppWithParametersArgs "--updated"
        ${else}
            StrCpy $startAppWithParametersArgs ""
        ${endif}
        ${StdUtils.GetAllParameters} $R0 0
        ${StdUtils.ExecShellAsUser} $0 "$launchLink" "open" '$startAppWithParametersArgs $R0'
    !macroend
    
    !macro customInstall
        HideWindow
        !insertmacro StartAppWithParameters
    !macroend
    

Details are in electron-builder NSIS configuration, and electron-builder NSIS template

Thanks!

1

1 Answers

0
votes

Yes you can do it manually with Exec and GetParameters:

!include "FileFunc.nsh"
!include "MUI2.nsh"

!macro RunWithInstallersParameters app
Push "${app}"
Call RunWithInstallersParameters
!macroend
Function RunWithInstallersParameters
Exch $0
Push $1
${GetParameters} $1
Exec '"$0" $1'
Pop $1
Pop $0
FunctionEnd

!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_FUNCTION MyFinishRun
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE English

Function MyFinishRun
!insertmacro RunWithInstallersParameters "$sysdir\Calc.exe"
FunctionEnd

Section
SetOutPath $InstDir
File "blahblah"
!insertmacro RunWithInstallersParameters "$windir\Notepad.exe"
SectionEnd

The MUI Finish page also supports a way to specify the parameters directly but since we don't know the parameters at compile time we have to use a variable:

!include "FileFunc.nsh"
!include "MUI2.nsh"

!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN "$windir\Notepad.exe"
!define MUI_FINISHPAGE_RUN_PARAMETERS $1 ; Initialized by our MUI_PAGE_CUSTOMFUNCTION_SHOW function
!define MUI_PAGE_CUSTOMFUNCTION_SHOW InitFinishPage
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE English

Function InitFinishPage
${GetParameters} $1
FunctionEnd

I don't know anything about electron-builder but I assume there is a way for you to customize the NSIS script somehow.