From 6eda55de0c1dfed3eb7d9e727b8c1891af52d4b0 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Tue, 5 Jun 2018 20:12:13 +0200 Subject: [PATCH] fix(resources): fix up resource paths Move rpc.proto into the resources directory along with all of the other external resources. Fix up the resource paths to reference external resources correctly depending on wether the app is being run locally or from a release. --- app/lnd/config/index.js | 25 +++++++++++++++++++++---- app/lnd/lib/lightning.js | 3 +-- app/lnd/lib/walletUnlocker.js | 3 +-- {app/lnd/config => resources}/rpc.proto | 0 4 files changed, 23 insertions(+), 8 deletions(-) rename {app/lnd/config => resources}/rpc.proto (100%) diff --git a/app/lnd/config/index.js b/app/lnd/config/index.js index 80ac4a37..ba33ff9a 100644 --- a/app/lnd/config/index.js +++ b/app/lnd/config/index.js @@ -3,10 +3,27 @@ // Linux: ~/.lnd/tls.cert // Windows: TODO find out where cert is located for windows machine import { userInfo, platform } from 'os' -import { join } from 'path' +import { join, normalize } from 'path' import Store from 'electron-store' +import { app } from 'electron' +// Get a path to prepend to any nodejs calls that are getting at files in the package, +// so that it works both from source and in an asar-packaged mac app. +// See https://github.com/electron-userland/electron-builder/issues/751 +// +// windows from source: "C:\myapp\node_modules\electron\dist\resources\default_app.asar" +// mac from source: "/Users/me/dev/myapp/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar" +// mac from a package: "/my.app/Contents/Resources/app.asar" +// +// If we are run from outside of a packaged app, our working directory is the right place to be. +// And no, we can't just set our working directory to somewhere inside the asar. The OS can't handle that. +const appPath = app.getAppPath() +const appRootPath = appPath.indexOf('default_app.asar') < 0 ? normalize(`${appPath}/..`) : '' + +// Get an electromn store named 'connection' in which the saved connection detailes are stored. const store = new Store({ name: 'connection' }) + +// Get the name of the current platform which we can use to determine the location of various lnd resources. const plat = platform() let loc @@ -35,15 +52,15 @@ switch (plat) { } if (process.env.NODE_ENV === 'development') { - lndPath = join(__dirname, '..', '..', '..', 'resources', 'bin', plat, lndBin) + lndPath = join(appRootPath, 'resources', 'bin', plat, lndBin) } else { - lndPath = join(__dirname, '..', '..', '..', 'bin', lndBin) + lndPath = join(appRootPath, 'bin', lndBin) } export default { lnd: () => ({ lndPath, - lightningRpc: join(__dirname, 'rpc.proto'), + lightningRpc: join(appRootPath, 'resources', 'rpc.proto'), lightningHost: store.get('host') || 'localhost:10009', cert: store.get('cert') || join(userInfo().homedir, loc), macaroon: store.get('macaroon') || join(userInfo().homedir, macaroonPath) diff --git a/app/lnd/lib/lightning.js b/app/lnd/lib/lightning.js index 58f9f090..2ce1e638 100644 --- a/app/lnd/lib/lightning.js +++ b/app/lnd/lib/lightning.js @@ -1,5 +1,4 @@ import fs from 'fs' -import path from 'path' import grpc from 'grpc' import config from '../config' @@ -22,7 +21,7 @@ const lightning = (rpcpath, host) => { const lndConfig = config.lnd() const lndCert = fs.readFileSync(lndConfig.cert) const sslCreds = grpc.credentials.createSsl(lndCert) - const rpc = grpc.load(path.join(__dirname, '..', 'rpc.proto')) + const rpc = grpc.load(lndConfig.lightningRpc) const metadata = new grpc.Metadata() const macaroonHex = fs.readFileSync(lndConfig.macaroon).toString('hex') diff --git a/app/lnd/lib/walletUnlocker.js b/app/lnd/lib/walletUnlocker.js index 3291feec..9d1400c5 100644 --- a/app/lnd/lib/walletUnlocker.js +++ b/app/lnd/lib/walletUnlocker.js @@ -1,5 +1,4 @@ import fs from 'fs' -import path from 'path' import grpc from 'grpc' import config from '../config' @@ -22,7 +21,7 @@ const walletUnlocker = (rpcpath, host) => { const lndConfig = config.lnd() const lndCert = fs.readFileSync(lndConfig.cert) const credentials = grpc.credentials.createSsl(lndCert) - const rpc = grpc.load(path.join(__dirname, '..', 'rpc.proto')) + const rpc = grpc.load(lndConfig.lightningRpc) return new rpc.lnrpc.WalletUnlocker(host, credentials) } diff --git a/app/lnd/config/rpc.proto b/resources/rpc.proto similarity index 100% rename from app/lnd/config/rpc.proto rename to resources/rpc.proto