This commit is contained in:
Sean Yesmunt 2019-02-03 13:14:46 -05:00
parent 1731530253
commit 8ee5c5c667
7 changed files with 455 additions and 165 deletions

View file

@ -1,8 +1,7 @@
/* eslint-disable no-console,import/no-extraneous-dependencies,import/no-commonjs */
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const packageJSON = require("../package.json"); const packageJSON = require("../package.json");
const axios = require("axios"); const fetch = require("node-fetch");
const decompress = require("decompress"); const decompress = require("decompress");
const os = require("os"); const os = require("os");
const del = require("del"); const del = require("del");
@ -11,19 +10,13 @@ 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 = path.join( const daemonDir = path.join(__dirname, "..", packageJSON.lbrySettings.lbrynetDaemonDir);
__dirname,
"..",
packageJSON.lbrySettings.lbrynetDaemonDir
);
let daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName; let daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName;
let currentPlatform = os.platform(); let currentPlatform = os.platform();
var daemonPlatform = var daemonPlatform = process.env.TARGET || targetPlatform || currentPlatform;
process.env.TARGET || targetPlatform || currentPlatform; if (daemonPlatform === "mac" || daemonPlatform === "darwin") daemonPlatform = "mac";
if (daemonPlatform === "mac" || daemonPlatform === "darwin")
daemonPlatform = "mac";
if (daemonPlatform === "win32" || daemonPlatform === "windows") { if (daemonPlatform === "win32" || daemonPlatform === "windows") {
daemonPlatform = "windows"; daemonPlatform = "windows";
daemonFileName = daemonFileName + ".exe"; daemonFileName = daemonFileName + ".exe";
@ -31,9 +24,7 @@ const downloadDaemon = targetPlatform =>
const daemonFilePath = path.join(daemonDir, daemonFileName); const daemonFilePath = path.join(daemonDir, daemonFileName);
const daemonVersionPath = path.join(__dirname, "daemon.ver"); const daemonVersionPath = path.join(__dirname, "daemon.ver");
const tmpZipPath = path.join(__dirname, "..", "dist", "daemon.zip"); const tmpZipPath = path.join(__dirname, "..", "dist", "daemon.zip");
const daemonURL = daemonURLTemplate const daemonURL = daemonURLTemplate.replace(/DAEMONVER/g, daemonVersion).replace(/OSNAME/g, daemonPlatform);
.replace(/DAEMONVER/g, daemonVersion)
.replace(/OSNAME/g, daemonPlatform);
// If a daemon and daemon.ver exists, check to see if it matches the current daemon version // If a daemon and daemon.ver exists, check to see if it matches the current daemon version
const hasDaemonDownloaded = fs.existsSync(daemonFilePath); const hasDaemonDownloaded = fs.existsSync(daemonFilePath);
@ -43,25 +34,19 @@ const downloadDaemon = targetPlatform =>
downloadedDaemonVersion = fs.readFileSync(daemonVersionPath, "utf8"); downloadedDaemonVersion = fs.readFileSync(daemonVersionPath, "utf8");
} }
if ( if (hasDaemonDownloaded && hasDaemonVersion && downloadedDaemonVersion === daemonVersion) {
hasDaemonDownloaded &&
hasDaemonVersion &&
downloadedDaemonVersion === daemonVersion
) {
console.log("\x1b[34minfo\x1b[0m Daemon already downloaded"); console.log("\x1b[34minfo\x1b[0m Daemon already downloaded");
resolve("Done"); resolve("Done");
return; return;
} else { } else {
console.log("\x1b[34minfo\x1b[0m Downloading daemon..."); console.log("\x1b[34minfo\x1b[0m Downloading daemon...");
axios fetch(daemonURL, {
.request({ method: "GET",
responseType: "arraybuffer",
url: daemonURL,
method: "get",
headers: { headers: {
"Content-Type": "application/zip" "Content-Type": "application/zip"
} }
}) })
.then(response => response.buffer())
.then( .then(
result => result =>
new Promise((newResolve, newReject) => { new Promise((newResolve, newReject) => {
@ -72,7 +57,7 @@ const downloadDaemon = targetPlatform =>
fs.mkdirSync(distPath); fs.mkdirSync(distPath);
} }
fs.writeFile(tmpZipPath, result.data, error => { fs.writeFile(tmpZipPath, result, error => {
if (error) return newReject(error); if (error) return newReject(error);
return newResolve(); return newResolve();
}); });
@ -94,9 +79,7 @@ const downloadDaemon = targetPlatform =>
resolve("Done"); resolve("Done");
}) })
.catch(error => { .catch(error => {
console.error( console.error(`\x1b[31merror\x1b[0m Daemon download failed due to: \x1b[35m${error}\x1b[0m`);
`\x1b[31merror\x1b[0m Daemon download failed due to: \x1b[35m${error}\x1b[0m`
);
reject(error); reject(error);
}); });
} }

View file

