forked from datso/react-native-pjsip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibs.js
115 lines (77 loc) · 2.52 KB
/
libs.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
const VERSION = "v2.8.0";
const URL = `https://github.com/datso/react-native-pjsip-builder/releases/download/${VERSION}/release.tar.gz`;
const LOCK = ".libs.lock";
const DEST = ".libs.tar.gz";
let DOWNLOAD = true;
const fs = require('fs');
const http = require('http');
const https = require('https');
async function downloadFile(url, filePath) {
const proto = !url.charAt(4).localeCompare('s') ? https : http;
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filePath);
let fileInfo = null;
const request = proto.get(url, response => {
if (response.statusCode === 302) {
reject(new Error(response.headers['location']));
return;
}
if (response.statusCode !== 200) {
reject(new Error(`Failed to get '${url}' (${response.statusCode})`));
return;
}
fileInfo = {
mime: response.headers['content-type'],
size: parseInt(response.headers['content-length'], 10),
};
response.pipe(file);
});
// The destination stream is ended by the time it's called
file.on('finish', () => resolve(fileInfo));
request.on('error', err => {
fs.unlink(filePath, () => reject(err));
});
file.on('error', err => {
fs.unlink(filePath, () => reject(err));
});
request.end();
});
}
async function main() {
if (fs.existsSync(LOCK)) {
const CURRENT_VERSION = fs.readFileSync(LOCK);
console.log(`Check lock file ${CURRENT_VERSION} with current version ${VERSION}`);
if (`${CURRENT_VERSION}` === VERSION) DOWNLOAD = false;
}
if (DOWNLOAD) {
console.log(`Download the file`);
let newUrl = '';
try {
await downloadFile(URL, DEST);
} catch (ex) {
if (ex.message.startsWith('http')) {
newUrl = ex.message;
} else {
return;
}
}
if (newUrl) {
try {
await downloadFile(newUrl, DEST);
} catch (ex) {
console.log(ex);
return;
}
}
console.log('Download completed!');
const decompress = require('decompress');
console.log('Extract content');
decompress(DEST, '.').then(files => {
console.log('Extract complete!');
console.log('Delete the downloaded file');
fs.unlinkSync(DEST);
});
fs.writeFileSync(LOCK, VERSION);
}
}
main();