2018-03-14 21:38:55 +01:00
|
|
|
/* eslint-disable no-console,import/no-extraneous-dependencies,import/no-commonjs */
|
2018-03-08 00:03:45 +01:00
|
|
|
const path = require('path');
|
2018-03-29 21:57:17 +02:00
|
|
|
const fs = require('fs-path');
|
2018-03-08 00:03:45 +01:00
|
|
|
const packageJSON = require('../package.json');
|
|
|
|
const axios = require('axios');
|
|
|
|
const decompress = require('decompress');
|
|
|
|
const os = require('os');
|
2018-03-14 21:38:55 +01:00
|
|
|
const del = require('del');
|
2018-03-08 00:03:45 +01:00
|
|
|
|
2018-03-14 21:38:55 +01:00
|
|
|
const downloadDaemon = targetPlatform =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const daemonURLTemplate = packageJSON.lbrySettings.lbrynetDaemonUrlTemplate;
|
|
|
|
const daemonVersion = packageJSON.lbrySettings.lbrynetDaemonVersion;
|
|
|
|
const daemonDir = packageJSON.lbrySettings.lbrynetDaemonDir;
|
|
|
|
const daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName;
|
2018-03-08 00:03:45 +01:00
|
|
|
|
2018-03-14 21:38:55 +01:00
|
|
|
let currentPlatform = os.platform();
|
|
|
|
if (currentPlatform === 'darwin') currentPlatform = 'macos';
|
|
|
|
if (currentPlatform === 'win32') currentPlatform = 'windows';
|
2018-03-08 00:03:45 +01:00
|
|
|
|
2018-03-14 21:38:55 +01:00
|
|
|
const daemonPlatform = targetPlatform || currentPlatform;
|
|
|
|
|
|
|
|
const daemonURL = daemonURLTemplate
|
|
|
|
.replace(/DAEMONVER/g, daemonVersion)
|
|
|
|
.replace(/OSNAME/g, daemonPlatform);
|
2018-03-28 00:42:26 +02:00
|
|
|
const tmpZipPath = 'dist/daemon.zip';
|
2018-03-14 21:38:55 +01:00
|
|
|
|
|
|
|
console.log('\x1b[34minfo\x1b[0m Downloading daemon...');
|
|
|
|
axios
|
|
|
|
.request({
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
url: daemonURL,
|
|
|
|
method: 'get',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/zip',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
result =>
|
|
|
|
new Promise((newResolve, newReject) => {
|
|
|
|
fs.writeFile(tmpZipPath, result.data, error => {
|
|
|
|
if (error) return newReject(error);
|
|
|
|
return newResolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(() => del(`${daemonDir}/${daemonFileName}*`))
|
|
|
|
.then(() =>
|
|
|
|
decompress(tmpZipPath, daemonDir, {
|
|
|
|
filter: file =>
|
|
|
|
path.basename(file.path).replace(path.extname(file.path), '') === daemonFileName,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
console.log('\x1b[32msuccess\x1b[0m Daemon downloaded!');
|
|
|
|
resolve(true);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(
|
|
|
|
`\x1b[31merror\x1b[0m Daemon download failed due to: \x1b[35m${error}\x1b[0m`
|
|
|
|
);
|
|
|
|
reject(error);
|
|
|
|
});
|
2018-03-08 00:03:45 +01:00
|
|
|
});
|
2018-03-14 21:38:55 +01:00
|
|
|
|
|
|
|
module.exports = downloadDaemon;
|
|
|
|
|
|
|
|
require('make-runnable/custom')({
|
|
|
|
printOutputFrame: false
|
|
|
|
});
|