'Electron-builder (NSIS): do not ask about deleting data on reinstall (cannot distinguish reinstall and uninstall)

I use Electron-builder (of version 22.14.13) to create installer for our app (Windows).

I needed to create custom installer, so when user uninstalls the app the dialog appears if he wants to remove app data either or not.

The problem was that in case when user reinstalls app (installing over existing one) he shouldn't be asked about that and app data should be saved. Based on answers of people with similar issues (#5633, #4141) I've come to the next solution. I customized macros with following script:

!macro customInit
  #If app is installed over previous version we shouldn't ask whether to delete app data or not
  StrCpy $1 "install"
!macroend

!macro customUnInit
  SetSilent normal
!macroend

!macro customUnInstall
  ${if} $1 == "install"
    Goto done
  ${EndIf}

  MessageBox MB_YESNO "Delete application data?" \
    /SD IDNO IDNO Skipped IDYES Accepted

  Accepted:
    !ifdef APP_PRODUCT_FILENAME
      RMDir /r "$APPDATA\${APP_PRODUCT_FILENAME}"
    !endif
    Goto done
  Skipped:
    Goto done
  done:
!macroend

So we use a variable to distinguish when app is reinstalled and when it's uninstalled. It was working until we updated electron-builder (previous version 22.11.7) and electron (previous version was 13.1.6). The problem now is that variable doesn't store value "install" on reinstall and the dialog always appears, on reinstall too. Any help would be great.



Solution 1:[1]

The problem was solved with a flag isUpdated which is mentioned in documentation. (https://www.electron.build/configuration/nsis.html)

So updated script looks like this:

!macro customUnInstall
  # when App is updated we want to preserve user data by default
  ${if} ${isUpdated}
    Goto done
  ${endIf}


  MessageBox MB_YESNO "Delete application data?" \
    /SD IDNO IDNO Skipped IDYES Accepted

  Accepted:
    !ifdef APP_PRODUCT_FILENAME
      RMDir /r "$APPDATA\${APP_PRODUCT_FILENAME}"
    !endif
    Goto done
  Skipped:
    Goto done
  done:
!macroend

So simple. I tried to use it some time ago and by some reason it wasn't working. Not it works and we don't need to use variable!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Danil Frolov