-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathdist.js
176 lines (163 loc) · 4.43 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
'use strict'
const environment = require('./environment')
const path = require('path')
const urljoin = require('url-join')
const fs = require('fs-extra')
const CMLog = require('./cmLog')
const TargetOptions = require('./targetOptions')
const runtimePaths = require('./runtimePaths')
const Downloader = require('./downloader')
const os = require('os')
function testSum(sums, sum, fPath) {
const serverSum = sums.find(function (s) {
return s.getPath === fPath
})
if (serverSum && serverSum.sum === sum) {
return
}
throw new Error("SHA sum of file '" + fPath + "' mismatch!")
}
class Dist {
get internalPath() {
const cacheDirectory = '.cmake-js'
const runtimeArchDirectory = this.targetOptions.runtime + '-' + this.targetOptions.arch
const runtimeVersionDirectory = 'v' + this.targetOptions.runtimeVersion
return (
this.options.runtimeDirectory ||
path.join(os.homedir(), cacheDirectory, runtimeArchDirectory, runtimeVersionDirectory)
)
}
get externalPath() {
return runtimePaths.get(this.targetOptions).externalPath
}
get downloaded() {
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 (const 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: () => false,
isDirectory: () => false,
}
}
}
}
get winLibs() {
const libs = runtimePaths.get(this.targetOptions).winLibs
const result = []
for (const lib of libs) {
result.push(path.join(this.internalPath, lib.dir, lib.name))
}
return result
}
get headerOnly() {
return runtimePaths.get(this.targetOptions).headerOnly
}
constructor(options) {
this.options = options || {}
this.log = new CMLog(this.options)
this.targetOptions = new TargetOptions(this.options)
this.downloader = new Downloader(this.options)
}
async ensureDownloaded() {
if (!this.downloaded) {
await this.download()
}
}
async download() {
const log = this.log
log.info('DIST', 'Downloading distribution files to: ' + this.internalPath)
await fs.ensureDir(this.internalPath)
const sums = await this._downloadShaSums()
await Promise.all([this._downloadLibs(sums), this._downloadTar(sums)])
}
async _downloadShaSums() {
if (this.targetOptions.runtime === 'node') {
const sumUrl = urljoin(this.externalPath, 'SHASUMS256.txt')
const log = this.log
log.http('DIST', '\t- ' + sumUrl)
return (await this.downloader.downloadString(sumUrl))
.split('\n')
.map(function (line) {
const parts = line.split(/\s+/)
return {
getPath: parts[1],
sum: parts[0],
}
})
.filter(function (i) {
return i.getPath && i.sum
})
} else {
return null
}
}
async _downloadTar(sums) {
const log = this.log
const self = this
const tarLocalPath = runtimePaths.get(self.targetOptions).tarPath
const tarUrl = urljoin(self.externalPath, tarLocalPath)
log.http('DIST', '\t- ' + tarUrl)
const sum = await this.downloader.downloadTgz(tarUrl, {
hash: sums ? 'sha256' : null,
cwd: self.internalPath,
strip: 1,
filter: function (entryPath) {
if (entryPath === self.internalPath) {
return true
}
const ext = path.extname(entryPath)
return ext && ext.toLowerCase() === '.h'
},
})
if (sums) {
testSum(sums, sum, tarLocalPath)
}
}
async _downloadLibs(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