From 5512dd7c4f28322d06fa12f47b1b57976a0ab666 Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Sun, 3 Feb 2019 13:41:33 -0500 Subject: [PATCH] daemon -> sdk --- .gitignore | 3 +- build/download-daemon.js | 88 --------------------------------- build/download-sdk.js | 89 ++++++++++++++++++++++++++++++++++ package.json | 10 ++-- src/main/index.html | 2 +- src/main/index.js | 16 +++--- src/main/{daemon.js => sdk.js} | 13 +++-- src/renderer/index.js | 12 ++--- 8 files changed, 116 insertions(+), 117 deletions(-) delete mode 100644 build/download-daemon.js create mode 100644 build/download-sdk.js rename src/main/{daemon.js => sdk.js} (72%) diff --git a/.gitignore b/.gitignore index 2c4ef6f..d5dfab3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ node_modules -static/daemon dist -build/daemon.ver +build/sdk.ver diff --git a/build/download-daemon.js b/build/download-daemon.js deleted file mode 100644 index 07f8bf4..0000000 --- a/build/download-daemon.js +++ /dev/null @@ -1,88 +0,0 @@ -const path = require("path"); -const fs = require("fs"); -const packageJSON = require("../package.json"); -const fetch = require("node-fetch"); -const decompress = require("decompress"); -const os = require("os"); -const del = require("del"); - -const downloadDaemon = targetPlatform => - new Promise((resolve, reject) => { - const daemonURLTemplate = packageJSON.lbrySettings.lbrynetDaemonUrlTemplate; - const daemonVersion = packageJSON.lbrySettings.lbrynetDaemonVersion; - const daemonDir = path.join(__dirname, "..", packageJSON.lbrySettings.lbrynetDaemonDir); - let daemonFileName = packageJSON.lbrySettings.lbrynetDaemonFileName; - - let currentPlatform = os.platform(); - - var daemonPlatform = process.env.TARGET || targetPlatform || currentPlatform; - if (daemonPlatform === "mac" || daemonPlatform === "darwin") daemonPlatform = "mac"; - if (daemonPlatform === "win32" || daemonPlatform === "windows") { - daemonPlatform = "windows"; - daemonFileName = daemonFileName + ".exe"; - } - const daemonFilePath = path.join(daemonDir, daemonFileName); - const daemonVersionPath = path.join(__dirname, "daemon.ver"); - const tmpZipPath = path.join(__dirname, "..", "dist", "daemon.zip"); - const daemonURL = daemonURLTemplate.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 - 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..."); - fetch(daemonURL, { - method: "GET", - headers: { - "Content-Type": "application/zip" - } - }) - .then(response => response.buffer()) - .then( - result => - new Promise((newResolve, newReject) => { - const distPath = path.join(__dirname, "..", "dist"); - const hasDistFolder = fs.existsSync(distPath); - - if (!hasDistFolder) { - fs.mkdirSync(distPath); - } - - fs.writeFile(tmpZipPath, result, error => { - if (error) return newReject(error); - return newResolve(); - }); - }) - ) - .then(() => del(`${daemonFilePath}*`)) - .then(() => - decompress(tmpZipPath, daemonDir, { - filter: file => path.basename(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 => { - console.error(`\x1b[31merror\x1b[0m Daemon download failed due to: \x1b[35m${error}\x1b[0m`); - reject(error); - }); - } - }); - -downloadDaemon(); diff --git a/build/download-sdk.js b/build/download-sdk.js new file mode 100644 index 0000000..7e21795 --- /dev/null +++ b/build/download-sdk.js @@ -0,0 +1,89 @@ +const path = require("path"); +const fs = require("fs"); +const packageJSON = require("../package.json"); +const fetch = require("node-fetch"); +const decompress = require("decompress"); +const os = require("os"); +const del = require("del"); + +// Downloads and extracts the LBRY SDK +const downloadSDK = targetPlatform => + new Promise((resolve, reject) => { + const sdkURLTemplate = packageJSON.lbrySettings.lbrynetSDKUrlTemplate; + const sdkVersion = packageJSON.lbrySettings.lbrynetSDKVersion; + const sdkDir = path.join(__dirname, "..", packageJSON.lbrySettings.lbrynetSDKDir); + let sdkFileName = packageJSON.lbrySettings.lbrynetSDKFileName; + + let currentPlatform = os.platform(); + + var sdkPlatform = process.env.TARGET || targetPlatform || currentPlatform; + if (sdkPlatform === "mac" || sdkPlatform === "darwin") sdkPlatform = "mac"; + if (sdkPlatform === "win32" || sdkPlatform === "windows") { + sdkPlatform = "windows"; + sdkFileName = sdkFileName + ".exe"; + } + const sdkFilePath = path.join(sdkDir, sdkFileName); + const sdkVersionPath = path.join(__dirname, "sdk.ver"); + const tmpZipPath = path.join(__dirname, "..", "dist", "sdk.zip"); + const sdkURL = sdkURLTemplate.replace(/SDKVER/g, sdkVersion).replace(/OSNAME/g, sdkPlatform); + + // If an sdk was perviously downloaded and sdk.ver exists, check to see if it matches the current sdk version + const hasSDKDownloaded = fs.existsSync(sdkFilePath); + const hasSDKVersion = fs.existsSync(sdkVersionPath); + let downloadedSDKVersion; + if (hasSDKVersion) { + downloadedSDKVersion = fs.readFileSync(sdkVersionPath, "utf8"); + } + + if (hasSDKDownloaded && hasSDKVersion && downloadedSDKVersion === sdkVersion) { + console.log("\x1b[34minfo\x1b[0m SDK already downloaded"); + resolve("Done"); + return; + } else { + console.log("\x1b[34minfo\x1b[0m Downloading SDK..."); + fetch(sdkURL, { + method: "GET", + headers: { + "Content-Type": "application/zip" + } + }) + .then(response => response.buffer()) + .then( + result => + new Promise((newResolve, newReject) => { + const distPath = path.join(__dirname, "..", "dist"); + const hasDistFolder = fs.existsSync(distPath); + + if (!hasDistFolder) { + fs.mkdirSync(distPath); + } + + fs.writeFile(tmpZipPath, result, error => { + if (error) return newReject(error); + return newResolve(); + }); + }) + ) + .then(() => del(`${sdkFilePath}*`)) + .then(() => + decompress(tmpZipPath, sdkDir, { + filter: file => path.basename(file.path) === sdkFileName + }) + ) + .then(() => { + console.log("\x1b[32msuccess\x1b[0m SDK downloaded!"); + if (hasSDKVersion) { + del(sdkVersionPath); + } + + fs.writeFileSync(sdkVersionPath, sdkVersion, "utf8"); + resolve("Done"); + }) + .catch(error => { + console.error(`\x1b[31merror\x1b[0m SDK download failed due to: \x1b[35m${error}\x1b[0m`); + reject(error); + }); + } + }); + +downloadSDK(); diff --git a/package.json b/package.json index fa0a100..4ef04cf 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "electron ./src/main", "dev": "NODE_ENV=development electron ./src/main/index.js", - "postinstall": "node build/download-daemon.js" + "postinstall": "node build/download-sdk.js" }, "dependencies": { "electron": "^4.0.4", @@ -21,10 +21,10 @@ "electron-reload": "^1.2.5" }, "lbrySettings": { - "lbrynetDaemonVersion": "0.30.4", - "lbrynetDaemonUrlTemplate": "https://github.com/lbryio/lbry/releases/download/vDAEMONVER/lbrynet-OSNAME.zip", - "lbrynetDaemonDir": "dist/daemon", - "lbrynetDaemonFileName": "lbrynet" + "lbrynetSDKVersion": "0.30.4", + "lbrynetSDKUrlTemplate": "https://github.com/lbryio/lbry/releases/download/vSDKVER/lbrynet-OSNAME.zip", + "lbrynetSDKDir": "dist/sdk", + "lbrynetSDKFileName": "lbrynet" }, "repository": { "type": "git", diff --git a/src/main/index.html b/src/main/index.html index 54b213c..58c7cc4 100644 --- a/src/main/index.html +++ b/src/main/index.html @@ -12,7 +12,7 @@

Hello world

-
Starting daemon...
+
Starting SDK...
diff --git a/src/main/index.js b/src/main/index.js index 36b4000..cc42741 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -3,7 +3,7 @@ const { app } = require("electron"); const path = require("path"); const findProcess = require("find-process"); const createWindow = require("./create-window"); -const Daemon = require("./daemon"); +const SDK = require("./sdk"); const IS_DEV = process.env.NODE_ENV === "development"; // Auto-reload when we make changes @@ -21,16 +21,16 @@ app.on("ready", function() { // 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 => { - const isDaemonRunning = processList.length > 0; + const isSDKRunning = processList.length > 0; - if (!isDaemonRunning) { - daemon = new Daemon(); - daemon.on("exit", () => { - daemon = null; - dialog.showErrorBox("Daemon has Exited"); + if (!isSDKRunning) { + sdk = new SDK(); + sdk.on("exit", () => { + sdk = null; + dialog.showErrorBox("SDK has quit"); app.quit(); }); - daemon.launch(); + sdk.launch(); } }); }); diff --git a/src/main/daemon.js b/src/main/sdk.js similarity index 72% rename from src/main/daemon.js rename to src/main/sdk.js index 17e1f64..6dbe13f 100644 --- a/src/main/daemon.js +++ b/src/main/sdk.js @@ -1,20 +1,19 @@ const path = require("path"); const { spawn, execSync } = require("child_process"); -class Daemon { +class SDK { constructor() { - this.path = process.env.LBRY_DAEMON || path.join(__dirname, "../../dist/daemon/lbrynet"); + this.path = process.env.LBRY_DAEMON || path.join(__dirname, "../../dist/sdk/lbrynet"); this.handlers = []; this.subprocess = undefined; } launch() { - console.log("launching daemon"); this.subprocess = spawn(this.path, ["start"]); - this.subprocess.stdout.on("data", data => console.log(`Daemon: ${data}`)); - this.subprocess.stderr.on("data", data => console.error(`Daemon: ${data}`)); + this.subprocess.stdout.on("data", data => console.log(`SDK: ${data}`)); + this.subprocess.stderr.on("data", data => console.error(`SDK: ${data}`)); this.subprocess.on("exit", () => this.fire("exit")); - this.subprocess.on("error", error => console.error(`Daemon error: ${error}`)); + this.subprocess.on("error", error => console.error(`SDK error: ${error}`)); } quit() { @@ -44,4 +43,4 @@ class Daemon { } } -module.exports = Daemon; +module.exports = SDK; diff --git a/src/renderer/index.js b/src/renderer/index.js index dcfb04d..1cbf4b1 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -1,12 +1,12 @@ const { Lbry } = require("lbry-redux"); -Lbry.connect().then(checkDaemonStarted); +Lbry.connect().then(checkSDKStarted); -// This waits until the daemon is fully started before doing anything else -function checkDaemonStarted() { +// Wait until the sdk is fully started before doing anything else +function checkSDKStarted() { Lbry.status().then(status => { if (status.is_running) { - // Daemon is now running + // SDK is now running const resolveWrapper = document.getElementById("resolve"); const loadingWrapper = document.getElementById("loading"); loadingWrapper.style.display = "none"; @@ -15,8 +15,8 @@ function checkDaemonStarted() { } setTimeout(() => { - checkDaemonStarted(); - }, 250); + checkSDKStarted(); + }, 500); }); }