-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinstall.js
51 lines (39 loc) · 1.55 KB
/
install.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
'use strict';
const os = require('os');
const https = require('https');
const {execSync} = require('child_process');
const fs = require('fs-extra');
const tracer = require('tracer');
const logger = tracer.console({format: "{{timestamp}} <{{title}}> {{message}}"});
const baseUrl = 'https://github.com/alekzonder/catapult/releases/download';
const release = 'wpr-build%2F2019.12.17-5';
const file = `wpr-${os.platform()}.tgz`;
const downloadUrl = `${baseUrl}/${release}/${file}`;
logger.info(`download wpr binaries: ${downloadUrl}`);
const targetDir = `${__dirname}/wpr`;
const archiveFilepath = `${__dirname}/wpr.tgz`;
const archiveStream = fs.createWriteStream(archiveFilepath);
if (fs.existsSync(targetDir)) {
logger.info(`remove ${targetDir}`);
fs.removeSync(targetDir);
}
https.get(downloadUrl, (firstRes) => {
// github redirect to amazonaws
if (firstRes.statusCode === 302) {
logger.info(`got redirect to: ${firstRes.headers.location}`);
https.get(firstRes.headers.location, (redirectedRes) => {
logger.info(`download ${redirectedRes.headers['content-length']} bytes`);
redirectedRes.pipe(archiveStream);
});
} else {
throw new Error(`cant download WPR binaries, download and unpack manually: ${downloadUrl}`);
}
});
archiveStream.on('finish', () => {
logger.info(`downloaded, untar to ${targetDir}`);
const cmd = `tar xzf ${archiveFilepath}`;
execSync(cmd);
logger.info(`cleanup, remove ${archiveFilepath}`);
fs.removeSync(archiveFilepath);
logger.info('done');
});