From 00344b3b39de59b77548572c085526a69f364350 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 23 Mar 2021 15:37:11 +0000 Subject: [PATCH] Some changes to how environment is loaded to make the init process by default flesh out some good settings. --- hosting/docker-compose.yaml | 6 +++- packages/server/.env.template | 17 --------- packages/server/dev.env | 1 - packages/server/package.json | 3 +- packages/server/scripts/dev/manage.js | 27 +++++++++++++++ packages/server/src/electron.js | 6 ---- packages/server/src/environment.js | 48 +++++++++++++++----------- packages/server/src/utilities/index.js | 9 +---- 8 files changed, 62 insertions(+), 55 deletions(-) delete mode 100644 packages/server/.env.template delete mode 100644 packages/server/dev.env diff --git a/hosting/docker-compose.yaml b/hosting/docker-compose.yaml index e8408d9a7d..ebfc07ef02 100644 --- a/hosting/docker-compose.yaml +++ b/hosting/docker-compose.yaml @@ -14,10 +14,14 @@ services: CLOUD: 1 COUCH_DB_URL: http://${COUCH_DB_USER}:${COUCH_DB_PASSWORD}@couchdb-service:5984 WORKER_URL: http://worker-service:4003 + MINIO_URL: http://minio-service:9000 HOSTING_KEY: ${HOSTING_KEY} BUDIBASE_ENVIRONMENT: ${BUDIBASE_ENVIRONMENT} PORT: 4002 JWT_SECRET: ${JWT_SECRET} + LOG_LEVEL: info + SENTRY_DSN: https://a34ae347621946bf8acded18e5b7d4b8@o420233.ingest.sentry.io/5338131 + ENABLE_ANALYTICS: true depends_on: - worker-service @@ -28,7 +32,7 @@ services: ports: - "${WORKER_PORT}:4003" environment: - SELF_HOSTED: 1, + SELF_HOSTED: 1 PORT: 4003 MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY} MINIO_SECRET_KEY: ${MINIO_SECRET_KEY} diff --git a/packages/server/.env.template b/packages/server/.env.template deleted file mode 100644 index b2ff5be3f4..0000000000 --- a/packages/server/.env.template +++ /dev/null @@ -1,17 +0,0 @@ -# url of couch db, including username and password -# http://admin:password@localhost:5984 -COUCH_DB_URL={{couchDbUrl}} - -# identifies a client database - i.e. group of apps -CLIENT_ID={{clientId}} - -# used to create cookie hashes -JWT_SECRET={{cookieKey1}} - -# error level for koa-pino -LOG_LEVEL=info - -DEPLOYMENT_CREDENTIALS_URL="https://dt4mpwwap8.execute-api.eu-west-1.amazonaws.com/prod/" -DEPLOYMENT_DB_URL="https://couchdb.budi.live:5984" -SENTRY_DSN=https://a34ae347621946bf8acded18e5b7d4b8@o420233.ingest.sentry.io/5338131 -ENABLE_ANALYTICS="true" diff --git a/packages/server/dev.env b/packages/server/dev.env deleted file mode 100644 index 45e34f8be4..0000000000 --- a/packages/server/dev.env +++ /dev/null @@ -1 +0,0 @@ -PORT=4001 diff --git a/packages/server/package.json b/packages/server/package.json index 84b46e051f..2d6ecec7c1 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -39,7 +39,7 @@ "dev:stack:up": "node scripts/dev/manage.js up", "dev:stack:down": "node scripts/dev/manage.js down", "dev:stack:nuke": "node scripts/dev/manage.js nuke", - "dev:builder": "npm run dev:stack:up && env-cmd -f dev.env nodemon src/index.js", + "dev:builder": "npm run dev:stack:up && nodemon src/index.js", "electron": "electron src/electron.js", "build:electron": "electron-builder --dir", "publish:electron": "electron-builder -mwl --publish always", @@ -141,7 +141,6 @@ "electron": "10.1.3", "electron-builder": "^22.9.1", "electron-builder-notarize": "^1.1.2", - "env-cmd": "^10.1.0", "eslint": "^6.8.0", "jest": "^24.8.0", "nodemon": "^2.0.4", diff --git a/packages/server/scripts/dev/manage.js b/packages/server/scripts/dev/manage.js index f76b5ebd3d..28b2e0d6a2 100644 --- a/packages/server/scripts/dev/manage.js +++ b/packages/server/scripts/dev/manage.js @@ -1,6 +1,7 @@ #!/usr/bin/env node const compose = require("docker-compose") const path = require("path") +const fs = require("fs") // This script wraps docker-compose allowing you to manage your dev infrastructure with simple commands. const CONFIG = { @@ -26,8 +27,34 @@ if ( ) } +async function init() { + const envFilePath = path.join(process.cwd(), ".env") + if (fs.existsSync(envFilePath)) { + return + } + const envFileJson = { + PORT: 4001, + MINIO_URL: "http://localhost:10000/", + COUCH_DB_URL: "http://budibase:budibase@localhost:10000/db/", + WORKER_URL: "http://localhost:4002", + JWT_SECRET: "testsecret", + MINIO_ACCESS_KEY: "budibase", + MINIO_SECRET_KEY: "budibase", + COUCH_DB_PASSWORD: "budibase", + COUCH_DB_USER: "budibase", + SELF_HOSTED: 1, + CLOUD: 1, + } + let envFile = "" + Object.keys(envFileJson).forEach(key => { + envFile += `${key}=${envFileJson[key]}\n` + }) + fs.writeFileSync(envFilePath, envFile) +} + async function up() { console.log("Spinning up your budibase dev environment... 🔧✨") + await init() try { await compose.upAll(CONFIG) } catch (err) { diff --git a/packages/server/src/electron.js b/packages/server/src/electron.js index 290c13e6a0..92b8984121 100644 --- a/packages/server/src/electron.js +++ b/packages/server/src/electron.js @@ -18,12 +18,6 @@ async function startApp() { } // evict environment from cache, so it reloads when next asked delete require.cache[require.resolve("./environment")] - // store the port incase its going to get overridden - const port = process.env.PORT - require("dotenv").config({ path: envFile }) - // overwrite the port - don't want to use dotenv for the port - require("./environment")._set("PORT", port) - unhandled({ showDialog: true, reportButton: error => { diff --git a/packages/server/src/environment.js b/packages/server/src/environment.js index 19c750486e..ee63d884b6 100644 --- a/packages/server/src/environment.js +++ b/packages/server/src/environment.js @@ -1,38 +1,45 @@ -const { resolve, join } = require("./utilities/centralPath") -const { homedir } = require("os") -const { app } = require("electron") +function isDev() { + return ( + !process.env.CLOUD && + process.env.NODE_ENV !== "production" && + process.env.NODE_ENV !== "jest" && + process.env.NODE_ENV !== "cypress" && + process.env.JEST_WORKER_ID == null + ) +} 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") }) +if (!LOADED && isDev()) { + require("dotenv").config() LOADED = true } module.exports = { - CLIENT_ID: process.env.CLIENT_ID, - NODE_ENV: process.env.NODE_ENV, - JWT_SECRET: process.env.JWT_SECRET, - BUDIBASE_DIR: process.env.BUDIBASE_DIR, + // important PORT: process.env.PORT, + JWT_SECRET: process.env.JWT_SECRET, COUCH_DB_URL: process.env.COUCH_DB_URL, + MINIO_URL: process.env.MINIO_URL, + WORKER_URL: process.env.WORKER_URL, + CLOUD: process.env.CLOUD, + SELF_HOSTED: process.env.SELF_HOSTED, + AWS_REGION: process.env.AWS_REGION, + ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS, + // environment + NODE_ENV: process.env.NODE_ENV, + JEST_WORKER_ID: process.env.JEST_WORKER_ID, + BUDIBASE_ENVIRONMENT: process.env.BUDIBASE_ENVIRONMENT, + // minor SALT_ROUNDS: process.env.SALT_ROUNDS, LOGGER: process.env.LOGGER, LOG_LEVEL: process.env.LOG_LEVEL, AUTOMATION_DIRECTORY: process.env.AUTOMATION_DIRECTORY, AUTOMATION_BUCKET: process.env.AUTOMATION_BUCKET, - BUDIBASE_ENVIRONMENT: process.env.BUDIBASE_ENVIRONMENT, SENDGRID_API_KEY: process.env.SENDGRID_API_KEY, - CLOUD: process.env.CLOUD, - SELF_HOSTED: process.env.SELF_HOSTED, - WORKER_URL: process.env.WORKER_URL, DYNAMO_ENDPOINT: process.env.DYNAMO_ENDPOINT, - AWS_REGION: process.env.AWS_REGION, - ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS, - // TODO: remove all below - single stack conversion + // old - to remove + CLIENT_ID: process.env.CLIENT_ID, + BUDIBASE_DIR: process.env.BUDIBASE_DIR, DEPLOYMENT_DB_URL: process.env.DEPLOYMENT_DB_URL, BUDIBASE_API_KEY: process.env.BUDIBASE_API_KEY, USERID_API_KEY: process.env.USERID_API_KEY, @@ -42,4 +49,5 @@ module.exports = { process.env[key] = value module.exports[key] = value }, + isDev, } diff --git a/packages/server/src/utilities/index.js b/packages/server/src/utilities/index.js index 7420884d44..226955101f 100644 --- a/packages/server/src/utilities/index.js +++ b/packages/server/src/utilities/index.js @@ -13,14 +13,7 @@ function confirmAppId(possibleAppId) { exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms)) -exports.isDev = () => { - return ( - !env.CLOUD && - env.NODE_ENV !== "production" && - env.NODE_ENV !== "jest" && - env.NODE_ENV !== "cypress" - ) -} +exports.isDev = env.isDev /** * Given a request tries to find the appId, which can be located in various places