-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (58 loc) · 2.28 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
const fetch = require('node-fetch');
const clc = require('cli-color');
const fs = require('fs');
const rimraf = require('rimraf');
const ok = clc.greenBright;
const emph = clc.blueBright;
const config = require('./config');
const { repository, ardroneVersion, outPath } = config;
console.log(emph(`ArDrone ${ardroneVersion}.0 batch firmware downloader started!`));
// prepare all urls
const urls = [];
for (let ma=config.majorFrom;ma<=config.majorTo;ma++) {
for (let mi=config.minorFrom;mi<=config.minorTo;mi++) {
for (let pa=config.patchFrom;pa<=config.patchTo;pa++) {
urls.push({
version: `${ma}.${mi}.${pa}`,
url: repository(ma, mi, pa)
})
}
}
}
console.log('Will try to fetch firmware from', emph(urls.length), 'different endpoints');
// clean previous directory if any
rimraf(outPath, () => {
console.log('Cleaned output directory', emph(outPath));
// create out directory
fs.mkdirSync(outPath);
console.log('Running now...');
Promise.all(urls.map(url => new Promise((resolve, reject) => {
fetch(url.url).then(res => {
status = res.status;
if (status === 200) {
console.log(ok('✓'), 'Found firmware for', url.version, emph('( HTTP', status, ')'), ', downloading...');
const fwPath = `${outPath}/${url.version}-ardrone${ardroneVersion}_update.plf`;
saveFile(res, fwPath).then(() => {
console.log(ok('✓'), url.version, 'firmware downloaded!');
resolve(true)
}).catch((err) => reject(err));
} else {
resolve(false);
}
}).catch((err) => {
reject(err);
});
})
)).then((dlRes) => {
console.log('Finished,', emph(dlRes.filter(res => res === true).length), 'valid firmwares files saved to', emph(outPath));
process.exit(0);
}).catch((e) => { console.error(e); });
});
function saveFile(response, path) {
return new Promise((resolve, reject) => {
const dest = fs.createWriteStream(path);
response.body.pipe(dest);
response.body.on('error', reject);
dest.on('finish', resolve);
});
}