diff --git a/packages/cli/package.json b/packages/cli/package.json index 4c530d4c55..c3a47bb59e 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -24,6 +24,7 @@ "@budibase/server": "^0.0.32", "@inquirer/password": "^0.0.6-alpha.0", "chalk": "^2.4.2", + "dotenv": "^8.2.0", "fs-extra": "^8.1.0", "inquirer": "^7.0.0", "lodash": "^4.17.15", diff --git a/packages/cli/src/commands/init/initHandler.js b/packages/cli/src/commands/init/initHandler.js index 7ac01b7f23..f7b900f472 100644 --- a/packages/cli/src/commands/init/initHandler.js +++ b/packages/cli/src/commands/init/initHandler.js @@ -1,12 +1,11 @@ const inquirer = require("inquirer") const { exists, readFile, writeFile, ensureDir } = require("fs-extra") const chalk = require("chalk") -const { serverFileName } = require("../../common") +const { serverFileName, xPlatHomeDir } = require("../../common") const { join } = require("path") const initialiseClientDb = require("@budibase/server/db/initialiseClientDb") const Sqrl = require("squirrelly") const uuid = require("uuid") -const { homedir } = require("os") const CouchDb = require("@budibase/server/db/client") module.exports = opts => { @@ -26,9 +25,7 @@ const run = async opts => { } const ensureAppDir = async opts => { - if (opts.dir.startsWith("~")) { - opts.dir = join(homedir(), opts.dir.substring(1)) - } + opts.dir = xPlatHomeDir(opts.dir) await ensureDir(opts.dir) if (opts.database === "local") { diff --git a/packages/cli/src/commands/new/appPackageTemplate/.gitignore b/packages/cli/src/commands/new/appDirectoryTemplate/.gitignore similarity index 100% rename from packages/cli/src/commands/new/appPackageTemplate/.gitignore rename to packages/cli/src/commands/new/appDirectoryTemplate/.gitignore diff --git a/packages/cli/src/commands/new/appPackageTemplate/package.json b/packages/cli/src/commands/new/appDirectoryTemplate/package.json similarity index 100% rename from packages/cli/src/commands/new/appPackageTemplate/package.json rename to packages/cli/src/commands/new/appDirectoryTemplate/package.json diff --git a/packages/cli/src/commands/new/appPackageTemplate/plugins.js b/packages/cli/src/commands/new/appDirectoryTemplate/plugins.js similarity index 100% rename from packages/cli/src/commands/new/appPackageTemplate/plugins.js rename to packages/cli/src/commands/new/appDirectoryTemplate/plugins.js diff --git a/packages/cli/src/commands/new/appPackageTemplate/pages/main/screens/placeholder b/packages/cli/src/commands/new/appDirectoryTemplate/public/_shared/placeholder similarity index 100% rename from packages/cli/src/commands/new/appPackageTemplate/pages/main/screens/placeholder rename to packages/cli/src/commands/new/appDirectoryTemplate/public/_shared/placeholder diff --git a/packages/cli/src/commands/new/appPackageTemplate/pages/unauthenticated/screens/placeholder b/packages/cli/src/commands/new/appDirectoryTemplate/public/main/placeholder similarity index 100% rename from packages/cli/src/commands/new/appPackageTemplate/pages/unauthenticated/screens/placeholder rename to packages/cli/src/commands/new/appDirectoryTemplate/public/main/placeholder diff --git a/packages/cli/src/commands/new/appPackageTemplate/public/_shared/placeholder b/packages/cli/src/commands/new/appDirectoryTemplate/public/unauthenticated/placeholder similarity index 100% rename from packages/cli/src/commands/new/appPackageTemplate/public/_shared/placeholder rename to packages/cli/src/commands/new/appDirectoryTemplate/public/unauthenticated/placeholder diff --git a/packages/cli/src/commands/new/appPackageTemplate/access_levels.json b/packages/cli/src/commands/new/appPackageTemplate/access_levels.json deleted file mode 100644 index cbe1d6ef5e..0000000000 --- a/packages/cli/src/commands/new/appPackageTemplate/access_levels.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "levels": [], - "version": 0 -} diff --git a/packages/cli/src/commands/new/appPackageTemplate/appDefinition.json b/packages/cli/src/commands/new/appPackageTemplate/appDefinition.json deleted file mode 100644 index 13e6dccb88..0000000000 --- a/packages/cli/src/commands/new/appPackageTemplate/appDefinition.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "hierarchy": { - "name": "root", - "type": "root", - "children": [], - "pathMaps":[], - "indexes":[], - "nodeId": 0 - }, - "triggers": [], - "actions": [], - "props": {} -} diff --git a/packages/cli/src/commands/new/appPackageTemplate/pages/unauthenticated/page.json b/packages/cli/src/commands/new/appPackageTemplate/pages/unauthenticated/page.json deleted file mode 100644 index 14d0301c24..0000000000 --- a/packages/cli/src/commands/new/appPackageTemplate/pages/unauthenticated/page.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "title": "Test App", - "favicon": "./_shared/favicon.png", - "stylesheets": [], - "componentLibraries": ["@budibase/standard-components", "@budibase/materialdesign-components"], - "props" : { - "_component": "@budibase/standard-components/container", - "_children": [], - "_id": 1, - "type": "div", - "_styles": { - "layout": {}, - "position": {} - }, - "_code": "" - }, - "_css": "", - "uiFunctions": "" -} diff --git a/packages/cli/src/commands/new/appPackageTemplate/public/main/placeholder b/packages/cli/src/commands/new/appPackageTemplate/public/main/placeholder deleted file mode 100644 index 4a61114f5d..0000000000 --- a/packages/cli/src/commands/new/appPackageTemplate/public/main/placeholder +++ /dev/null @@ -1 +0,0 @@ -whats the craic big lawd ? \ No newline at end of file diff --git a/packages/cli/src/commands/new/appPackageTemplate/public/unauthenticated/placeholder b/packages/cli/src/commands/new/appPackageTemplate/public/unauthenticated/placeholder deleted file mode 100644 index 4a61114f5d..0000000000 --- a/packages/cli/src/commands/new/appPackageTemplate/public/unauthenticated/placeholder +++ /dev/null @@ -1 +0,0 @@ -whats the craic big lawd ? \ No newline at end of file diff --git a/packages/cli/src/commands/new/index.js b/packages/cli/src/commands/new/index.js index 15c991a585..1466cab997 100644 --- a/packages/cli/src/commands/new/index.js +++ b/packages/cli/src/commands/new/index.js @@ -1,7 +1,7 @@ const handler = require("./newHandler") module.exports = { - command: "new [config]", + command: "new [dir]", desc: "Create a new Budibase app", builder: yargs => { yargs.positional("name", { @@ -9,11 +9,11 @@ module.exports = { describe: "the name of your app", alias: "n", }) - yargs.positional("config", { + yargs.positional("dir", { type: "string", - describe: "config file to use - optional, defaults to config.js", - alias: "c", - default: "config.js", + describe: "budibase apps directory", + alias: "d", + default: "~/budibase", }) }, handler, diff --git a/packages/cli/src/commands/new/newHandler.js b/packages/cli/src/commands/new/newHandler.js index a654d1a8ce..5af1779336 100644 --- a/packages/cli/src/commands/new/newHandler.js +++ b/packages/cli/src/commands/new/newHandler.js @@ -1,10 +1,9 @@ -const { getAppContext } = require("../../common") -const { - getMasterApisWithFullAccess, -} = require("@budibase/server/utilities/budibaseApi") +const { xPlatHomeDir } = require("../../common") +const dotenv = require("dotenv") +const createInstance = require("@budibase/server/middleware/controllers/instance").create +const createApplication = require("@budibase/server/middleware/controllers/application").create const { copy, readJSON, writeJSON, remove, exists } = require("fs-extra") const { resolve, join } = require("path") -const thisPackageJson = require("../../../package.json") const chalk = require("chalk") const { exec } = require("child_process") @@ -13,49 +12,54 @@ module.exports = opts => { console.log(chalk.green(`Budibase app ${opts.name} created!`)) } -const run2 = async opts => { - // create a brand new app in couch - // create an empty app package locally - exec(`cd ${join(opts.config.latestPackagesFolder, opts.name)} && npm install`) +const run = async opts => { + opts.dir = xPlatHomeDir(opts.dir) + process.chdir(opts.dir) + dotenv.config() + await createRecords(opts) + await createEmptyAppPackage(opts) + exec(`cd ${join(opts.dir, opts.name)} && npm install`) } -const run = async opts => { - const context = await getAppContext({ - configName: opts.config, - masterIsCreated: true, +const createRecords = async opts => { + const createAppCtx = { + params: { clientId: process.env.CLIENT_ID }, + request: { + body: { name: opts.name }, + }, + body: {}, + } + + await createApplication(createAppCtx) + opts.applicationId = createAppCtx.body.id + await createInstance({ + params: { + clientId: process.env.CLIENT_ID, + applicationId: opts.applicationId, + }, + request: { + body: { name: `dev-${process.env.CLIENT_ID}` }, + }, }) - opts.config = context.config - const bb = await getMasterApisWithFullAccess(context) - - const app = bb.recordApi.getNew("/applications", "application") - app.name = opts.name - - await bb.recordApi.save(app) - await createEmptyAppPackage(opts) - - exec(`cd ${join(opts.config.latestPackagesFolder, opts.name)} && npm install`) } const createEmptyAppPackage = async opts => { - const templateFolder = resolve(__dirname, "appPackageTemplate") + const templateFolder = resolve(__dirname, "appDirectoryTemplate") - const appsFolder = opts.config.latestPackagesFolder || "." - const destinationFolder = resolve(appsFolder, opts.name) + const appsFolder = opts.dir + const destinationFolder = resolve(appsFolder, opts.applicationId) - if (await exists(destinationFolder)) { + if (await exists(destinationFolder)) { console.log(chalk.red(`App ${opts.name} already exists.`)) - return + return } await copy(templateFolder, destinationFolder) - const packageJsonPath = join(appsFolder, opts.name, "package.json") + const packageJsonPath = join(appsFolder, opts.applicationId, "package.json") const packageJson = await readJSON(packageJsonPath) packageJson.name = opts.name - packageJson.dependencies[ - "@budibase/standard-components" - ] = `^${thisPackageJson.version}` await writeJSON(packageJsonPath, packageJson) diff --git a/packages/cli/src/commands/new/appPackageTemplate/pages/main/page.json b/packages/cli/src/commands/new/page.template.json similarity index 100% rename from packages/cli/src/commands/new/appPackageTemplate/pages/main/page.json rename to packages/cli/src/commands/new/page.template.json diff --git a/packages/cli/src/commands/run/index.js b/packages/cli/src/commands/run/index.js index 0eadc58bb2..7114d2c147 100644 --- a/packages/cli/src/commands/run/index.js +++ b/packages/cli/src/commands/run/index.js @@ -1,17 +1,16 @@ const handler = require("./runHandler") module.exports = { - command: "run [config]", + command: "run [dir]", aliases: ["$0"], desc: "Start budibase Server. You can access your apps and the builder from here if you have dev=true in your config", builder: yargs => { - yargs.positional("config", { + yargs.positional("dir", { type: "string", - describe: - "config file to use. optional, defaults to config.js. Use 'dev' as shorthand for 'config.dev.js' ", - alias: "c", - default: "config.js", + describe: "your budibase apps directory", + alias: "d", + default: "~/budibase", }) }, handler, diff --git a/packages/cli/src/commands/run/runHandler.js b/packages/cli/src/commands/run/runHandler.js index b0f7ba3887..30ca1ddb9b 100644 --- a/packages/cli/src/commands/run/runHandler.js +++ b/packages/cli/src/commands/run/runHandler.js @@ -1,9 +1,9 @@ -const { getAppContext } = require("../../common") const app = require("@budibase/server/app") +const { xPlatHomeDir } = require("../../common") -module.exports = ({ config }) => { - getAppContext({ configName: config, masterIsCreated: true }).then(context => { - app(context) - console.log(`Budibase Builder running on port ${context.config.port}..`) - }) +module.exports = ({ dir }) => { + dir = xPlatHomeDir(dir) + process.chdir(dir) + app() + console.log(`Budibase Builder running on port ${process.env.PORT}..`) } diff --git a/packages/cli/src/common.js b/packages/cli/src/common.js index 924f446909..dd9dc976da 100644 --- a/packages/cli/src/common.js +++ b/packages/cli/src/common.js @@ -1,7 +1,7 @@ -const { resolve } = require("path") +const { resolve, join } = require("path") const { cwd } = require("process") const buildAppContext = require("@budibase/server/initialise/buildAppContext") - +const { homedir } = require("os") module.exports.serverFileName = relativePath => resolve(__dirname, "..", "node_modules", "@budibase", "server", relativePath) @@ -17,3 +17,10 @@ module.exports.getAppContext = async ({ configName, masterIsCreated }) => { const config = require(resolve(cwd(), configName))() return await buildAppContext(config, masterIsCreated) } + +module.exports.xPlatHomeDir = dir => { + if (dir.startsWith("~")) { + dir = join(homedir(), dir.substring(1)) + } + return dir +} diff --git a/packages/cli/yarn.lock b/packages/cli/yarn.lock index 2b30e81e9d..d719badd42 100644 --- a/packages/cli/yarn.lock +++ b/packages/cli/yarn.lock @@ -1509,6 +1509,11 @@ destroy@^1.0.4: resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80" integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA= +dotenv@^8.2.0: + version "8.2.0" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a" + integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw== + ee-first@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" diff --git a/packages/server/db/client.js b/packages/server/db/client.js index d27823d543..c7c296e45b 100644 --- a/packages/server/db/client.js +++ b/packages/server/db/client.js @@ -6,7 +6,7 @@ module.exports = () => { process.env.COUCH_DB_URL || "http://admin:password@localhost:5984" const DATABASE_TYPE = process.env.DATABASE_TYPE || "couch" - const pouch = PouchDB.defaults({ + const pouch = PouchDB.defaults({ prefix: COUCH_DB_URL, }) allDbs(pouch)