-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease_asset.js
47 lines (40 loc) · 1.28 KB
/
release_asset.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
const os = require('os');
const path = require('path');
const fs = require('fs');
const request = require('request');
const AdmZip = require('adm-zip');
function findAsset(assets, name){
for(var i in assets){
if(assets[i].name == name) {
return assets[i].url
}
}
throw `${name} not found in assets`;
}
async function downloadAsset(url, token) {
var filePath = path.join(os.tmpdir(), "asset.zip");
const options = {
url: url,
headers: {
'User-Agent': 'request',
'Accept': 'application/octet-stream',
'Authorization': `token ${token}`
}
};
var stream = request(options).pipe(fs.createWriteStream(filePath))
return new Promise((resolve) => {
stream.on('finish', ()=>{ resolve(filePath) });
});
}
exports.downloadAndExtract = async function(fileName, assets, token, targetPath) {
var assetUrl = findAsset(assets, fileName)
console.log(`Downloading ${assetUrl}`);
var tempFile = await downloadAsset(assetUrl, token)
var zip = new AdmZip(tempFile);
console.log(`Extracting ${tempFile}`);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function(zipEntry) {
console.log(" - " + zipEntry.entryName); // outputs zip entries information
});
await zip.extractAllTo(targetPath, true);
};