From 27acbac2354bfedd63cde526b1b4a3c365108315 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 26 Oct 2020 17:49:33 +0000 Subject: [PATCH] Found an issue where environment variables would load before they had actually been updated from dotenv, made sure they are always loaded correctly. --- packages/server/src/api/index.js | 6 ++---- packages/server/src/environment.js | 14 ++++++++++++++ packages/server/src/index.js | 20 ++++++++++---------- packages/server/src/utilities/index.js | 8 ++++++++ 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/packages/server/src/api/index.js b/packages/server/src/api/index.js index 0a4de71135..45eb635b10 100644 --- a/packages/server/src/api/index.js +++ b/packages/server/src/api/index.js @@ -3,6 +3,7 @@ const authenticated = require("../middleware/authenticated") const compress = require("koa-compress") const zlib = require("zlib") const { budibaseAppsDir } = require("../utilities/budibaseDir") +const { isDev } = require("../utilities") const { authRoutes, pageRoutes, @@ -45,10 +46,7 @@ router jwtSecret: env.JWT_SECRET, useAppRootPath: true, } - ctx.isDev = - process.env.NODE_ENV !== "production" && - process.env.NODE_ENV !== "jest" && - process.env.NODE_ENV !== "cypress" + ctx.isDev = isDev() await next() }) .use("/health", ctx => (ctx.status = 200)) diff --git a/packages/server/src/environment.js b/packages/server/src/environment.js index 3fb78a55f7..6e8c29cbb1 100644 --- a/packages/server/src/environment.js +++ b/packages/server/src/environment.js @@ -1,3 +1,17 @@ +const { resolve, join } = require("./utilities/centralPath") +const { homedir } = require("os") +const { app } = require("electron") + +let LOADED = false + +if (!LOADED) { + const homeDir = app ? app.getPath("home") : homedir() + const budibaseDir = join(homeDir, ".budibase") + process.env.BUDIBASE_DIR = budibaseDir + require("dotenv").config({ path: resolve(budibaseDir, ".env") }) + LOADED = true +} + module.exports = { CLIENT_ID: process.env.CLIENT_ID, NODE_ENV: process.env.NODE_ENV, diff --git a/packages/server/src/index.js b/packages/server/src/index.js index afe2b09e3b..2e60cab17b 100644 --- a/packages/server/src/index.js +++ b/packages/server/src/index.js @@ -1,18 +1,18 @@ -const { resolve, join } = require("./utilities/centralPath") -const { homedir } = require("os") -const { app } = require("electron") +const { budibaseTempDir } = require("./utilities/budibaseDir") +const { isDev } = require("./utilities") + const fixPath = require("fix-path") +const fs = require("fs") async function runServer() { - const homeDir = app ? app.getPath("home") : homedir() - - const budibaseDir = join(homeDir, ".budibase") - process.env.BUDIBASE_DIR = budibaseDir + if (isDev() && !fs.existsSync(budibaseTempDir())) { + console.error( + "Please run a build before attempting to run server indepdently to fill 'tmp' directory." + ) + process.exit(-1) + } fixPath() - - require("dotenv").config({ path: resolve(budibaseDir, ".env") }) - require("./app") } diff --git a/packages/server/src/utilities/index.js b/packages/server/src/utilities/index.js index 983c7803ba..e17a01b173 100644 --- a/packages/server/src/utilities/index.js +++ b/packages/server/src/utilities/index.js @@ -1 +1,9 @@ exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms)) + +exports.isDev = () => { + return ( + process.env.NODE_ENV !== "production" && + process.env.NODE_ENV !== "jest" && + process.env.NODE_ENV !== "cypress" + ) +}