1
1
'use strict' ;
2
2
3
- var { Alert } = require ( "./AlertAdapter" ) ;
4
- var NativeCodePush = require ( "react-native" ) . NativeModules . CodePush ;
5
- var PackageMixins = require ( "./package-mixins" ) ( NativeCodePush ) ;
6
- var requestFetchAdapter = require ( "./request-fetch-adapter.js" ) ;
7
- var Sdk = require ( "code-push/script/acquisition-sdk" ) . AcquisitionManager ;
8
- var semver = require ( "semver" ) ;
3
+ import { Alert } from "./AlertAdapter" ;
4
+ let NativeCodePush = require ( "react-native" ) . NativeModules . CodePush ;
5
+ let PackageMixins = require ( "./package-mixins" ) ( NativeCodePush ) ;
6
+ import requestFetchAdapter from "./request-fetch-adapter.js" ;
7
+ import { AcquisitionManager as Sdk } from "code-push/script/acquisition-sdk" ;
8
+ import semver from "semver" ;
9
9
10
- function checkForUpdate ( deploymentKey = null ) {
11
- var config , sdk ;
12
-
10
+ async function checkForUpdate ( deploymentKey = null ) {
13
11
/*
14
12
* Before we ask the server if an update exists, we
15
13
* need to retrieve three pieces of information from the
@@ -19,81 +17,74 @@ function checkForUpdate(deploymentKey = null) {
19
17
* for their specific deployment and version and which are actually
20
18
* different from the CodePush update they have already installed.
21
19
*/
22
- return getConfiguration ( )
23
- . then ( ( configResult ) => {
24
- /*
25
- * If a deployment key was explicitly provided,
26
- * then let's override the one we retrieved
27
- * from the native-side of the app. This allows
28
- * dynamically "redirecting" end-users at different
29
- * deployments (e.g. an early access deployment for insiders).
30
- */
31
- if ( deploymentKey ) {
32
- config = Object . assign ( { } , configResult , { deploymentKey } ) ;
33
- } else {
34
- config = configResult ;
35
- }
36
-
37
- sdk = new module . exports . AcquisitionSdk ( requestFetchAdapter , config ) ;
38
-
39
- // Allow dynamic overwrite of function. This is only to be used for tests.
40
- return module . exports . getCurrentPackage ( ) ;
41
- } )
42
- . then ( ( localPackage ) => {
43
- var queryPackage = { appVersion : config . appVersion } ;
44
-
45
- /*
46
- * If the app has a previously installed update, and that update
47
- * was targetted at the same app version that is currently running,
48
- * then we want to use its package hash to determine whether a new
49
- * release has been made on the server. Otherwise, we only need
50
- * to send the app version to the server, since we are interested
51
- * in any updates for current app store version, regardless of hash.
52
- */
53
- if ( localPackage && localPackage . appVersion && semver . compare ( localPackage . appVersion , config . appVersion ) === 0 ) {
54
- queryPackage = localPackage ;
55
- }
56
-
57
- return new Promise ( ( resolve , reject ) => {
58
- sdk . queryUpdateWithCurrentPackage ( queryPackage , ( err , update ) => {
59
- if ( err ) {
60
- return reject ( err ) ;
61
- }
62
-
63
- /*
64
- * There are three cases where checkForUpdate will resolve to null:
65
- * ----------------------------------------------------------------
66
- * 1) The server said there isn't an update. This is the most common case.
67
- * 2) The server said there is an update but it requires a newer binary version.
68
- * This would occur when end-users are running an older app store version than
69
- * is available, and CodePush is making sure they don't get an update that
70
- * potentially wouldn't be compatible with what they are running.
71
- * 3) The server said there is an update, but the update's hash is the same as
72
- * the currently running update. This should _never_ happen, unless there is a
73
- * bug in the server, but we're adding this check just to double-check that the
74
- * client app is resilient to a potential issue with the update check.
75
- */
76
- if ( ! update || update . updateAppVersion || ( update . packageHash === localPackage . packageHash ) ) {
77
- return resolve ( null ) ;
78
- }
20
+ let nativeConfig = await getConfiguration ( ) ;
21
+ /*
22
+ * If a deployment key was explicitly provided,
23
+ * then let's override the one we retrieved
24
+ * from the native-side of the app. This allows
25
+ * dynamically "redirecting" end-users at different
26
+ * deployments (e.g. an early access deployment for insiders).
27
+ */
28
+ let config = deploymentKey ? Object . assign ( { } , nativeConfig , { deploymentKey } )
29
+ : nativeConfig ;
30
+ let sdk = getPromisifiedSdk ( requestFetchAdapter , config ) ;
31
+ // Use dynamically overridden getCurrentPackage() during tests.
32
+ let localPackage = await module . exports . getCurrentPackage ( ) ;
33
+ /*
34
+ * If the app has a previously installed update, and that update
35
+ * was targetted at the same app version that is currently running,
36
+ * then we want to use its package hash to determine whether a new
37
+ * release has been made on the server. Otherwise, we only need
38
+ * to send the app version to the server, since we are interested
39
+ * in any updates for current app store version, regardless of hash.
40
+ */
41
+ let queryPackage = localPackage && localPackage . appVersion && semver . compare ( localPackage . appVersion , config . appVersion ) === 0
42
+ ? localPackage
43
+ : { appVersion : config . appVersion } ;
44
+ let update = await sdk . queryUpdateWithCurrentPackage ( queryPackage ) ;
45
+ /*
46
+ * There are three cases where checkForUpdate will resolve to null:
47
+ * ----------------------------------------------------------------
48
+ * 1) The server said there isn't an update. This is the most common case.
49
+ * 2) The server said there is an update but it requires a newer binary version.
50
+ * This would occur when end-users are running an older app store version than
51
+ * is available, and CodePush is making sure they don't get an update that
52
+ * potentially wouldn't be compatible with what they are running.
53
+ * 3) The server said there is an update, but the update's hash is the same as
54
+ * the currently running update. This should _never_ happen, unless there is a
55
+ * bug in the server, but we're adding this check just to double-check that the
56
+ * client app is resilient to a potential issue with the update check.
57
+ */
58
+ if ( ! update || update . updateAppVersion || ( update . packageHash === localPackage . packageHash ) ) {
59
+ return null ;
60
+ } else {
61
+ let remotePackage = Object . assign ( update , PackageMixins . remote ) ;
62
+ remotePackage . failedInstall = await NativeCodePush . isFailedUpdate ( remotePackage . packageHash ) ;
63
+ return remotePackage ;
64
+ }
65
+ }
79
66
80
- update = Object . assign ( update , PackageMixins . remote ) ;
81
-
82
- NativeCodePush . isFailedUpdate ( update . packageHash )
83
- . then ( ( isFailedHash ) => {
84
- update . failedInstall = isFailedHash ;
85
- resolve ( update ) ;
86
- } )
87
- . catch ( reject )
88
- . done ( ) ;
89
- } )
90
- } ) ;
67
+ function getPromisifiedSdk ( requestFetchAdapter , config ) {
68
+ // Use dynamically overridden AcquisitionSdk during tests.
69
+ let sdk = new module . exports . AcquisitionSdk ( requestFetchAdapter , config ) ;
70
+ sdk . queryUpdateWithCurrentPackage = ( queryPackage ) => {
71
+ return new Promise ( ( resolve , reject ) => {
72
+ sdk . queryUpdateWithCurrentPackage ( queryPackage , ( err , update ) => {
73
+ if ( err ) {
74
+ reject ( err ) ;
75
+ } else {
76
+ resolve ( update ) ;
77
+ }
78
+ } ) ;
91
79
} ) ;
80
+ } ;
81
+
82
+ return sdk ;
92
83
}
93
84
94
- var getConfiguration = ( ( ) => {
95
- var config ;
96
- return function getConfiguration ( ) {
85
+ let getConfiguration = ( ( ) => {
86
+ let config ;
87
+ return ( ) => {
97
88
if ( config ) {
98
89
return Promise . resolve ( config ) ;
99
90
} else if ( testConfig ) {
@@ -105,7 +96,7 @@ var getConfiguration = (() => {
105
96
return config ;
106
97
} ) ;
107
98
}
108
- }
99
+ } ;
109
100
} ) ( ) ;
110
101
111
102
function getCurrentPackage ( ) {
0 commit comments