@ -8,32 +8,29 @@
"dev": "NODE_ENV=development electron ./src/main/index.js", "dev": "NODE_ENV=development electron ./src/main/index.js",
"postinstall": "node build/download-daemon.js" "postinstall": "node build/download-daemon.js"
}, },
"repository": {
"type": "git",
"url": "git+https://github.com/lbry-developers/lbry-electron-starter.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/lbry-developers/lbry-electron-starter/issues"
},
"homepage": "https://github.com/lbry-developers/lbry-electron-starter#readme",
"dependencies": { "dependencies": {
"electron": "^3.0.8", "electron": "^4.0.4",
"find-process": "^1.2.0", "find-process": "^1.2.0",
"lbry-redux": "lbryio/lbry-redux", "lbry-redux": "lbryio/lbry-redux",
"node-fetch": "^2.3.0",
"url": "^0.11.0" "url": "^0.11.0"
}, },
"devDependencies": { "devDependencies": {
"axios": "^0.18.0",
"decompress": "^4.2.0", "decompress": "^4.2.0",
"del": "^3.0.0", "del": "^3.0.0",
"electron-reload": "^1.2.5" "electron-reload": "^1.2.5"
}, },
"lbrySettings": { "lbrySettings": {
"lbrynetDaemonVersion": "0.30.0", "lbrynetDaemonVersion": "0.30.4",
"lbrynetDaemonUrlTemplate": "https://github.com/lbryio/lbry/releases/download/vDAEMONVER/lbrynet-OSNAME.zip", "lbrynetDaemonUrlTemplate": "https://github.com/lbryio/lbry/releases/download/vDAEMONVER/lbrynet-OSNAME.zip",
"lbrynetDaemonDir": "static/daemon", "lbrynetDaemonDir": "dist/daemon",
"lbrynetDaemonFileName": "lbrynet" "lbrynetDaemonFileName": "lbrynet"
} },
"repository": {
"type": "git",
"url": "git+https://github.com/lbry-developers/lbry-electron-starter.git"
},
"author": "https://github.com/lbryio/electron-starter/graphs/contributors",
"license": "MIT",
"homepage": "https://github.com/lbry-developers/lbry-electron-starter#readme"
} }

View file

@ -3,21 +3,18 @@ const { spawn, execSync } = require("child_process");
class Daemon { class Daemon {
constructor() { constructor() {
this.path = this.path = process.env.LBRY_DAEMON || path.join(__dirname, "../../dist/daemon/lbrynet");
process.env.LBRY_DAEMON ||
path.join(__dirname, "../../static/daemon/lbrynet");
this.handlers = []; this.handlers = [];
this.subprocess = undefined; this.subprocess = undefined;
} }
launch() { launch() {
console.log("launching daemon");
this.subprocess = spawn(this.path, ["start"]); this.subprocess = spawn(this.path, ["start"]);
this.subprocess.stdout.on("data", data => console.log(`Daemon: ${data}`)); this.subprocess.stdout.on("data", data => console.log(`Daemon: ${data}`));
this.subprocess.stderr.on("data", data => console.error(`Daemon: ${data}`)); this.subprocess.stderr.on("data", data => console.error(`Daemon: ${data}`));
this.subprocess.on("exit", () => this.fire("exit")); this.subprocess.on("exit", () => this.fire("exit"));
this.subprocess.on("error", error => this.subprocess.on("error", error => console.error(`Daemon error: ${error}`));
console.error(`Daemon error: ${error}`)
);
} }
quit() { quit() {

View file

@ -5,13 +5,6 @@
<title>LBRY Starter</title> <title>LBRY Starter</title>
</head> </head>
<style> <style>
body: {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#resolve { #resolve {
display: none; display: none;
} }

View file

@ -17,26 +17,18 @@ if (IS_DEV) {
app.on("ready", function() { app.on("ready", function() {
createWindow(); createWindow();
const processListArgs = // Determine if the LBRY SDK is already running, or if it needs to be started
process.platform === "win32" ? "lbrynet" : "lbrynet start"; // This allows you to run the sdk binary separately and have your app connect to it
const processListArgs = process.platform === "win32" ? "lbrynet" : "lbrynet start";
findProcess("name", processListArgs).then(processList => { findProcess("name", processListArgs).then(processList => {
const isDaemonRunning = processList.length > 0; const isDaemonRunning = processList.length > 0;
if (!isDaemonRunning) { if (!isDaemonRunning) {
daemon = new Daemon(); daemon = new Daemon();
daemon.on("exit", () => { daemon.on("exit", () => {
if (!isDev) {
daemon = null; daemon = null;
if (!appState.isQuitting) { dialog.showErrorBox("Daemon has Exited");
dialog.showErrorBox(
"Daemon has Exited",
"The daemon may have encountered an unexpected error, or another daemon instance is already running. \n\n" +
"For more information please visit: \n" +
"https://lbry.io/faq/startup-troubleshooting"
);
}
app.quit(); app.quit();
}
}); });
daemon.launch(); daemon.launch();
} }

View file

@ -2,20 +2,21 @@ const { Lbry } = require("lbry-redux");
Lbry.connect().then(checkDaemonStarted); Lbry.connect().then(checkDaemonStarted);
// This waits until the daemon is fully started before doing anything else
function checkDaemonStarted() { function checkDaemonStarted() {
Lbry.status().then(status => { Lbry.status().then(status => {
if (status.is_running) { if (status.is_running) {
// Daemon is now running // Daemon is now running
const resolveWrapper = document.getElementById("resolve"); const resolveWrapper = document.getElementById("resolve");
const loadingWrapper = document.getElementById("loading"); const loadingWrapper = document.getElementById("loading");
loadingWrapper.style.display = "none"; loadingWrapper.style.display = "none";
resolveWrapper.style.display = "block"; resolveWrapper.style.display = "block";
return;
} }
setTimeout(() => { setTimeout(() => {
checkDaemonStarted(); checkDaemonStarted();
}, 500); }, 250);
}); });
} }

505
yarn.lock

File diff suppressed because it is too large Load diff