From b7ad4de09e528c2026ed2991da34932f847ce47d Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Tue, 26 May 2020 16:29:16 +0100 Subject: [PATCH] server - create app endpoint creates files --- .../server/src/api/controllers/application.js | 71 ++++++++++++++++++- .../utilities/appDirectoryTemplate/.gitignore | 1 + .../appDirectoryTemplate/package.json | 11 +++ .../appDirectoryTemplate/pages/main/page.json | 19 +++++ .../pages/main/screens/.gitkeep | 0 .../pages/unauthenticated/page.json | 19 +++++ .../pages/unauthenticated/screens/.gitkeep | 0 .../utilities/appDirectoryTemplate/plugins.js | 1 + 8 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 packages/server/src/utilities/appDirectoryTemplate/.gitignore create mode 100644 packages/server/src/utilities/appDirectoryTemplate/package.json create mode 100644 packages/server/src/utilities/appDirectoryTemplate/pages/main/page.json create mode 100644 packages/server/src/utilities/appDirectoryTemplate/pages/main/screens/.gitkeep create mode 100644 packages/server/src/utilities/appDirectoryTemplate/pages/unauthenticated/page.json create mode 100644 packages/server/src/utilities/appDirectoryTemplate/pages/unauthenticated/screens/.gitkeep create mode 100644 packages/server/src/utilities/appDirectoryTemplate/plugins.js diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index c4b49d1f74..c9c072fc2b 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -3,6 +3,10 @@ const ClientDb = require("../../db/clientDb") const { getPackageForBuilder } = require("../../utilities/builder") const newid = require("../../db/newid") const env = require("../../environment") +const instanceController = require("./instance") +const { resolve, join } = require("path") +const { copy, readJSON, writeJSON, exists } = require("fs-extra") +const { exec } = require("child_process") exports.fetch = async function(ctx) { const db = new CouchDB(ClientDb.name(env.CLIENT_ID)) @@ -32,12 +36,77 @@ exports.create = async function(ctx) { "@budibase/standard-components", "@budibase/materialdesign-components", ], - ...ctx.request.body, + name: ctx.request.body.name, + description: ctx.request.body.description, } const { rev } = await db.post(newApplication) newApplication._rev = rev + const createInstCtx = { + params: { + clientId: env.CLIENT_ID, + applicationId: newApplication._id, + }, + request: { + body: { name: `dev-${env.CLIENT_ID}` }, + }, + } + await instanceController.create(createInstCtx) + + if (env.NODE_ENV === "production") { + const newAppFolder = await createEmptyAppPackage(ctx, newApplication) + await runNpmInstall(newAppFolder) + } + ctx.body = newApplication ctx.message = `Application ${ctx.request.body.name} created successfully` } + +const createEmptyAppPackage = async (ctx, app) => { + const templateFolder = resolve( + __dirname, + "..", + "..", + "utilities", + "appDirectoryTemplate" + ) + + const appsFolder = env.BUDIBASE_DIR + const newAppFolder = resolve(appsFolder, app._id) + + if (await exists(newAppFolder)) { + ctx.throw(400, "App folder already exists for this application") + return + } + + await copy(templateFolder, newAppFolder) + + const packageJsonPath = join(appsFolder, app._id, "package.json") + const packageJson = await readJSON(packageJsonPath) + + packageJson.name = npmFriendlyAppName(app.name) + + await writeJSON(packageJsonPath, packageJson) + + return newAppFolder +} + +const runNpmInstall = async newAppFolder => { + return new Promise((resolve, reject) => { + const cmd = `cd ${newAppFolder} && npm install` + exec(cmd, (error, stdout, stderr) => { + if (error) { + reject(error) + } + resolve(stdout ? stdout : stderr) + }) + }) +} + +const npmFriendlyAppName = name => + name + .replace(/_/g, "") + .replace(/./g, "") + .replace(/ /g, "") + .toLowerCase() diff --git a/packages/server/src/utilities/appDirectoryTemplate/.gitignore b/packages/server/src/utilities/appDirectoryTemplate/.gitignore new file mode 100644 index 0000000000..77738287f0 --- /dev/null +++ b/packages/server/src/utilities/appDirectoryTemplate/.gitignore @@ -0,0 +1 @@ +dist/ \ No newline at end of file diff --git a/packages/server/src/utilities/appDirectoryTemplate/package.json b/packages/server/src/utilities/appDirectoryTemplate/package.json new file mode 100644 index 0000000000..f49e35d23f --- /dev/null +++ b/packages/server/src/utilities/appDirectoryTemplate/package.json @@ -0,0 +1,11 @@ +{ + "name": "name", + "version": "1.0.0", + "description": "", + "author": "", + "license": "ISC", + "dependencies": { + "@budibase/standard-components": "0.x", + "@budibase/materialdesign-components": "0.x" + } +} diff --git a/packages/server/src/utilities/appDirectoryTemplate/pages/main/page.json b/packages/server/src/utilities/appDirectoryTemplate/pages/main/page.json new file mode 100644 index 0000000000..89a23a78e5 --- /dev/null +++ b/packages/server/src/utilities/appDirectoryTemplate/pages/main/page.json @@ -0,0 +1,19 @@ +{ + "title": "Test App", + "favicon": "./_shared/favicon.png", + "stylesheets": [], + "componentLibraries": ["@budibase/standard-components", "@budibase/materialdesign-components"], + "props" : { + "_component": "@budibase/standard-components/container", + "_children": [], + "_id": 0, + "type": "div", + "_styles": { + "layout": {}, + "position": {} + }, + "_code": "" + }, + "_css": "", + "uiFunctions": "" +} diff --git a/packages/server/src/utilities/appDirectoryTemplate/pages/main/screens/.gitkeep b/packages/server/src/utilities/appDirectoryTemplate/pages/main/screens/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/server/src/utilities/appDirectoryTemplate/pages/unauthenticated/page.json b/packages/server/src/utilities/appDirectoryTemplate/pages/unauthenticated/page.json new file mode 100644 index 0000000000..14d0301c24 --- /dev/null +++ b/packages/server/src/utilities/appDirectoryTemplate/pages/unauthenticated/page.json @@ -0,0 +1,19 @@ +{ + "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/server/src/utilities/appDirectoryTemplate/pages/unauthenticated/screens/.gitkeep b/packages/server/src/utilities/appDirectoryTemplate/pages/unauthenticated/screens/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/server/src/utilities/appDirectoryTemplate/plugins.js b/packages/server/src/utilities/appDirectoryTemplate/plugins.js new file mode 100644 index 0000000000..44368bf6ec --- /dev/null +++ b/packages/server/src/utilities/appDirectoryTemplate/plugins.js @@ -0,0 +1 @@ +module.exports = () => ({})