0.0.7 • Published 2 years ago

@wisdomgarden/capacitor-updater v0.0.7

Weekly downloads
-
License
AGPL-3.0-only
Repository
github
Last release
2 years ago

capacitor-updater

Update capacitor app withtout store review.

You have 3 ways possible :

  • use capgo.app a full featured auto update system in 5 min Setup, to manage version, update, revert and see stats.
  • use your own server update with auto update system
  • use manual methods to zip, upload, download, from JS to do it when you want.

Community

Join the discord to get help.

Documentation

I maintain a more user friendly and complete documentation in notion site.

install plugin

npm install capacitor-updater
npx cap sync

Auto update setup

Create account in capgo.app and get your API key

  • Download the CLI npm i -g capgo
  • Add app from CLI capgo add -a API_KEY
  • Upload app capgo upload -a API_KEY
  • Upload app capgo set -a API_KEY -s public
  • Edit your capacitor.config.json like below, set autoUpdateUrl with the url printed in the previous step.
// capacitor.config.json
{
	"appId": "**.***.**",
	"appName": "Name",
	"plugins": {
		"CapacitorUpdater": {
			"autoUpdateUrl": "https://capgo.app/api/latest?appid=**.****.***&channel=dev"
		}
	}
}
  • Add to your main code
  import { CapacitorUpdater } from 'capacitor-updater'
  CapacitorUpdater.notifyAppReady()
  // To let auto update know you app boot well.
  • Do npm run build && npx cap copy to copy the build to capacitor.
  • Run the app and see app auto update after each backgrounding.
  • If update fail it will roolback to previous version.

See more there in the Auto update documentation.

Manual setup

Download app update from url when user enter the app install it when user background the app.

In your main code :

  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/Cap-go/demo-app/releases/download/0.0.4/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/Cap-go/demo-app/releases/download/0.0.4/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 reinstalls it.

If you need more secure way to update your app, you can use Auto update system.

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

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/ or {project directory}/www/. 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; version: string; }) => Promise<{ version: string; }>

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

ParamType
options{ url: string; version: string; }

Returns: Promise<{ version: string; }>


set(...)

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

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

ParamType
options{ version: string; autoReload?: boolean; }

delete(...)

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

Delete version in storage

ParamType
options{ version: string; }

list()

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

Get all available versions

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


reset(...)

reset(options: { autoReload: boolean; }) => Promise<void>

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

ParamType
options{ autoReload: boolean; }

current()

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

Get the current version, if none are set it returns builtin

Returns: Promise<{ serverBasePath: string; pathPersist: string; }>


reload()

reload() => Promise<void>

Reload the view


Listen to download events

  import { CapacitorUpdater } from 'capacitor-updater';

CapacitorUpdater.addListener('download', (info: any) => {
  console.log('download was fired', info.percent);
});

On iOS, Apple don't allow you to show a message when the app is updated, so you can't show a progress bar.

Inspiration

Contributors

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