forked from yeoman/generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
59 lines (49 loc) · 1.64 KB
/
fetch.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
var fs = require('fs'),
path = require('path'),
colors = require('colors'),
zlib = require('zlib'),
request = require('request'),
tar = require('tar');
// Check if we're behind some kind of proxy.
var proxy = process.env.http_proxy || process.env.HTTP_PROXY ||
process.env.https_proxy || process.env.HTTPS_PROXY || '';
// heavily based on npm's util/untar.js file
function fetch(tarball, target, cb) {
var now = +new Date();
var log = this.log
.subhead('Fetching ' + tarball)
.writeln('This might take a few moment'.yellow);
// tarball untar opts
var extractOpts = { type: 'Directory', path: target, strip: 1 };
// remote request --> zlib.Unzip() --> untar into h5bp/root
var req = fetch.request.get(tarball).on('error', cb);
req.on('data', function() { log.write('.'); }).on('end', function() {
log.ok().writeln();
log.ok( ('Done in ' + (+new Date() - now) / 1000 + 's.').green );
});
req
// first gzip
.pipe(zlib.Unzip())
.on('error', function(err) {
console.error('unzip error', err);
cb(err);
})
// then tar extract into h5bp/root
.pipe(tar.Extract(extractOpts))
.on('entry', function(entry) {
entry.props.uid = entry.uid = 501;
entry.props.gid = entry.gid = 20;
})
.on('error', function(err) {
console.error('untar error', err);
cb(err);
})
.on('close', function() {
log.writeln().ok( ('Done in ' + extractOpts.path).green ).writeln();
cb();
});
}
module.exports = fetch;
// re-expose the request with proxy defaults, so that we can
// reuse this instance of request.
fetch.request = request.defaults({ proxy: proxy });