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-07-23 22:32:25 +02:00
|
|
|
const fs = require('fs');
|
2018-03-08 00:03:45 +01:00
|
|
|
const packageJSON = require('../package.json');
|
2019-01-16 05:33:06 +01:00
|
|
|
const fetch = require('node-fetch');
|
2018-03-08 00:03:45 +01:00
|
|
|
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;
|
2019-01-16 05:33:06 +01:00
|
|
|
const daemonDir = path.join(__dirname, '..', packageJSON.lbrySettings.lbrynetDaemonDir);
|
2018-07-23 22:32:25 +02:00
|
|
|
let daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName;
|
2018-03-08 00:03:45 +01:00
|
|
|
|
2019-01-16 05:33:06 +01:00
|
|
|
const currentPlatform = os.platform();
|
2018-07-21 11:22:28 +02:00
|
|
|
|
2019-01-16 05:33:06 +01:00
|
|
|
let daemonPlatform = process.env.TARGET || targetPlatform || currentPlatform;
|
2018-10-26 06:20:18 +02:00
|
|
|
if (daemonPlatform === 'mac' || daemonPlatform === 'darwin') daemonPlatform = 'mac';
|
2018-07-23 22:32:25 +02:00
|
|
|
if (daemonPlatform === 'win32' || daemonPlatform === 'windows') {
|
2019-01-16 05:33:06 +01:00
|
|
|
daemonPlatform = 'windows';
|
|
|
|
daemonFileName += '.exe';
|
2018-07-23 22:32:25 +02:00
|
|
|
}
|
|
|
|
const daemonFilePath = path.join(daemonDir, daemonFileName);
|
|
|
|
const daemonVersionPath = path.join(__dirname, 'daemon.ver');
|
|
|
|
const tmpZipPath = path.join(__dirname, '..', 'dist', 'daemon.zip');
|
2018-03-14 21:38:55 +01:00
|
|
|
const daemonURL = daemonURLTemplate
|
|
|
|
.replace(/DAEMONVER/g, daemonVersion)
|
|
|
|
.replace(/OSNAME/g, daemonPlatform);
|
2018-07-23 22:32:25 +02:00
|
|
|
|
|
|
|
// If a daemon and daemon.ver exists, check to see if it matches the current daemon version
|
|
|
|
const hasDaemonDownloaded = fs.existsSync(daemonFilePath);
|
|
|
|
const hasDaemonVersion = fs.existsSync(daemonVersionPath);
|
|
|
|
let downloadedDaemonVersion;
|
|
|
|
if (hasDaemonVersion) {
|
2019-01-16 05:33:06 +01:00
|
|
|
downloadedDaemonVersion = fs.readFileSync(daemonVersionPath, 'utf8');
|
2018-07-23 22:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hasDaemonDownloaded && hasDaemonVersion && downloadedDaemonVersion === daemonVersion) {
|
|
|
|
console.log('\x1b[34minfo\x1b[0m Daemon already downloaded');
|
|
|
|
resolve('Done');
|
|
|
|
} else {
|
|
|
|
console.log('\x1b[34minfo\x1b[0m Downloading daemon...');
|
2019-01-16 05:33:06 +01:00
|
|
|
fetch(daemonURL, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/zip',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(response => response.buffer())
|
2018-07-23 22:32:25 +02:00
|
|
|
.then(
|
|
|
|
result =>
|
|
|
|
new Promise((newResolve, newReject) => {
|
|
|
|
const distPath = path.join(__dirname, '..', 'dist');
|
|
|
|
const hasDistFolder = fs.existsSync(distPath);
|
2018-07-19 15:42:57 +02:00
|
|
|
|
2018-07-23 22:32:25 +02:00
|
|
|
if (!hasDistFolder) {
|
|
|
|
fs.mkdirSync(distPath);
|
|
|
|
}
|
|
|
|
|
2019-01-16 05:33:06 +01:00
|
|
|
fs.writeFile(tmpZipPath, result, error => {
|
2018-07-23 22:32:25 +02:00
|
|
|
if (error) return newReject(error);
|
|
|
|
return newResolve();
|
|
|
|
});
|
2019-01-16 05:33:06 +01:00
|
|
|
})
|
2018-07-23 22:32:25 +02:00
|
|
|
)
|
|
|
|
.then(() => del(`${daemonFilePath}*`))
|
2019-01-16 05:33:06 +01:00
|
|
|
.then(() =>
|
|
|
|
decompress(tmpZipPath, daemonDir, {
|
|
|
|
filter: file => path.basename(file.path) === daemonFileName,
|
|
|
|
})
|
|
|
|
)
|
2018-07-23 22:32:25 +02:00
|
|
|
.then(() => {
|
|
|
|
console.log('\x1b[32msuccess\x1b[0m Daemon downloaded!');
|
|
|
|
if (hasDaemonVersion) {
|
|
|
|
del(daemonVersionPath);
|
|
|
|
}
|
|
|
|
|
2019-01-16 05:33:06 +01:00
|
|
|
fs.writeFileSync(daemonVersionPath, daemonVersion, 'utf8');
|
2018-07-23 22:32:25 +02:00
|
|
|
resolve('Done');
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(
|
|
|
|
`\x1b[31merror\x1b[0m Daemon download failed due to: \x1b[35m${error}\x1b[0m`
|
|
|
|
);
|
|
|
|
reject(error);
|
2019-01-16 05:33:06 +01:00
|
|
|
});
|
|
|
|
}
|
2018-03-08 00:03:45 +01:00
|
|
|
});
|
2018-03-14 21:38:55 +01:00
|
|
|
|
2019-03-05 05:46:57 +01:00
|
|
|
downloadDaemon();
|