forked from cmake-js/cmake-js
/
dist.js
196 lines (179 loc) · 5.93 KB
/
dist.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"use strict";
let environment = require("./environment");
let path = require("path");
let urljoin = require("url-join");
let fs = require("fs-extra");
let _ = require("lodash");
let CMLog = require("./cmLog");
let TargetOptions = require("./targetOptions");
let runtimePaths = require("./runtimePaths");
let Downloader = require("./downloader");
function testSum(sums, sum, fPath) {
let serverSum = _.first(sums.filter(function (s) {
return s.getPath === fPath;
}));
if (serverSum && serverSum.sum === sum) {
return;
}
throw new Error("SHA sum of file '" + fPath + "' mismatch!");
}
function Dist(options) {
this.options = options || {};
this.log = new CMLog(this.options);
this.targetOptions = new TargetOptions(this.options);
this.downloader = new Downloader(this.options);
}
// Props
Object.defineProperties(Dist.prototype, {
internalPath: {
get: function () {
let cacheDirectory = ".cmake-js";
let runtimeArchDirectory = (this.targetOptions.runtime) + "-" + this.targetOptions.arch;
let runtimeVersionDirectory = "v" + this.targetOptions.runtimeVersion;
return this.options.runtimeDirectory ||
path.join(environment.home,
cacheDirectory,
runtimeArchDirectory,
runtimeVersionDirectory);
}
},
externalPath: {
get: function () {
return runtimePaths.get(this.targetOptions).externalPath;
}
},
downloaded: {
get: function () {
let headers = false;
let libs = true;
let stat = getStat(this.internalPath);
if (stat.isDirectory()) {
if (this.headerOnly) {
stat = getStat(path.join(this.internalPath, "include/node/node.h"));
headers = stat.isFile();
}
else {
stat = getStat(path.join(this.internalPath, "src/node.h"));
if (stat.isFile()) {
stat = getStat(path.join(this.internalPath, "deps/v8/include/v8.h"));
headers = stat.isFile();
}
}
if (environment.isWin) {
for (let libPath of this.winLibs) {
stat = getStat(libPath);
libs = libs && stat.isFile();
}
}
}
return headers && libs;
function getStat(path) {
try {
return fs.statSync(path);
}
catch (e) {
return {
isFile: _.constant(false),
isDirectory: _.constant(false)
};
}
}
}
},
winLibs: {
get: function () {
let libs = runtimePaths.get(this.targetOptions).winLibs;
let result = [];
for (let lib of libs) {
result.push(path.join(this.internalPath, lib.dir, lib.name));
}
return result;
}
},
headerOnly: {
get: function () {
return runtimePaths.get(this.targetOptions).headerOnly;
}
}
});
// Methods
Dist.prototype.ensureDownloaded = async function () {
if (!this.downloaded) {
await this.download();
}
};
Dist.prototype.download = async function () {
let log = this.log;
log.info("DIST", "Downloading distribution files to: " + this.internalPath);
await fs.ensureDir(this.internalPath);
let sums = await this._downloadShaSums();
await Promise.all([this._downloadLibs(sums), this._downloadTar(sums)]);
};
Dist.prototype._downloadShaSums = async function () {
if (this.targetOptions.runtime === "node" || this.targetOptions.runtime === "iojs") {
let sumUrl = urljoin(this.externalPath, "SHASUMS256.txt");
let log = this.log;
log.http("DIST", "\t- " + sumUrl);
return (await this.downloader.downloadString(sumUrl))
.split("\n")
.map(function (line) {
let parts = line.split(/\s+/);
return {
getPath: parts[1],
sum: parts[0]
};
})
.filter(function (i) {
return i.getPath && i.sum;
});
}
else {
return null;
}
};
Dist.prototype._downloadTar = async function (sums) {
let log = this.log;
let self = this;
let tarLocalPath = runtimePaths.get(self.targetOptions).tarPath;
let tarUrl = urljoin(self.externalPath, tarLocalPath);
log.http("DIST", "\t- " + tarUrl);
let sum = await this.downloader.downloadTgz(tarUrl, {
hash: sums ? "sha256" : null,
cwd: self.internalPath,
strip: 1,
filter: function (entryPath) {
if (entryPath === self.internalPath) {
return true;
}
let ext = path.extname(entryPath);
return ext && ext.toLowerCase() === ".h";
}
});
if (sums) {
testSum(sums, sum, tarLocalPath);
}
};
Dist.prototype._downloadLibs = async function (sums) {
const log = this.log;
const self = this;
if (!environment.isWin) {
return;
}
const paths = runtimePaths.get(self.targetOptions);
for (const dirs of paths.winLibs) {
const subDir = dirs.dir;
const fn = dirs.name;
const fPath = subDir ? urljoin(subDir, fn) : fn;
const libUrl = urljoin(self.externalPath, fPath);
log.http("DIST", "\t- " + libUrl);
await fs.ensureDir(path.join(self.internalPath, subDir));
const sum = await this.downloader.downloadFile(libUrl, {
path: path.join(self.internalPath, fPath),
hash: sums ? "sha256" : null
});
if (sums) {
testSum(sums, sum, fPath);
}
}
};
module.exports = Dist;