Skip to content

Instantly share code, notes, and snippets.

@slorber
Created June 3, 2016 15:19
Show Gist options
  • Save slorber/fc6167275c0d194ada00da9bf49408d4 to your computer and use it in GitHub Desktop.
Save slorber/fc6167275c0d194ada00da9bf49408d4 to your computer and use it in GitHub Desktop.
"use strict";
// If you want to use CodePush for development
// Maybe it can be worth being able to inject your own key here with Webpack's DefinePlugin
const DeveloperOptions = {
// deploymentKey: "PUT_YOUR_KEY"
};
const StagingIOSKey = 'xxx';
const StagingAndroidKey = 'zzz';
function setup(atomReactContext,bootstrapData) {
console.debug("CodePush setup called",arguments);
const isUsingNextVersion = bootstrapData.user.useNextVersion;
// If user uses next frontend version, we want him to use the Staging codePush code instead of production
// Overrides deploymentKey set in config.xml
// Not sure it's the best strategy but let's try it
const customOptions = isUsingNextVersion ? {
deploymentKey: isIOS() ? StagingIOSKey : StagingAndroidKey
} : {};
syncStampleCode(customOptions);
document.addEventListener("resume", function () {
syncStampleCode(customOptions);
});
setInterval(function() {
syncStampleCode(customOptions);
},1000 * 60 * 10); // 10 minutes
}
exports.setup = setup;
function isIOS() {
return device.platform.toUpperCase() === "IOS";
}
function getUpdateDialogOptions() {
// IOS does not allow this, according to the doc
if ( isIOS() ) {
return;
}
return {
updateTitle: "Stample update available",
descriptionPrefix: "\nUpdate description:\n",
appendReleaseDescription: true,
mandatoryUpdateMessage: "Stample application must upgrade to continue working",
mandatoryContinueButtonLabel: "Update",
optionalUpdateMessage: "A new version of Stample application is available",
optionalIgnoreButtonLabel: "Ignore",
optionalInstallButtonLabel: "Update",
}
}
function syncStampleCode(customOptions) {
const syncOptions = {
installMode: InstallMode.ON_NEXT_RESUME,
minimumBackgroundDuration: 60 * 60 * 1, // 1h
updateDialog: getUpdateDialogOptions(),
...customOptions,
...DeveloperOptions,
};
console.debug("CodePush sync called",syncOptions);
codePush.sync(syncCallback,syncOptions,downloadProgressCallback);
}
function downloadProgressCallback(downloadProgress) {
console.debug("Downloading " + downloadProgress.receivedBytes + " of " + downloadProgress.totalBytes + " bytes.");
}
function syncCallback(syncStatus) {
console.debug("CodePush syncStatus="+syncStatus);
switch (syncStatus) {
// Result (final) statuses
case SyncStatus.UPDATE_INSTALLED:
alert("Stample app update successful!")
break;
case SyncStatus.UP_TO_DATE:
console.debug("The application is up to date.");
break;
case SyncStatus.UPDATE_IGNORED:
console.debug("The user decided not to install the optional update.");
break;
case SyncStatus.ERROR:
console.error("An error occured while checking for updates");
break;
// Intermediate (non final) statuses
case SyncStatus.CHECKING_FOR_UPDATE:
console.debug("Checking for update.");
break;
case SyncStatus.AWAITING_USER_ACTION:
console.debug("Alerting user.");
break;
case SyncStatus.DOWNLOADING_PACKAGE:
alert("A Stample app update is being downloaded. It will be installed soon.");
break;
case SyncStatus.INSTALLING_UPDATE:
alert("Installing Stample app update.");
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment