daemon -> sdk

This commit is contained in:
Sean Yesmunt 2019-02-03 13:41:33 -05:00
parent 8ee5c5c667
commit 5512dd7c4f
8 changed files with 116 additions and 117 deletions

3
.gitignore vendored
View file

@ -1,4 +1,3 @@
node_modules node_modules
static/daemon
dist dist
build/daemon.ver build/sdk.ver

View file

@ -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();

89
build/download-sdk.js Normal file
View file

@ -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();

View file

@ -6,7 +6,7 @@
"scripts": { "scripts": {
"start": "electron ./src/main", "start": "electron ./src/main",
"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-sdk.js"
}, },
"dependencies": { "dependencies": {
"electron": "^4.0.4", "electron": "^4.0.4",
@ -21,10 +21,10 @@
"electron-reload": "^1.2.5" "electron-reload": "^1.2.5"
}, },
"lbrySettings": { "lbrySettings": {
"lbrynetDaemonVersion": "0.30.4", "lbrynetSDKVersion": "0.30.4",
"lbrynetDaemonUrlTemplate": "https://github.com/lbryio/lbry/releases/download/vDAEMONVER/lbrynet-OSNAME.zip", "lbrynetSDKUrlTemplate": "https://github.com/lbryio/lbry/releases/download/vSDKVER/lbrynet-OSNAME.zip",
"lbrynetDaemonDir": "dist/daemon", "lbrynetSDKDir": "dist/sdk",
"lbrynetDaemonFileName": "lbrynet" "lbrynetSDKFileName": "lbrynet"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View file

@ -12,7 +12,7 @@
<body> <body>
<h1>Hello world</h1> <h1>Hello world</h1>
<div class="content"> <div class="content">
<div id="loading">Starting daemon...</div> <div id="loading">Starting SDK...</div>
<div id="resolve"> <div id="resolve">
<input id="resolve-input" /> <input id="resolve-input" />
<button id="resolve-button">Resolve</button> <button id="resolve-button">Resolve</button>

View file

@ -3,7 +3,7 @@ const { app } = require("electron");
const path = require("path"); const path = require("path");
const findProcess = require("find-process"); const findProcess = require("find-process");
const createWindow = require("./create-window"); const createWindow = require("./create-window");
const Daemon = require("./daemon"); const SDK = require("./sdk");
const IS_DEV = process.env.NODE_ENV === "development"; const IS_DEV = process.env.NODE_ENV === "development";
// Auto-reload when we make changes // 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 // This allows you to run the sdk binary separately and have your app connect to it
const processListArgs = process.platform === "win32" ? "lbrynet" : "lbrynet start"; const processListArgs = process.platform === "win32" ? "lbrynet" : "lbrynet start";
findProcess("name", processListArgs).then(processList => { findProcess("name", processListArgs).then(processList => {
const isDaemonRunning = processList.length > 0; const isSDKRunning = processList.length > 0;
if (!isDaemonRunning) { if (!isSDKRunning) {
daemon = new Daemon(); sdk = new SDK();
daemon.on("exit", () => { sdk.on("exit", () => {
daemon = null; sdk = null;
dialog.showErrorBox("Daemon has Exited"); dialog.showErrorBox("SDK has quit");
app.quit(); app.quit();
}); });
daemon.launch(); sdk.launch();
} }
}); });
}); });

View file

@ -1,20 +1,19 @@
const path = require("path"); const path = require("path");
const { spawn, execSync } = require("child_process"); const { spawn, execSync } = require("child_process");
class Daemon { class SDK {
constructor() { 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.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(`SDK: ${data}`));
this.subprocess.stderr.on("data", data => console.error(`Daemon: ${data}`)); this.subprocess.stderr.on("data", data => console.error(`SDK: ${data}`));
this.subprocess.on("exit", () => this.fire("exit")); 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() { quit() {
@ -44,4 +43,4 @@ class Daemon {
} }
} }
module.exports = Daemon; module.exports = SDK;

View file

@ -1,12 +1,12 @@
const { Lbry } = require("lbry-redux"); 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 // Wait until the sdk is fully started before doing anything else
function checkDaemonStarted() { function checkSDKStarted() {
Lbry.status().then(status => { Lbry.status().then(status => {
if (status.is_running) { if (status.is_running) {
// Daemon is now running // SDK 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";
@ -15,8 +15,8 @@ function checkDaemonStarted() {
} }
setTimeout(() => { setTimeout(() => {
checkDaemonStarted(); checkSDKStarted();
}, 250); }, 500);
}); });
} }