check if daemon is already installed before installing again

This commit is contained in:
Sean Yesmunt 2018-07-18 17:23:26 -04:00
parent 1c55774165
commit 935f656add
2 changed files with 59 additions and 38 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');
@ -13,54 +13,74 @@ const downloadDaemon = targetPlatform =>
const daemonVersion = packageJSON.lbrySettings.lbrynetDaemonVersion; const daemonVersion = packageJSON.lbrySettings.lbrynetDaemonVersion;
const daemonDir = packageJSON.lbrySettings.lbrynetDaemonDir; const daemonDir = packageJSON.lbrySettings.lbrynetDaemonDir;
const daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName; const daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName;
const daemonFilePath = `${__dirname}/../${daemonDir}/${daemonFileName}`;
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';
const daemonVersionPath = __dirname + '/daemon.ver';
const daemonPlatform = targetPlatform || currentPlatform; const daemonPlatform = targetPlatform || currentPlatform;
const tmpZipPath = __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';
// 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) {
downloadedDaemonVersion = fs.readFileSync(daemonVersionPath, "utf8");
}
if (hasDaemonDownloaded && hasDaemonVersion && downloadedDaemonVersion === daemonVersion) {
console.log('\x1b[34minfo\x1b[0m Daemon already downloaded');
resolve('Done');
return;
} else {
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 => {
console.log('\x1b[34minfo\x1b[0m Downloading daemon...'); if (error) return newReject(error);
axios return newResolve();
.request({ });
responseType: 'arraybuffer', })
url: daemonURL, )
method: 'get', .then(() => del(`${daemonDir}/${daemonFileName}*`))
headers: { .then(() => decompress(tmpZipPath, daemonDir, {
'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 => filter: file =>
path.basename(file.path).replace(path.extname(file.path), '') === daemonFileName, path.basename(file.path).replace(path.extname(file.path), '') === daemonFileName,
}))
.then(() => {
console.log('\x1b[32msuccess\x1b[0m Daemon downloaded!');
if (hasDaemonVersion) {
del(daemonVersionPath);
}
fs.writeFileSync(daemonVersionPath, daemonVersion, "utf8")
resolve('Done');
}) })
) .catch(error => {
.then(() => { console.error(
console.log('\x1b[32msuccess\x1b[0m Daemon downloaded!'); `\x1b[31merror\x1b[0m Daemon download failed due to: \x1b[35m${error}\x1b[0m`
resolve(true); );
}) reject(error);
.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;