-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
executable file
·275 lines (266 loc) · 9.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'use strict';
const ReactNative = require('react-native');
const {
NativeModules,
Platform,
AsyncStorage,
} = ReactNative;
// errors
const
ERROR_NULL = 0,
ERROR_DOWNKOAD_APK = 1,
ERROR_DOWNKOAD_JS = 2,
ERROR_FAILED_INSTALL = 3,
ERROR_UNZIP_JS = 4;
const JS_VERISON_ITEM_NAME = 'rct_update_js_version_code';
let JS_VERISON_CODE = 0;
const fs = require('react-native-fs');
const RCTUpdate = NativeModules.Update;
const FileTransfer = require('@remobile/react-native-file-transfer');
const Zip = require('@remobile/react-native-zip');
RCTUpdate.getLocalValue('JS_VERSION_CLEAR', (val) => {
if (val === 'yes') {
JS_VERISON_CODE = 0;
AsyncStorage.setItem(JS_VERISON_ITEM_NAME, '0');
app.updateMgr.setNeedShowSplash(true);
RCTUpdate.setLocalValue('JS_VERSION_CLEAR', '');
} else {
AsyncStorage.getItem(JS_VERISON_ITEM_NAME).then((version) => {
JS_VERISON_CODE = (version || 0) * 1;
});
}
});
const IS_ANDROID = Platform.OS === 'android';
const DOCUMENT_FILE_PATH = RCTUpdate.documentFilePath;
const WWW_PATH = DOCUMENT_FILE_PATH + 'www';
const CACHE_PATH = DOCUMENT_FILE_PATH + '_www';
const JS_BUNDLE_ZIP_PATH = DOCUMENT_FILE_PATH + 'www.zip';
function updateApp (options) {
console.log('updateApp');
if (IS_ANDROID) {
downloadApkFromServer(options);
} else {
RCTUpdate.installFromAppStore(options.trackViewUrl);
}
}
function downloadApkFromServer (options) {
console.log('downloadApkFromServer', options);
let oldval;
const fileTransfer = new FileTransfer();
const { androidApkUrl, androidApkDownloadDestPath, onDownloadAPKProgress, onDownloadAPKStart, onDownloadAPKEnd, onError } = options;
if (onDownloadAPKProgress) {
fileTransfer.onprogress = (progress) => {
console.log('downloadApkFromServer', progress.loaded, progress.total, progress);
const val = parseInt(progress.loaded * 100 / (progress.total || 0.1));
if (oldval !== val) {
onDownloadAPKProgress(val);
oldval = val;
}
};
}
onDownloadAPKStart && onDownloadAPKStart();
fileTransfer.download(
androidApkUrl,
androidApkDownloadDestPath,
(result) => {
onDownloadAPKEnd && onDownloadAPKEnd();
RCTUpdate.installApk(androidApkDownloadDestPath);
setTimeout(() => {
onError && onError(ERROR_FAILED_INSTALL);
}, 500);
},
(error) => {
onError && onError(ERROR_DOWNKOAD_APK);
},
true
);
}
function updateJS (options) {
console.log('updateJS');
let oldval;
const fileTransfer = new FileTransfer();
const { jsbundleUrl, jsVersionCode, onDownloadJSProgress, onDownloadJSStart, onDownloadJSEnd, onError } = options;
const newJsbundleUrl = (JS_VERISON_CODE < jsVersionCode - 1) ? jsbundleUrl.replace(/\.zip/, '_all.zip') : jsbundleUrl;
if (onDownloadJSProgress) {
fileTransfer.onprogress = (progress) => {
console.log('downloadJSFromServer', progress.loaded, progress.total, progress);
const val = parseInt(progress.loaded * 100 / (progress.total || 0.1));
if (oldval !== val) {
onDownloadJSProgress && onDownloadJSProgress(val);
oldval = val;
}
};
}
onDownloadJSStart && onDownloadJSStart();
fileTransfer.download(
newJsbundleUrl,
JS_BUNDLE_ZIP_PATH,
(result) => {
onDownloadJSEnd && onDownloadJSEnd();
unzipJSZipFile(options);
},
(error) => {
onError && onError(ERROR_DOWNKOAD_JS);
},
true
);
}
function saveLocalJsVersion (ver) {
return new Promise((resolve, reject) => {
AsyncStorage.setItem(JS_VERISON_ITEM_NAME, ver + '').then(() => {
JS_VERISON_CODE = ver;
resolve();
}).catch((err) => {
resolve();
});
});
}
function deleteDir (path) {
return new Promise((resolve, reject) => {
fs.unlink(path).then(() => {
resolve();
}).catch((err) => {
resolve();
});
});
}
async function unzipJSZipFile (options) {
console.log('unzipJSZipFile', options);
let oldval;
const { jsVersionCode, onUnzipJSProgress, onUnzipJSStart, onUnzipJSEnd, onError } = options;
let onprogress;
if (onUnzipJSProgress) {
onprogress = (progress) => {
const val = parseInt(progress.loaded * 100 / progress.total);
if (oldval !== val) {
onUnzipJSProgress(val);
oldval = val;
}
};
}
onUnzipJSStart && onUnzipJSStart();
Zip.unzip(JS_BUNDLE_ZIP_PATH, DOCUMENT_FILE_PATH, async (res) => {
await deleteDir(JS_BUNDLE_ZIP_PATH);
if (res) {
onError && onError(ERROR_UNZIP_JS);
} else {
try {
let needCopyFiles = JSON.parse(await fs.readFile(CACHE_PATH + '/needCopyFiles.json'));
if (JS_VERISON_CODE === 0) {
for (let file of needCopyFiles) {
await fs.mkdir(CACHE_PATH + '/' + file.replace(/[^/]*$/, ''));
if (IS_ANDROID) {
await fs.copyFileRes(file.replace(/.*\/(.*)/, '$1'), CACHE_PATH + '/' + file);
} else {
await fs.copyFile(fs.MainBundlePath + '/' + file, CACHE_PATH + '/' + file);
}
}
} else {
for (let file of needCopyFiles) {
await fs.mkdir(CACHE_PATH + '/' + file.replace(/[^/]*$/, ''));
await fs.copyFile(WWW_PATH + '/' + file, CACHE_PATH + '/' + file);
}
}
} catch (e) {
console.log('unzip erorr:', e);
await deleteDir(CACHE_PATH);
onError && onError(ERROR_UNZIP_JS);
return;
}
await deleteDir(WWW_PATH);
await fs.unlink(CACHE_PATH + '/needCopyFiles.json');
await fs.moveFile(CACHE_PATH, WWW_PATH);
await saveLocalJsVersion(jsVersionCode);
await app.updateMgr.setNeedShowSplash(true);
onUnzipJSEnd && onUnzipJSEnd();
RCTUpdate.restartApp();
}
}, onprogress);
}
function GET (url, success, error) {
fetch(url)
.then((response) => response.json())
.then((json) => {
console.log(url, json);
success && success(json);
})
.catch((err) => {
error(err);
});
}
function getServerVersion (options) {
const { versionUrl } = options;
console.log('getServerVersion', versionUrl);
GET(versionUrl, getServerVersionSuccess.bind(null, options), getServerVersionError.bind(null, options));
}
function getServerVersionSuccess (options, remote) {
console.log('getServerVersionSuccess', options, remote);
const { resolve, versionName, currentVersion, versionCode, trackViewUrl } = options;
const jsVersionCode = IS_ANDROID ? remote.androidJsVersionCode : remote.iosJsVersionCode;
const description = IS_ANDROID ? remote.androidDescription : remote.iosDescription;
if (!IS_ANDROID) { // ios
if (versionName < remote.iosVersion) { //只有小于的时候,才提示更新大版本
if (trackViewUrl) {
return resolve({ currentVersion, description, newVersion: remote.iosVersion + '.0', trackViewUrl });
}
} else if (versionName === remote.iosVersion) { // 只有大版本相同的时候,才检测小版本
if (JS_VERISON_CODE < jsVersionCode) {
return resolve({ currentVersion, description, newVersion: versionName + '.' + jsVersionCode, jsVersionCode });
}
}
} else { // android
if (versionName < remote.androidVersion) { //只有小于的时候,才提示更新大版本
return resolve({ currentVersion, description, newVersion: remote.androidVersion + '.0' });
} else if (versionName === remote.androidVersion) { // 只有大版本相同的时候,才检测小版本
if (JS_VERISON_CODE < jsVersionCode && !remote[CONSTANTS.CHANNEL]) {
return resolve({ currentVersion, description, newVersion: versionName + '.' + jsVersionCode, jsVersionCode });
}
}
}
resolve({ currentVersion });
}
function getAppStoreVersion (options) {
const { iosAppId } = options;
if (!iosAppId) {
console.log('getAppStoreVersion without appID');
getServerVersion(options);
return;
}
console.log('getAppStoreVersion with appID:', iosAppId);
GET('http://itunes.apple.com/lookup?id=' + iosAppId, getAppStoreVersionSuccess.bind(null, options), getServerVersionError.bind(null, options));
}
function getAppStoreVersionSuccess (options, data) {
console.log('getAppStoreVersionSuccess', data);
if (data.resultCount < 1) {
getServerVersionError(options);
return;
}
const result = data.results[0];
options.trackViewUrl = result.trackViewUrl;
getServerVersion(options);
}
function getServerVersionError (options, error) {
console.log('getServerVersionError', error);
options.resolve();
}
function checkVersion (options) {
return new Promise((resolve) => {
Object.assign(options, {
resolve,
versionName: RCTUpdate.versionName,
currentVersion: RCTUpdate.versionName + '.' + JS_VERISON_CODE,
versionCode: RCTUpdate.versionCode,
});
if (IS_ANDROID) {
getServerVersion(options);
} else {
getAppStoreVersion(options);
}
});
}
module.exports = {
getVersion: () => RCTUpdate.versionName + '.' + JS_VERISON_CODE,
checkVersion,
updateApp,
updateJS,
};