npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cartona/deploy-cli

v0.0.4

Published

Deploy changes to an Ionic app for live update

Downloads

12

Readme

Cartona Live Update CLI

A replacement for AppFlow's live update feature.

You need to be granted access to Cartona's deploy-api server (by default at deploy-api.cartona.com).

You also need to add code in your Ionic app using @capgo/capacitor-updater.

Using the CLI

Start by installing the CLI globally and access it from the command line in your project folder.

npm install -g @cartona/deploy-cli

You can also install it locally and then access the deploy command from inside package.json or using npm-exec.

npm install --save-dev @cartona/deploy-cli

In your project you must also run the following:

npm install @capgo/capacitor-updater
npx cap sync

Run deploy help to learn the available commands and parameters.

Using the credentials provided to you by the server administrator run deploy login at least once.

Whenever a live update is required for an Ionic project, just follow the following steps:

ionic build
deploy upload

Commands will prompt you to provide the required parameters. Alternatively you can pass them as arguments. For example:

deploy upload app=retailer dir=./www

Save common commands and parameters in your package.json scripts.

Server management

The update-api server has its own API but most common tasks can be performed from this CLI. For example:

    deploy list builds app=retailer
    deploy list permissions app=retailer
    deploy grant username=ahmed app=retailer can_upload=1

Run deploy help to learn the available commands and parameters.

Enabling your Ionic app

First, make sure automatic updates are disabled for @capgo/capacitor-updater since we will be handling the update from our own server. Edit your capacitor.config.ts file to include the following:

  plugins: {
    CapacitorUpdater: {
		  autoUpdate: false
	}
  }

Add code that runs immediately at startup. This can be a provider, a singleton class, or a class with static methods.

For example:

export class AppModule {

  constructor(private injector: Injector) {
    DeployUpdateService.init(injector);
  }
}

Implement functionality to choose when to update. The following class shows 2 possible ways; on startup and on state change:

import { Injector } from '@angular/core'
import { HttpClient, HttpParams } from '@angular/common/http';
import { firstValueFrom } from 'rxjs';

import { App } from '@capacitor/app'
import { CapacitorUpdater } from '@capgo/capacitor-updater'
import type { BundleInfo } from '@capgo/capacitor-updater'
import { AlertController } from '@ionic/angular';
import { SplashScreen } from '@capacitor/splash-screen'

export class DeployUpdateService {

    // Your Application ID (you can have different ids for QA and Prod versions)
    private static appcode = 'photo-gallery';

    // To deploy to mobile devices:
    private static update_url = 'https://deploy-api.cartona.com';
    
    // To support running in an emulator against a localhost server:
    // private static update_url = 'http://10.0.2.2:8080';

    private static injector: Injector;
    private static http: HttpClient;
    private static alertCtrl: AlertController;
    private static bundle: BundleInfo | null = null;

    private static latest: any;
    private static device_id: string;

    public static init(injector: Injector) {
        this.injector = injector;
        this.http =  injector.get<HttpClient>(HttpClient);

        CapacitorUpdater.notifyAppReady();
    
        // Just check on every app startup..
        // If necessary, the app will restart..
        this.checkOnStartup();

        // Or check on every state change..
        // download latest whenever the app is opened
        // and do the update when the app is minimized
        this.checkOnStateChange();
    }

    private static async checkOnStartup() {
        const latest = await this.getLatest();
        this.latest = latest;

        let current = await CapacitorUpdater.current();
        if (current.bundle?.version == this.latest?.build) return;

        const device = await this.getDeviceId();

        if (latest && latest.id && latest.build) {
            let url = this.update_url + '/build/download';
            let params = new HttpParams();
            params = params.set('id', latest.id);
            params = params.set('device', device);
            url += '?' + params;

            let options = {
                url: url,
                version: latest.build + '',
            }

            try {
                this.latest = latest;
                this.bundle = await CapacitorUpdater.download(options);

                await this.saveEvent(this.latest);
                await CapacitorUpdater.set(this.bundle);

            } catch(err) {
                this.alert('Error in download ' + err);
            }
        } else {
            this.alert('No download possible');
        }
    }

    private static checkOnStateChange() {
            App.addListener('appStateChange', async (state: any) => {

            if (state.isActive) {
                // Do the download during user active app time to prevent failed download

                const device = await this.getDeviceId();
                const known = this.latest?.id;
                const latest = await this.getLatest();

                if (known == latest?.id) return;

                let current = await CapacitorUpdater.current();
                if (current.bundle?.version == this.latest?.build) return;

                if (latest && latest.build && latest.id && latest.id != known) {
                    let url = this.update_url + '/build/download';
                    let params = new HttpParams();
                    params = params.set('id', latest.id);
                    params = params.set('device', device);
                    url += '?' + params;
            
                    let options = {
                        url: url,
                        version: latest.build + '',
                    };

                    try {
                        this.latest = latest;
                        this.bundle = await CapacitorUpdater.download(options);
                    } catch(err) {
                        this.alert('Error in download ' + err);
                    }
                } else {
                    this.alert('No download possible');
                }
            }

            if (!state.isActive) {
                if (this.bundle && this.latest) {
                    // Do the switch when user leaves app or the app is minimized
                    SplashScreen.show()
                    
                    try {
                        await this.saveEvent(this.latest);

                        await CapacitorUpdater.set({ id: this.bundle.id });
                    }
                    catch (err) {
                        this.alert('' + err);
                        
                        SplashScreen.hide() // in case the set fail, otherwise the new app will have to hide it
                    }
                }
            }
        })
    }

    private static async getDeviceId(): Promise<string> {
        this.device_id = (await CapacitorUpdater.getDeviceId()).deviceId;
        return this.device_id;
    }

    private static async getLatest(): Promise<any> {
        const device = await this.getDeviceId();

        let url = this.update_url + '/build/latest';
        let params = new HttpParams();
        params = params.set('app', this.appcode);
        params = params.set('device', device);

        url += '?' + params;

        return firstValueFrom(this.http.get(url))
        .then((json: any) => {
            if (json && json.build) {
                return json.build;
            }
            if (json && json.builds) {
                if (Array.isArray(json.builds) && json.builds.length) {
                    return json.builds[0];
                }
            }
            return undefined;
        })
        .catch(err => {
            this.alert(err);
        })
    }

    private static async saveEvent(latest: any) {
        const device = await this.getDeviceId();

        let url = this.update_url + '/build/download/complete';
        let params = new HttpParams();
        params = params.set('id', latest.id);
        params = params.set('device', device);
        url += '?' + params;

        return firstValueFrom(this.http.get(url))
        .then((json: any) => {
            if (json) {
                return json.ok;
            } else return false;
        })
        .catch(err => {
            this.alert(err);
        })
    }

    static async alert(msg: string) {  
        this.alertCtrl = this.alertCtrl || this.injector.get<AlertController>(AlertController);

        const dlg = await this.alertCtrl.create({  
          message: msg,  
          buttons: ['OK']  
        });  
        await dlg.present();  
        const result = await dlg.onDidDismiss();  
      }  
}