-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathremote.js
38 lines (33 loc) · 928 Bytes
/
remote.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
'use strict';
const unzip = require('node-unzip-2');
const config = require('./config');
const axios = require('axios');
/**
* Download the zip file from GitHub and extract it to folder.
* @param path {string} Path to destination folder.
* @returns {Promise<void>} A promise when the operation is completed.
*/
exports.download = (path) => {
const url = config.get().repository;
// Creating the extractor
const extractor = unzip.Extract({ path });
let req = axios({
method: 'get',
url: url,
responseType: 'stream',
headers: { 'User-Agent' : 'tldr-node-client' }
}).then(function (response) {
response.data.pipe(extractor);
});
return new Promise((resolve, reject) => {
req.catch((err) => {
reject(err);
});
extractor.on('error', () => {
reject(new Error('Cannot update from ' + url));
});
extractor.on('close', () => {
resolve();
});
});
};