From 7d6046d02174741723d7a002bd57acea95f59c60 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 11 May 2020 15:42:42 +0100 Subject: [PATCH 1/2] allow bb home folder to bem anywhere --- packages/cli/src/commands/run/runHandler.js | 1 + packages/server/src/api/controllers/component.js | 10 ++++++---- packages/server/src/api/controllers/static.js | 14 +++++++------- packages/server/src/api/index.js | 5 ++--- packages/server/src/api/routes/static.js | 3 ++- packages/server/src/db/client.js | 7 ++----- packages/server/src/index.js | 10 ---------- packages/server/src/utilities/budibaseDir.js | 10 ++++++++++ scripts/symlinkDev.js | 6 ++++-- 9 files changed, 34 insertions(+), 32 deletions(-) delete mode 100644 packages/server/src/index.js create mode 100644 packages/server/src/utilities/budibaseDir.js diff --git a/packages/cli/src/commands/run/runHandler.js b/packages/cli/src/commands/run/runHandler.js index 83dca843f0..cdb75b47c8 100644 --- a/packages/cli/src/commands/run/runHandler.js +++ b/packages/cli/src/commands/run/runHandler.js @@ -3,6 +3,7 @@ const { resolve } = require("path") module.exports = ({ dir }) => { dir = xPlatHomeDir(dir) + process.env.BUDIBASE_DIR = resolve(dir) require("dotenv").config({ path: resolve(dir, ".env") }) require("@budibase/server/src/app") console.log(`Budibase Builder running on port ${process.env.PORT}..`) diff --git a/packages/server/src/api/controllers/component.js b/packages/server/src/api/controllers/component.js index 5ae66e32c6..da8d0e5140 100644 --- a/packages/server/src/api/controllers/component.js +++ b/packages/server/src/api/controllers/component.js @@ -1,6 +1,9 @@ const CouchDB = require("../../db") -const { homedir } = require("os") const { resolve, join } = require("path") +const { + budibaseTempDir, + budibaseAppsDir, +} = require("../../utilities/budibaseDir") const isDev = process.env.NODE_ENV !== "production" @@ -11,14 +14,13 @@ exports.fetchAppComponentDefinitions = async function(ctx) { const componentDefinitions = app.componentLibraries.reduce( (acc, componentLibrary) => { let appDirectory = resolve( - homedir(), - ".budibase", + budibaseAppsDir(), ctx.params.appId, "node_modules" ) if (isDev) { - appDirectory = "/tmp/.budibase" + appDirectory = budibaseTempDir() } const componentJson = require(join( diff --git a/packages/server/src/api/controllers/static.js b/packages/server/src/api/controllers/static.js index 980aca907d..3badf1940a 100644 --- a/packages/server/src/api/controllers/static.js +++ b/packages/server/src/api/controllers/static.js @@ -1,6 +1,9 @@ const send = require("koa-send") const { resolve, join } = require("path") -const { homedir } = require("os") +const { + budibaseAppsDir, + budibaseTempDir, +} = require("../../utilities/budibaseDir") exports.serveBuilder = async function(ctx) { let builderPath = resolve(process.cwd(), "builder") @@ -12,8 +15,7 @@ exports.serveApp = async function(ctx) { // TODO: update homedir stuff to wherever budi is run // default to homedir const appPath = resolve( - homedir(), - ".budibase", + budibaseAppsDir(), ctx.params.appId, "public", ctx.isAuthenticated ? "main" : "unauthenticated" @@ -26,8 +28,7 @@ exports.serveComponentLibrary = async function(ctx) { // TODO: update homedir stuff to wherever budi is run // default to homedir let componentLibraryPath = resolve( - homedir(), - ".budibase", + budibaseAppsDir(), ctx.params.appId, "node_modules", decodeURI(ctx.query.library), @@ -36,8 +37,7 @@ exports.serveComponentLibrary = async function(ctx) { if (ctx.isDev) { componentLibraryPath = join( - "/tmp", - ".budibase", + budibaseTempDir(), decodeURI(ctx.query.library), "dist" ) diff --git a/packages/server/src/api/index.js b/packages/server/src/api/index.js index 19c44b3955..65fb60ee71 100644 --- a/packages/server/src/api/index.js +++ b/packages/server/src/api/index.js @@ -2,8 +2,7 @@ const Router = require("@koa/router") const authenticated = require("../middleware/authenticated") const compress = require("koa-compress") const zlib = require("zlib") -const { resolve } = require("path") -const { homedir } = require("os") +const { budibaseAppsDir } = require("../utilities/budibaseDir") const { authRoutes, pageRoutes, @@ -34,7 +33,7 @@ router ) .use(async (ctx, next) => { ctx.config = { - latestPackagesFolder: resolve(homedir(), ".budibase"), + latestPackagesFolder: budibaseAppsDir(), jwtSecret: process.env.JWT_SECRET, } ctx.isDev = process.env.NODE_ENV !== "production" diff --git a/packages/server/src/api/routes/static.js b/packages/server/src/api/routes/static.js index b1dfaf8f12..606173b8fe 100644 --- a/packages/server/src/api/routes/static.js +++ b/packages/server/src/api/routes/static.js @@ -1,5 +1,6 @@ const Router = require("@koa/router") const controller = require("../controllers/static") +const { budibaseTempDir } = require("../../utilities/budibaseDir") const router = Router() @@ -9,7 +10,7 @@ router // Serving the client library from your local dir in dev if (ctx.isDev && ctx.file.startsWith("budibase-client")) { - ctx.devPath = "/tmp/.budibase" + ctx.devPath = budibaseTempDir() } await next() diff --git a/packages/server/src/db/client.js b/packages/server/src/db/client.js index 8ee19f8de6..fcb7cadec5 100644 --- a/packages/server/src/db/client.js +++ b/packages/server/src/db/client.js @@ -1,11 +1,8 @@ const PouchDB = require("pouchdb") const allDbs = require("pouchdb-all-dbs") -const os = require("os") -const path = require("path") +const { budibaseAppsDir } = require("../utilities/budibaseDir") -const BUDIBASE_DIR = path.join(os.homedir(), ".budibase") - -const COUCH_DB_URL = process.env.COUCH_DB_URL || `leveldb://${BUDIBASE_DIR}/` +const COUCH_DB_URL = process.env.COUCH_DB_URL || `leveldb://${budibaseAppsDir()}/` const Pouch = PouchDB.defaults({ prefix: COUCH_DB_URL, diff --git a/packages/server/src/index.js b/packages/server/src/index.js deleted file mode 100644 index 2b44909a97..0000000000 --- a/packages/server/src/index.js +++ /dev/null @@ -1,10 +0,0 @@ -const { join } = require("path") -const { homedir } = require("os") - -const path = join(homedir(), ".budibase", ".env") -require("dotenv").config({ path }) -const server = require("./app") - -server.on("listening", () => { - console.log(`Budibase Server listening on port ${process.env.PORT}`) -}) diff --git a/packages/server/src/utilities/budibaseDir.js b/packages/server/src/utilities/budibaseDir.js new file mode 100644 index 0000000000..fd531d4199 --- /dev/null +++ b/packages/server/src/utilities/budibaseDir.js @@ -0,0 +1,10 @@ +const { join } = require("path") +const { homedir, tmpdir } = require("os") + +module.exports.budibaseAppsDir = function() { + return process.env.BUDIBASE_DIR || join(homedir(), ".budibase") +} + +module.exports.budibaseTempDir = function() { + return join(tmpdir(), ".budibase") +} diff --git a/scripts/symlinkDev.js b/scripts/symlinkDev.js index 9792a4529e..fdcf0c2f3e 100755 --- a/scripts/symlinkDev.js +++ b/scripts/symlinkDev.js @@ -10,8 +10,10 @@ const fs = require("fs") const { resolve } = require("path") const rimraf = require("rimraf"); - -const devDir = "/tmp/.budibase" +const { + budibaseTempDir, +} = require("../packages/server/src/utilities/budibaseDir") +const devDir = budibaseTempDir() rimraf.sync(devDir); fs.mkdirSync(`${devDir}/@budibase`, { recursive: true }) From 1a1d54517873e1d829e88c9dcf92758e89865b5b Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 11 May 2020 16:14:31 +0100 Subject: [PATCH 2/2] bugfix - could not create more than one app --- packages/cli/src/commands/new/newHandler.js | 6 +++--- packages/server/src/api/controllers/instance.js | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/commands/new/newHandler.js b/packages/cli/src/commands/new/newHandler.js index 36297ed664..f79a46f9fb 100644 --- a/packages/cli/src/commands/new/newHandler.js +++ b/packages/cli/src/commands/new/newHandler.js @@ -11,19 +11,17 @@ const { exec } = require("child_process") module.exports = opts => { run(opts) - console.log(chalk.green(`Budibase app ${opts.name} created!`)) } const run = async opts => { - console.log(opts) try { opts.dir = xPlatHomeDir(opts.dir) - console.log(resolve(opts.dir)) const bbconfig = dotenv.config({ path: resolve(opts.dir, ".env") }) console.log(bbconfig) await createAppInstance(opts) await createEmptyAppPackage(opts) exec(`cd ${join(opts.dir, opts.applicationId)} && npm install`) + console.log(chalk.green(`Budibase app ${opts.name} created!`)) } catch (error) { console.error(chalk.red("Error creating new app", error)) } @@ -40,6 +38,7 @@ const createAppInstance = async opts => { await createApplication(createAppCtx) opts.applicationId = createAppCtx.body.id + console.log(chalk.green(`Application Created: ${createAppCtx.body.id}`)) await createInstance({ params: { clientId: process.env.CLIENT_ID, @@ -49,6 +48,7 @@ const createAppInstance = async opts => { body: { name: `dev-${process.env.CLIENT_ID}` }, }, }) + console.log(chalk.green(`Default Instance Created`)) } const createEmptyAppPackage = async opts => { diff --git a/packages/server/src/api/controllers/instance.js b/packages/server/src/api/controllers/instance.js index d359ee81d7..7e0e0eef1b 100644 --- a/packages/server/src/api/controllers/instance.js +++ b/packages/server/src/api/controllers/instance.js @@ -1,10 +1,11 @@ const CouchDB = require("../../db") +const uuid = require("uuid") exports.create = async function(ctx) { const instanceName = ctx.request.body.name - + const instanceId = `app_${ctx.params.applicationId.substring(6)}_inst_${uuid.v4()}` const { clientId, applicationId } = ctx.params - const db = new CouchDB(instanceName) + const db = new CouchDB(instanceId) await db.put({ _id: "_design/database", metadata: { @@ -31,7 +32,7 @@ exports.create = async function(ctx) { const clientDatabaseId = `client-${clientId}` const clientDb = new CouchDB(clientDatabaseId) const budibaseApp = await clientDb.get(applicationId) - const instance = { id: instanceName, name: instanceName } + const instance = { id: instanceId, name: instanceName } budibaseApp.instances.push(instance) await clientDb.put(budibaseApp)