Skip to content

playaz87/capacitor-updater

 
 

Repository files navigation

capacitor-updater

Download app update from url and install it.

And reload the view.

You can list the version and manage it with the command below.

Community

Join the discord to get help : https://discord.gg/VnYRvBfgA6

Install

npm install capacitor-updater
npx cap sync
  import { CapacitorUpdater } from 'capacitor-updater'
  import { SplashScreen } from '@capacitor/splash-screen'
  import { App } from '@capacitor/app'

  let version = ""
  App.addListener('appStateChange', async(state) => {
      if (state.isActive) {
        // Do the download during user active app time to prevent failed download
        version = await CapacitorUpdater.download({
        url: 'https://github.com/Forgr-ee/Mimesis/releases/download/0.0.1/dist.zip',
        })
      }
      if (!state.isActive && version !== "") {
        // Do the switch when user leave app
        SplashScreen.show()
        try {
          await CapacitorUpdater.set(version)
        } catch () {
          SplashScreen.hide() // in case the set fail, otherwise the new app will have to hide it
        }
      }
  })

  // or do it when click on button
  const updateNow = async () => {
    const version = await CapacitorUpdater.download({
      url: 'https://github.com/Forgr-ee/Mimesis/releases/download/0.0.1/dist.zip',
    })
    // show the splashscreen to let the update happen
    SplashScreen.show()
    await CapacitorUpdater.set(version)
    SplashScreen.hide() // in case the set fail, otherwise the new app will have to hide it
  }

Be extra carufull for your update if you send a broken update, the app will crash until the user uninstall it.

Packaging dist.zip

Whatever you choose to name the file you download from your release/update server URL, the zip file should contain the full contents of your production Capacitor build output folder, usually {project directory}/dist/. This is where index.html will be located, and it should also contain all bundled JavaScript, CSS, and web resources necessary for your app to run.

Do not password encrypt this file, or it will fail to unpack.

API

download(...)

download(options: { url: string; }) => Promise<{ version: string; }>

Download new version from url, it should be a zip file, with files inside or with a unique folder inside with all your files

Param Type
options { url: string; }

Returns: Promise<{ version: string; }>


set(...)

set(options: { version: string; versionName?: string; }) => Promise<void>

Set version as current version, set will return error if there are no index.html file inside the version folder, versionName is optional and it's a custom value who will be saved for you

Param Type
options { version: string; versionName?: string; }

delete(...)

delete(options: { version: string; }) => Promise<void>

Delete version in storage

Param Type
options { version: string; }

list()

list() => Promise<{ versions: string[]; }>

Get all avaible versions

Returns: Promise<{ versions: string[]; }>


reset()

reset() => Promise<void>

Set the original version (the one sent to Apple store / Google play store ) as current version


current()

current() => Promise<{ current: string; }>

Get the curent version, if none are set it return 'default'

Returns: Promise<{ current: string; }>


reload()

reload() => Promise<void>

Reload the view


versionName()

versionName() => Promise<{ versionName: string; }>

Get the version name, if it was set during the set phase

Returns: Promise<{ versionName: string; }>


notifyAppReady()

notifyAppReady() => Promise<void>

Notify native plugin that the update is working, only in auto update


delayUpdate()

delayUpdate() => Promise<void>

Skip update in the next app backgrounding, only in auto update


cancelDelay()

cancelDelay() => Promise<void>

allow update in the next app backgrounding, only in auto update


Inspiraton

Contributer

jamesyoung1337 Thanks a lot for your guidance and support, it was impossible to make this plugin work without you.

Releases

No releases published

Packages

No packages published

Languages

  • Java 46.0%
  • Swift 38.9%
  • TypeScript 9.0%
  • Objective-C 2.3%
  • Ruby 1.8%
  • JavaScript 0.9%
  • Other 1.1%