This repository has been archived by the owner on May 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
preinstall.js
91 lines (83 loc) · 2.16 KB
/
preinstall.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
const fs = require('fs')
const path = require('path')
const https = require('https')
const { libcoreVersion } = require('./package.json')
const perPlatform = {
linux: {
dir: 'linux',
files: ['libledger-core.so'],
chmod: 0o755,
},
darwin: {
dir: 'macos',
files: ['libledger-core.dylib'],
chmod: 0o755,
},
win32: {
dir: 'win/vs2017',
files: ['ledger-core.dll', 'ledger-core.lib', 'crypto.dll'],
},
}
const conf = perPlatform[process.platform]
if (!conf) {
console.error(`Platform ${process.platform} is not supported`)
process.exit(1)
}
const dir = conf.dir
// const dir =
// process.platform === 'linux' && Number(process.version.match(/^v(\d+\.\d+)/)[1]) >= 10
// ? `${conf.dir}-arch_ssl_1_1`
// : conf.dir
const endpointURL = `https://s3-eu-west-1.amazonaws.com/ledger-lib-ledger-core/${libcoreVersion}/${dir}`
if (!fs.existsSync('lib')) {
fs.mkdirSync('lib')
}
conf.files.reduce(
(p, file) =>
p.then(() =>
get(file)
.then(() => {
if (conf.chmod) {
fs.chmodSync(`lib/${file}`, conf.chmod)
}
})
.catch(err => {
console.error(`Error: ${err.message}, statusCode: ${err.code}`)
process.exit(1)
}),
),
Promise.resolve(),
)
function get(file) {
return new Promise((resolve, reject) => {
const dest = `lib/${file}`
if (process.env.LEDGER_CORE_LOCAL_BUILD) {
const src = path.join(process.env.LEDGER_CORE_LOCAL_BUILD, file)
console.log(`Copy ${src} -> ${dest}`)
fs.copyFile(src, dest, err => {
if (err) reject(err)
else resolve()
})
} else {
const url = `${endpointURL}/${file}`
console.log(`Downloading ${url} ...`)
const f = fs.createWriteStream(dest)
f.on('finish', () => {
console.log(`${file} downloaded.`)
f.close(resolve)
})
https
.get(url, res => {
if (res.statusCode === 404) {
reject({ code: res.statusCode, message: res.statusMessage })
} else {
res.pipe(f)
}
})
.on('error', err => {
fs.unlink(dest)
reject(err)
})
}
})
}