forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieveExternalDependencies.js
More file actions
113 lines (100 loc) · 3.3 KB
/
retrieveExternalDependencies.js
File metadata and controls
113 lines (100 loc) · 3.3 KB
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
var Promise = require("nodegit-promise");
var promisify = require("promisify-node");
var fse = promisify("fs-extra");
var zlib = require("zlib");
var cp = require("child_process");
var path = require("path");
var local = path.join.bind(path, __dirname);
var rooted = path.join.bind(path, __dirname, "..");
var check = require(local("checkPrepared")).checkVendor;
var pkg = require(rooted("package"));
var tar;
var request;
var NODE_VERSION = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
module.exports = function retrieveExternalDependencies() {
tar = require("tar");
request = require("request");
return Promise.all([
getVendorLib("libgit2"),
getVendorLib("libssh2"),
getVendorLib("http_parser")
]);
};
function getVendorLib(name) {
var vendorPath = "vendor/" + name + "/";
var vendorPackage = pkg[name];
if (vendorPackage[NODE_VERSION]) {
vendorPackage = vendorPackage[NODE_VERSION];
}
var version = vendorPackage.sha || vendorPackage.version;
console.info("[nodegit] Detecting " + vendorPath + version + ".");
return check(name)
.then(function(exists) {
if (exists) {
console.info("[nodegit] " + vendorPath + version + " already exists.");
return Promise.resolve();
}
else {
return check(name, true)
.then(function(exists) {
if (exists) {
console.info("[nodegit] Removing outdated " + vendorPath + ".");
return fse.remove(rooted(vendorPath));
}
else {
console.info("[nodegit] " + vendorPath + " not found.");
return Promise.resolve();
}
})
.then(function() {
return new Promise(function (resolve, reject) {
console.info("[nodegit] Retrieving " + vendorPath + ".");
var extract = tar.Extract({
path: rooted("vendor/" + name + "/"),
strip: true
});
request.get(vendorPackage.url)
.pipe(zlib.createUnzip())
.pipe(extract)
.on("error", reject)
.on("end", resolve);
});
})
.then(function() {
return fse.writeFile(rooted(vendorPath + version), "");
})
.then(function() {
if ((name == "libssh2") && (process.platform !== "win32")) {
return new Promise(function(resolve, reject) {
console.info("[nodegit] Configuring libssh2.");
cp.execFile(
rooted(vendorPath) + "configure",
{cwd: rooted(vendorPath)},
function(err, stdout, stderr) {
if (err) {
console.error(err);
console.error(stderr);
reject(err, stderr);
}
else {
resolve(stdout);
}
}
);
});
}
else {
return Promise.resolve();
}
})
.then(function() {
console.info("[nodegit] Successfully updated " + vendorPath +
version + ".");
});
}
});
}
// Called on the command line
if (require.main === module) {
module.exports();
}