-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
177 lines (157 loc) · 5.06 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
import { NativeModules, NetInfo, Platform } from 'react-native';
import { deleteFile, download, isFileExists } from './lib/downloadHelper';
const { RNEasyUpgrade } = NativeModules;
let jobId = -1;
const isAndroid = Platform.OS === 'android';
const VERSION_NAME = RNEasyUpgrade.versionName;
const VERSION_CODE = RNEasyUpgrade.versionCode;
const DEFAULT_DOWNLOAD_APK_NAME = `newVersion.apk`;
const DocumentDirectoryPath = isAndroid && RNEasyUpgrade.RNDocumentDirectoryPath;
const defaults = {
iOSAppLookupUrl: 'https://itunes.apple.com/lookup?id={0}',
downloadTitle: '下载更新包',
downloadDescription: '新版本更新包下载中',
downloadDestDirectory: DocumentDirectoryPath,
downloadApkName: DEFAULT_DOWNLOAD_APK_NAME,
downloadApkEnd: path => RNEasyUpgrade.installApk(path),
shouldCheckApkHasDownloaded: true,
onError: () => {}
};
class AppUpgrade {
/**
*
* @param {object} options
* @param {string} [options.iOSAppLookupUrl] 可选,ios检查更新的地址
* @param {string} options.iOSAppId IOS的AppID
*/
constructor(options) {
this.options = Object.assign({}, defaults, options);
}
get downloadDestPath() {
return this.options.downloadDestDirectory + '/' + this.options.downloadApkName;
}
get downloadDestDirectory() {
return this.options.downloadDestDirectory;
}
get downloading() {
return jobId > -1;
}
_handleError(err) {
console.log('EasyUpgradeError', err);
this.options.onError(err);
}
getNetworkStatus() {
return new Promise(async resolve => {
NetInfo.fetch().done(resolve);
});
}
getLocalVersionInfo() {
return { VERSION_NAME, VERSION_CODE };
}
checkApkHasDownloaded = async (path = this.downloadDestPath) => {
return await isFileExists(path);
}
/**
* download file
* @param fileUrl
* @param downloadConf
*/
downloadFile(fileUrl, downloadConf = {}) {
jobId = 1;
download(fileUrl, downloadConf)
.then(async res => {
jobId = -1;
if (this.options.shouldCheckApkHasDownloaded) {
await RNEasyUpgrade.moveFile(downloadConf.tempDownloadPath, this.downloadDestPath);
}
this.options.downloadApkEnd(this.downloadDestPath);
})
.catch(err => {
jobId = -1;
this._handleError(err);
});
}
/**
* update app and install
* @param apkUrl
*/
async updateAndroidApp(apkUrl) {
if (this.downloading) {
return;
}
const tempDownloadApkName = 'temp_download.apk';
const tempDownloadPath = this.downloadDestDirectory + '/' + tempDownloadApkName;
const downloadConf = {
downloadTitle: this.options.downloadTitle,
downloadDescription: this.options.downloadDescription,
saveAsName: this.options.shouldCheckApkHasDownloaded ? tempDownloadApkName : this.options.downloadApkName,
allowedInRoaming: true,
allowedInMetered: true,
showInDownloads: true,
external: true, //when false basically means use the default Download path
path: this.downloadDestDirectory, //if "external" is true then use this path
tempDownloadPath: tempDownloadPath
};
if (this.options.shouldCheckApkHasDownloaded) {
try {
const isTempFileExisted = await isFileExists(tempDownloadPath);
if (isTempFileExisted) {
await deleteFile(tempDownloadPath);
}
} catch (err) {
this._handleError(err);
}
}
this.downloadFile(apkUrl, downloadConf);
}
installApk(apkPath = this.downloadDestPath) {
RNEasyUpgrade.installApk(apkPath);
}
/**
* Check the ios app version info from app store.
*/
async checkAppVersionIOS() {
if (!this.options.iOSAppId) {
throw new Error('Must provide iOSAppId.');
}
const iosCheckVersionUrl = (this.options.iOSAppLookupUrl || '').replace(/\{0\}/g, this.options.iOSAppId);
const data = await fetch(iosCheckVersionUrl).then(res => res.json());
const result = ((data || {}).results || [])[0] || {};
const latestVersion = result.version;
const localVersion = VERSION_NAME;
this.trackViewUrl = result.trackViewUrl;
const hasNewVersion = latestVersion !== localVersion; // 简单认为,版本号不一致就是有新版本
return {
latestVersion,
localVersion,
hasNewVersion,
trackViewUrl: this.trackViewUrl,
lookupInfo: result
};
}
/**
* navigate to app store download page.
* @param {string} [trackViewUrl] [Optional] The app download page
*/
navigateToAppStore(trackViewUrl) {
RNEasyUpgrade.openURL(trackViewUrl || this.trackViewUrl);
}
startAppUpdate(apkUrl, appStoreUrl = this.trackViewUrl) {
if (isAndroid) {
if (this.options.shouldCheckApkHasDownloaded) {
this.checkApkHasDownloaded().then(async hasDownloaded => {
if (hasDownloaded) {
this.options.downloadApkEnd(this.downloadDestPath);
} else {
this.updateAndroidApp(apkUrl);
}
});
} else {
this.updateAndroidApp(apkUrl);
}
} else {
this.navigateToAppStore(appStoreUrl);
}
}
}
export default AppUpgrade;