-
Notifications
You must be signed in to change notification settings - Fork 17
/
binstall.js
149 lines (128 loc) · 3.63 KB
/
binstall.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var fs = require("fs");
var request = require("request");
var tar = require("tar");
var zlib = require("zlib");
var unzip = require("unzip-stream");
function binstall(url, path, options) {
if (url.endsWith(".zip")) {
return unzipUrl(url, path, options);
} else {
return untgz(url, path, options);
}
}
function untgz(url, path, options) {
options = options || {};
var verbose = options.verbose;
var verify = options.verify;
return new Promise(function(resolve, reject) {
var untar = tar
.x({ cwd: path })
.on("error", function(error) {
reject("Error extracting " + url + " - " + error);
})
.on("end", function() {
var successMessage = "Successfully downloaded and processed " + url;
if (verify) {
verifyContents(verify)
.then(function() {
resolve(successMessage);
})
.catch(reject);
} else {
resolve(successMessage);
}
});
var gunzip = zlib.createGunzip().on("error", function(error) {
reject("Error decompressing " + url + " " + error);
});
try {
fs.mkdirSync(path);
} catch (error) {
if (error.code !== 'EEXIST') throw error;
}
request
.get(url, function(error, response) {
if (error) {
reject("Error communicating with URL " + url + " " + error);
return;
}
if (response.statusCode == 404) {
var errorMessage = options.errorMessage || "Not Found: " + url;
reject(new Error(errorMessage));
return;
}
if (verbose) {
console.log("Downloading binaries from " + url);
}
response.on("error", function() {
reject("Error receiving " + url);
});
})
.pipe(gunzip)
.pipe(untar);
});
}
function unzipUrl(url, path, options) {
options = options || {};
var verbose = options.verbose;
var verify = options.verify;
return new Promise(function(resolve, reject) {
var writeStream = unzip
.Extract({ path: path })
.on("error", function(error) {
reject("Error extracting " + url + " - " + error);
})
.on("entry", function(entry) {
console.log("Entry: " + entry.path);
})
.on("close", function() {
var successMessage = "Successfully downloaded and processed " + url;
if (verify) {
verifyContents(verify)
.then(function() {
resolve(successMessage);
})
.catch(reject);
} else {
resolve(successMessage);
}
});
request
.get(url, function(error, response) {
if (error) {
reject("Error communicating with URL " + url + " " + error);
return;
}
if (response.statusCode == 404) {
var errorMessage = options.errorMessage || "Not Found: " + url;
reject(new Error(errorMessage));
return;
}
if (verbose) {
console.log("Downloading binaries from " + url);
}
response.on("error", function() {
reject("Error receiving " + url);
});
})
.pipe(writeStream);
});
}
function verifyContents(files) {
return Promise.all(
files.map(function(filePath) {
return new Promise(function(resolve, reject) {
fs.stat(filePath, function(err, stats) {
if (err) {
reject(filePath + " was not found.");
} else if (!stats.isFile()) {
reject(filePath + " was not a file.");
} else {
resolve();
}
});
});
})
);
}
module.exports = binstall;