-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.js
95 lines (80 loc) · 2.73 KB
/
util.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var _ = module.exports;
var path = require('path');
var fs = require('fs');
_.mkdirp = function mkdirP ( p, mode, made ) {
if (mode === undefined) {
mode = 0777 & (~process.umask());
}
if (!made) made = null;
if (typeof mode === 'string') mode = parseInt(mode, 8);
p = path.resolve(p);
try {
fs.mkdirSync(p, mode);
made = made || p;
}
catch (err0) {
switch (err0.code) {
case 'ENOENT' :
made = mkdirP(path.dirname(p), mode, made);
mkdirP(p, mode, made);
break;
// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
var stat;
try {
stat = fs.statSync(p);
}
catch (err1) {
throw err0;
}
if (!stat.isDirectory()) throw err0;
break;
}
}
return made;
}
_.download = function(opt, done) {
var remote = opt.remote;
var dest = opt.dest;
var url = require('url');
var fs = require('fs');
var options = url.parse(remote);
var notifiedSize = opt.notifiedSize || 512 * 1024;
var http = options.protocol === 'https:' ? require('https') : require('http')
var client;
process.stdout.write('Downloading ' + remote + ' ...\n');
client = http.get( options, function( res ) {
var count = 0,
notifiedCount = 0,
outFile;
if ( res.statusCode === 200 ) {
_.mkdirp(path.dirname(dest));
outFile = fs.openSync( dest, 'w' );
res.on('data', function( data ) {
fs.writeSync(outFile, data, 0, data.length, null);
count += data.length;
if ( (count - notifiedCount) > notifiedSize ) {
process.stdout.write('Received ' + Math.floor( count / 1024 ) + 'K...\n');
notifiedCount = count;
}
});
res.addListener('end', function() {
process.stdout.write('Received ' + Math.floor(count / 1024) + 'K total.\n');
fs.closeSync( outFile );
done( false );
});
} else if (res.statusCode === 302 && res.headers.location) {
client.abort();
opt.remote = res.headers.location;
process.stdout.write('Redirct to ' + opt.remote + '\n');
_.download(opt, done);
} else {
client.abort();
done('Error requesting archive')
}
}).on('error', function(e) {
done(e.message || 'unkown error');
});
}