Merge pull request #1787 from lbryio/daemon-download

check if daemon is already installed before installing again
This commit is contained in:
Sean Yesmunt 2018-07-20 11:16:42 -04:00 committed by GitHub
commit 54f5891df7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 43 deletions

5
.gitignore vendored
View file

@ -1,7 +1,8 @@
/node_modules /node_modules
/dist /dist
/static/daemon/lbrynet* /static/daemon/lbrynet-daemon*
/static/locales /static/locales
yarn-error.log yarn-error.log
package-lock.json package-lock.json
.idea/ .idea/
/build/daemon.ver

View file

@ -1,6 +1,6 @@
/* eslint-disable no-console,import/no-extraneous-dependencies,import/no-commonjs */ /* eslint-disable no-console,import/no-extraneous-dependencies,import/no-commonjs */
const path = require('path'); const path = require('path');
const fs = require('fs-path'); const fs = require('fs');
const packageJSON = require('../package.json'); const packageJSON = require('../package.json');
const axios = require('axios'); const axios = require('axios');
const decompress = require('decompress'); const decompress = require('decompress');
@ -11,56 +11,80 @@ const downloadDaemon = targetPlatform =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const daemonURLTemplate = packageJSON.lbrySettings.lbrynetDaemonUrlTemplate; const daemonURLTemplate = packageJSON.lbrySettings.lbrynetDaemonUrlTemplate;
const daemonVersion = packageJSON.lbrySettings.lbrynetDaemonVersion; const daemonVersion = packageJSON.lbrySettings.lbrynetDaemonVersion;
const daemonDir = packageJSON.lbrySettings.lbrynetDaemonDir; const daemonDir = path.join(__dirname,'..',packageJSON.lbrySettings.lbrynetDaemonDir);
const daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName; let daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName;
let currentPlatform = os.platform(); let currentPlatform = os.platform();
if (currentPlatform === 'darwin') currentPlatform = 'macos'; if (currentPlatform === 'darwin') currentPlatform = 'macos';
if (currentPlatform === 'win32') currentPlatform = 'windows'; if (currentPlatform === 'win32') {
currentPlatform = 'windows';
daemonFileName = daemonFileName + '.exe';
}
const daemonFilePath = path.join(daemonDir, daemonFileName);
const daemonVersionPath = path.join(__dirname, 'daemon.ver');
const daemonPlatform = targetPlatform || currentPlatform; const daemonPlatform = targetPlatform || currentPlatform;
const tmpZipPath = path.join(__dirname, '../','dist','daemon.zip');
const daemonURL = daemonURLTemplate const daemonURL = daemonURLTemplate
.replace(/DAEMONVER/g, daemonVersion) .replace(/DAEMONVER/g, daemonVersion)
.replace(/OSNAME/g, daemonPlatform); .replace(/OSNAME/g, daemonPlatform);
const tmpZipPath = 'dist/daemon.zip';
console.log('\x1b[34minfo\x1b[0m Downloading daemon...');
axios // If a daemon and daemon.ver exists, check to see if it matches the current daemon version
.request({ const hasDaemonDownloaded = fs.existsSync(daemonFilePath);
responseType: 'arraybuffer', const hasDaemonVersion = fs.existsSync(daemonVersionPath);
url: daemonURL, let downloadedDaemonVersion;
method: 'get', if (hasDaemonVersion) {
headers: { downloadedDaemonVersion = fs.readFileSync(daemonVersionPath, "utf8");
'Content-Type': 'application/zip', }
},
}) if (hasDaemonDownloaded && hasDaemonVersion && downloadedDaemonVersion === daemonVersion) {
.then( console.log('\x1b[34minfo\x1b[0m Daemon already downloaded');
result => resolve('Done');
new Promise((newResolve, newReject) => { return;
fs.writeFile(tmpZipPath, result.data, error => { } else {
if (error) return newReject(error); console.log('\x1b[34minfo\x1b[0m Downloading daemon...');
return newResolve(); axios
}); .request({
}) responseType: 'arraybuffer',
) url: daemonURL,
.then(() => del(`${daemonDir}/${daemonFileName}*`)) method: 'get',
.then(() => headers: {
decompress(tmpZipPath, daemonDir, { 'Content-Type': 'application/zip',
filter: file => },
path.basename(file.path).replace(path.extname(file.path), '') === daemonFileName,
}) })
) .then(
.then(() => { result =>
console.log('\x1b[32msuccess\x1b[0m Daemon downloaded!'); new Promise((newResolve, newReject) => {
resolve(true); fs.writeFile(tmpZipPath, result.data, error => {
})
.catch(error => { if (error) return newReject(error);
console.error( return newResolve();
`\x1b[31merror\x1b[0m Daemon download failed due to: \x1b[35m${error}\x1b[0m` });
); })
reject(error); )
}); .then(() => del(`${daemonFilePath}*`))
.then(() => decompress(tmpZipPath, daemonDir, {
filter: file =>
path.basename(file.path) === daemonFileName,
}))
.then(() => del(`${tmpZipPath }*`))
.then(() => {
console.log('\x1b[32msuccess\x1b[0m Daemon downloaded!');
if (hasDaemonVersion) {
del(daemonVersionPath);
}
fs.writeFileSync(daemonVersionPath, daemonVersion, "utf8")
resolve('Done');
})
.catch(error => {
console.error(
`\x1b[31merror\x1b[0m Daemon download failed due to: \x1b[35m${error}\x1b[0m`
);
reject(error);
});
}
}); });
module.exports = downloadDaemon; module.exports = downloadDaemon;