diff --git a/CodePush.js b/CodePush.js index 183ff65..e97c0fb 100644 --- a/CodePush.js +++ b/CodePush.js @@ -1,15 +1,13 @@ 'use strict'; -var { Alert } = require("./AlertAdapter"); -var NativeCodePush = require("react-native").NativeModules.CodePush; -var PackageMixins = require("./package-mixins")(NativeCodePush); -var requestFetchAdapter = require("./request-fetch-adapter.js"); -var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager; -var semver = require("semver"); +import { Alert } from "./AlertAdapter"; +let NativeCodePush = require("react-native").NativeModules.CodePush; +let PackageMixins = require("./package-mixins")(NativeCodePush); +import requestFetchAdapter from "./request-fetch-adapter.js"; +import { AcquisitionManager as Sdk } from "code-push/script/acquisition-sdk"; +import semver from "semver"; -function checkForUpdate(deploymentKey = null) { - var config, sdk; - +async function checkForUpdate(deploymentKey = null) { /* * Before we ask the server if an update exists, we * need to retrieve three pieces of information from the @@ -19,81 +17,74 @@ function checkForUpdate(deploymentKey = null) { * for their specific deployment and version and which are actually * different from the CodePush update they have already installed. */ - return getConfiguration() - .then((configResult) => { - /* - * If a deployment key was explicitly provided, - * then let's override the one we retrieved - * from the native-side of the app. This allows - * dynamically "redirecting" end-users at different - * deployments (e.g. an early access deployment for insiders). - */ - if (deploymentKey) { - config = Object.assign({}, configResult, { deploymentKey }); - } else { - config = configResult; - } - - sdk = new module.exports.AcquisitionSdk(requestFetchAdapter, config); - - // Allow dynamic overwrite of function. This is only to be used for tests. - return module.exports.getCurrentPackage(); - }) - .then((localPackage) => { - var queryPackage = { appVersion: config.appVersion }; - - /* - * If the app has a previously installed update, and that update - * was targetted at the same app version that is currently running, - * then we want to use its package hash to determine whether a new - * release has been made on the server. Otherwise, we only need - * to send the app version to the server, since we are interested - * in any updates for current app store version, regardless of hash. - */ - if (localPackage && localPackage.appVersion && semver.compare(localPackage.appVersion, config.appVersion) === 0) { - queryPackage = localPackage; - } - - return new Promise((resolve, reject) => { - sdk.queryUpdateWithCurrentPackage(queryPackage, (err, update) => { - if (err) { - return reject(err); - } - - /* - * There are three cases where checkForUpdate will resolve to null: - * ---------------------------------------------------------------- - * 1) The server said there isn't an update. This is the most common case. - * 2) The server said there is an update but it requires a newer binary version. - * This would occur when end-users are running an older app store version than - * is available, and CodePush is making sure they don't get an update that - * potentially wouldn't be compatible with what they are running. - * 3) The server said there is an update, but the update's hash is the same as - * the currently running update. This should _never_ happen, unless there is a - * bug in the server, but we're adding this check just to double-check that the - * client app is resilient to a potential issue with the update check. - */ - if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) { - return resolve(null); - } - - update = Object.assign(update, PackageMixins.remote); - - NativeCodePush.isFailedUpdate(update.packageHash) - .then((isFailedHash) => { - update.failedInstall = isFailedHash; - resolve(update); - }) - .catch(reject) - .done(); - }) - }); - }); + let nativeConfig = await getConfiguration(); + /* + * If a deployment key was explicitly provided, + * then let's override the one we retrieved + * from the native-side of the app. This allows + * dynamically "redirecting" end-users at different + * deployments (e.g. an early access deployment for insiders). + */ + let config = deploymentKey ? Object.assign({}, nativeConfig, { deploymentKey }) + : nativeConfig; + let sdk = getPromisifiedSdk(requestFetchAdapter, config); + // Use dynamically overridden getCurrentPackage() during tests. + let localPackage = await module.exports.getCurrentPackage(); + /* + * If the app has a previously installed update, and that update + * was targetted at the same app version that is currently running, + * then we want to use its package hash to determine whether a new + * release has been made on the server. Otherwise, we only need + * to send the app version to the server, since we are interested + * in any updates for current app store version, regardless of hash. + */ + let queryPackage = localPackage && localPackage.appVersion && semver.compare(localPackage.appVersion, config.appVersion) === 0 + ? localPackage + : { appVersion: config.appVersion }; + let update = await sdk.queryUpdateWithCurrentPackage(queryPackage); + /* + * There are three cases where checkForUpdate will resolve to null: + * ---------------------------------------------------------------- + * 1) The server said there isn't an update. This is the most common case. + * 2) The server said there is an update but it requires a newer binary version. + * This would occur when end-users are running an older app store version than + * is available, and CodePush is making sure they don't get an update that + * potentially wouldn't be compatible with what they are running. + * 3) The server said there is an update, but the update's hash is the same as + * the currently running update. This should _never_ happen, unless there is a + * bug in the server, but we're adding this check just to double-check that the + * client app is resilient to a potential issue with the update check. + */ + if (!update || update.updateAppVersion || (update.packageHash === localPackage.packageHash)) { + return null; + } else { + let remotePackage = Object.assign(update, PackageMixins.remote); + remotePackage.failedInstall = await NativeCodePush.isFailedUpdate(remotePackage.packageHash); + return remotePackage; + } } -var getConfiguration = (() => { - var config; - return function getConfiguration() { +function getPromisifiedSdk(requestFetchAdapter, config) { + // Use dynamically overridden AcquisitionSdk during tests. + let sdk = new module.exports.AcquisitionSdk(requestFetchAdapter, config); + sdk.queryUpdateWithCurrentPackage = (queryPackage) => { + return new Promise((resolve, reject) => { + sdk.queryUpdateWithCurrentPackage(queryPackage, (err, update) => { + if (err) { + reject(err); + } else { + resolve(update); + } + }); + }); + }; + + return sdk; +} + +let getConfiguration = (() => { + let config; + return () => { if (config) { return Promise.resolve(config); } else if (testConfig) { @@ -105,7 +96,7 @@ var getConfiguration = (() => { return config; }); } - } + }; })(); function getCurrentPackage() { diff --git a/Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m b/Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m index aac98cc..11de946 100644 --- a/Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m +++ b/Examples/CodePushDemoApp/iOS/CodePushDemoApp/AppDelegate.m @@ -33,7 +33,7 @@ * on the same Wi-Fi network. */ - //jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.includeRequire.runModule.bundle?dev=true&platform=ios"]; + jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.includeRequire.runModule.bundle?dev=true&platform=ios"]; /** * OPTION 2 @@ -45,7 +45,7 @@ * see http://facebook.github.io/react-native/docs/runningondevice.html */ - jsCodeLocation = [CodePush bundleURL]; + //jsCodeLocation = [CodePush bundleURL]; RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"CodePushDemoApp" diff --git a/Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist b/Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist index e1033b9..6cb683d 100644 --- a/Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist +++ b/Examples/CodePushDemoApp/iOS/CodePushDemoApp/Info.plist @@ -44,6 +44,6 @@ NSLocationWhenInUseUsageDescription CodePushDeploymentKey - deployment-key-here + 5c73310a-cc93-4aa5-bf9f-81c6b648232c