From 42d19efebad74395c38d53296dd86a7d3e8d69db Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Thu, 14 May 2020 16:56:41 +0100 Subject: [PATCH] bugfix: early require of environment.js --- packages/cli/src/commands/init/initHandler.js | 10 ++++--- packages/cli/src/commands/new/newHandler.js | 26 +++++++++++-------- packages/cli/src/commands/run/runHandler.js | 9 +++++-- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/cli/src/commands/init/initHandler.js b/packages/cli/src/commands/init/initHandler.js index ebc2ea912f..27b68e48c5 100644 --- a/packages/cli/src/commands/init/initHandler.js +++ b/packages/cli/src/commands/init/initHandler.js @@ -3,10 +3,8 @@ const { exists, readFile, writeFile, ensureDir } = require("fs-extra") const chalk = require("chalk") const { serverFileName, xPlatHomeDir } = require("../../common") const { join } = require("path") -const { create } = require("@budibase/server/src/db/clientDb") const Sqrl = require("squirrelly") const uuid = require("uuid") -const CouchDB = require("@budibase/server/src/db/client") module.exports = opts => { run(opts) @@ -57,6 +55,9 @@ const prompts = async opts => { const createClientDatabase = async opts => { if (opts.clientId === "new") { + // cannot be a top level require as it + // will cause environment module to be loaded prematurely + const CouchDB = require("@budibase/server/src/db/client") const existing = await CouchDB.allDbs() let i = 0 @@ -68,7 +69,10 @@ const createClientDatabase = async opts => { } } - await create(opts.clientId) + // cannot be a top level require as it + // will cause environment module to be loaded prematurely + const clientDb = require("@budibase/server/src/db/clientDb") + await clientDb.create(opts.clientId) } const createDevEnvFile = async opts => { diff --git a/packages/cli/src/commands/new/newHandler.js b/packages/cli/src/commands/new/newHandler.js index dec20bedd4..b533266d0e 100644 --- a/packages/cli/src/commands/new/newHandler.js +++ b/packages/cli/src/commands/new/newHandler.js @@ -1,11 +1,6 @@ const { xPlatHomeDir } = require("../../common") const dotenv = require("dotenv") -const createInstance = require("@budibase/server/src/api/controllers/instance") - .create -const createApplication = require("@budibase/server/src/api/controllers/application") - .create -const buildPage = require("@budibase/server/src/utilities/builder/buildPage") -const { copy, readJSON, writeJSON, remove, exists } = require("fs-extra") +const { copy, readJSON, writeJSON, exists } = require("fs-extra") const { resolve, join } = require("path") const chalk = require("chalk") const { exec } = require("child_process") @@ -37,10 +32,18 @@ const createAppInstance = async opts => { body: {}, } - await createApplication(createAppCtx) - opts.applicationId = createAppCtx.body.id - console.log(chalk.green(`Application Created: ${createAppCtx.body.id}`)) - await createInstance({ + // this cannot be a top level require as it will cause + // the environment module to be loaded prematurely + const appController = require("@budibase/server/src/api/controllers/application") + await appController.create(createAppCtx) + + opts.applicationId = createAppCtx.body._id + console.log(chalk.green(`Application Created: ${createAppCtx.body._id}`)) + + // this cannot be a top level require as it will cause + // the environment module to be loaded prematurely + const instanceController = require("@budibase/server/src/api/controllers/instance") + await instanceController.create({ params: { clientId: process.env.CLIENT_ID, applicationId: opts.applicationId, @@ -49,6 +52,7 @@ const createAppInstance = async opts => { body: { name: `dev-${process.env.CLIENT_ID}` }, }, }) + console.log(chalk.green(`Default Instance Created`)) } @@ -71,4 +75,4 @@ const createEmptyAppPackage = async opts => { packageJson.name = opts.name await writeJSON(packageJsonPath, packageJson) -} \ No newline at end of file +} diff --git a/packages/cli/src/commands/run/runHandler.js b/packages/cli/src/commands/run/runHandler.js index fc6ee25dd8..04b21a4ff0 100644 --- a/packages/cli/src/commands/run/runHandler.js +++ b/packages/cli/src/commands/run/runHandler.js @@ -5,7 +5,12 @@ module.exports = ({ dir }) => { dir = xPlatHomeDir(dir) process.env.BUDIBASE_DIR = resolve(dir) require("dotenv").config({ path: resolve(dir, ".env") }) - require("@budibase/server/src/app")().then(() => { - console.log(`Budibase Builder running on port ${process.env.PORT}..`) + console.log("dotenv loaded") + + // dont make this a variable or top level require + // ti will cause environment module to be loaded prematurely + require("@budibase/server/src/app")().then(server => { + server.on("close", () => console.log("Server Closed")) + console.log(`Budibase running on ${JSON.stringify(server.address())}`) }) }