|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const path = require('path'); |
4 | | -const os = require('os'); |
5 | | -const fs = require('fs-extra'); |
| 3 | +const unzip = require('unzip2'); |
6 | 4 | const config = require('./config'); |
7 | 5 |
|
8 | | -// Downloads the zip file from github and extracts it to /tmp/tldr |
9 | | -exports.download = () => { |
10 | | - let request = require('request'); |
11 | | - let unzip = require('unzip2'); |
| 6 | +let request = require('request'); |
| 7 | + |
| 8 | +// Downloads the zip file from github and extracts it to folder |
| 9 | +exports.download = (path) => { |
12 | 10 | let url = config.get().repository; |
13 | | - let target = path.join(os.tmpdir(), 'tldr'); |
14 | | - |
15 | | - // Empty the tmp dir |
16 | | - return fs.emptyDir(target) |
17 | | - .then(() => { |
18 | | - // Creating the extractor |
19 | | - let extractor = unzip.Extract({ path: target }); |
20 | | - |
21 | | - // Setting the proxy if set by config |
22 | | - if (config.get().proxy) { |
23 | | - request = request.defaults({ proxy: config.proxy }); |
24 | | - } |
25 | | - |
26 | | - // Creating the request and passing the extractor |
27 | | - let req = request.get({ |
28 | | - url: url, |
29 | | - headers: { 'User-Agent' : 'tldr-node-client' } |
30 | | - }); |
31 | | - |
32 | | - req.pipe(extractor); |
33 | | - |
34 | | - return new Promise((resolve, reject) => { |
35 | | - req.on('error', (err) => { |
36 | | - reject(err); |
37 | | - }); |
38 | | - extractor.on('error', () => { |
39 | | - reject(new Error('Cannot update from ' + url)); |
40 | | - }); |
41 | | - extractor.on('close', () => { |
42 | | - resolve(target); |
43 | | - }); |
44 | | - }); |
| 11 | + |
| 12 | + // Creating the extractor |
| 13 | + let extractor = unzip.Extract({ path }); |
| 14 | + |
| 15 | + // Setting the proxy if set by config |
| 16 | + if (config.get().proxy) { |
| 17 | + request = request.defaults({ proxy: config.proxy }); |
| 18 | + } |
| 19 | + |
| 20 | + // Creating the request and passing the extractor |
| 21 | + let req = request.get({ |
| 22 | + url: url, |
| 23 | + headers: { 'User-Agent' : 'tldr-node-client' } |
| 24 | + }); |
| 25 | + |
| 26 | + req.pipe(extractor); |
| 27 | + |
| 28 | + return new Promise((resolve, reject) => { |
| 29 | + req.on('error', (err) => { |
| 30 | + reject(err); |
| 31 | + }); |
| 32 | + extractor.on('error', () => { |
| 33 | + reject(new Error('Cannot update from ' + url)); |
| 34 | + }); |
| 35 | + extractor.on('close', () => { |
| 36 | + resolve(); |
45 | 37 | }); |
| 38 | + }); |
46 | 39 | }; |
0 commit comments