From e7968da7f4a9a1b067b08010c7eac21b47190967 Mon Sep 17 00:00:00 2001 From: Guillaume Royer Date: Wed, 17 Jan 2018 09:40:58 +0000 Subject: [PATCH] feat(hot code push): add cordova-hot-code-push --- .../plugins/hot-code-push/index.ts | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/@ionic-native/plugins/hot-code-push/index.ts diff --git a/src/@ionic-native/plugins/hot-code-push/index.ts b/src/@ionic-native/plugins/hot-code-push/index.ts new file mode 100644 index 0000000000..92f08ca65f --- /dev/null +++ b/src/@ionic-native/plugins/hot-code-push/index.ts @@ -0,0 +1,131 @@ +import { Injectable } from '@angular/core'; +import { Cordova, Plugin, IonicNativePlugin, CordovaCheck } from '@ionic-native/core'; + +declare var chcp: any; + +export interface HotCodePushVersion { + /** + * Application's version name. This version is visible to the user on the stores. + */ + appVersion: string; + /** + * Application's build version number. + */ + buildVersion: string; + /** + * Version of the web content, that is displayed to the user. Basically, value of the release property from chcp.json file in your local www folder. + */ + currentWebVersion: string; + /** + * Previous web content version. This is a version of our backup. Can be empty, if there were no updates installed. + */ + previousWebVersion: string; + /** + * Version number of the web content, that was loaded by the plugin and ready to be installed. Basically, value of the release property from chcp.json file on your server. Can be empty, if no update is waiting for installation. + */ + readyToInstallWebVersion: string; +} + +export interface HotCodePushUpdate { + /** + * Current version installed. + */ + currentVersion: string; + /** + * Available version to replace the current one. + */ + readyToInstallVersion: string; +} + +export interface HotCodePushRequestOptions { + /** + * Url of the chcp.json config on the server. Plugin will use this one instead of from the config.xml. + */ + 'config-file'?: string; + /** + * Additional HTTP headers, that will be added to all requests in update download process, including loading configs and new/changed files. + */ + 'request-headers'?: {[key: string]: any}; +} + +/** + * @name Hot Code Push + * @description + * HotCodePush plugin for Cordova that supports iOS and Android. This plugin allows you to keep your html, css and js files synced with your server. + * + * For more info, please see the detailed wiki https://github.com/nordnet/cordova-hot-code-push/wiki + * + * @usage + * ```typescript + * import { HotCodePush } from '@ionic-native/hot-code-push'; + * + * constructor(private hotCodePush: HotCodePush) { } + * + * ... + * + * hotCodePush.fetchUpdate(options).then(data => { console.log('Update available'); }); + * + * ``` + */ +@Plugin({ + pluginName: 'HotCodePush', + plugin: 'cordova-hot-code-push', + pluginRef: 'chcp', + repo: 'https://github.com/nordnet/cordova-hot-code-push', + platforms: ['Android', 'iOS'] +}) +@Injectable() +export class HotCodePush extends IonicNativePlugin { + /** + * Show dialog with the request to update application through the Store (App Store or Google Play). + * @param message {string} Message to show in the dialog + * @returns {Promise} Resolves when the user is redirected to the store, rejects if the user declines. + */ + @Cordova() + requestApplicationUpdate(message: string): Promise { return; } + + /** + * Download updates from the server-side. + * @param options {HotCodePushRequestOptions} Additional options to the request. If not set - preferences from config.xml are used. + * @returns {Promise} Resolves if there is an update available, rejects otherwise. + */ + @CordovaCheck() + fetchUpdate(options?: HotCodePushRequestOptions): Promise { + return new Promise((resolve, reject) => { + HotCodePush.getPlugin().fetchUpdate((error: any, data: any) => { + if (error) { + reject(error); + } else { + resolve(data); + } + }, options); + }); + } + + /** + * Install update if there is anything to install. + * @returns {Promise} Resolves when the update is installed. + */ + @Cordova({ + callbackStyle: 'node' + }) + installUpdate(): Promise { return; } + + /** + * Check if update was loaded and ready to be installed. + * @returns {Promise} Resolves if an update is ready, rejects if there is no update. + */ + @Cordova({ + callbackStyle: 'node' + }) + isUpdateAvailableForInstallation(): Promise { return; } + + /** + * Gets information about the app's versions. + * @returns {Promise} Resolves with the information about the versions. + */ + @Cordova({ + callbackStyle: 'node' + }) + getVersionInfo(): Promise { return; } +}