From 55bf142a95cbc9272c79266a3fdf60c3a44a7fb0 Mon Sep 17 00:00:00 2001 From: michael shanks Date: Sat, 27 Jul 2019 07:43:03 +0100 Subject: [PATCH] delete derived component endpoint --- .../components/anotherSubFolder/newTextBox.json | 1 - packages/server/middleware/routers.js | 10 +++++++--- packages/server/tests/builder.spec.js | 6 ++++++ packages/server/tests/testApp.js | 7 +++++++ packages/server/utilities/builder.js | 11 +++++++++-- 5 files changed, 29 insertions(+), 6 deletions(-) delete mode 100644 packages/server/appPackages/testApp/components/anotherSubFolder/newTextBox.json diff --git a/packages/server/appPackages/testApp/components/anotherSubFolder/newTextBox.json b/packages/server/appPackages/testApp/components/anotherSubFolder/newTextBox.json deleted file mode 100644 index f5de87bb1e..0000000000 --- a/packages/server/appPackages/testApp/components/anotherSubFolder/newTextBox.json +++ /dev/null @@ -1 +0,0 @@ -{"_name":"newTextBox","_component":"./customComponents/textbox","label":"something else"} diff --git a/packages/server/middleware/routers.js b/packages/server/middleware/routers.js index 387624b323..4aff7a7aa6 100644 --- a/packages/server/middleware/routers.js +++ b/packages/server/middleware/routers.js @@ -10,7 +10,8 @@ const { savePackage, getApps, saveDerivedComponent, - renameDerivedComponent + renameDerivedComponent, + deleteDerivedComponent } = require("../utilities/builder"); const builderPath = resolve(__dirname, "../builder"); @@ -174,11 +175,14 @@ module.exports = (config, app) => { ctx.request.body.newname); ctx.response.status = StatusCodes.OK; }) - .delete("/_builder/api/:appname/derivedcomponent", async (ctx) => { + .delete("/_builder/api/:appname/derivedcomponent/*", async (ctx) => { + const name = ctx.request.path.replace( + `/_builder/api/${ctx.params.appname}/derivedcomponent/`, ""); + await deleteDerivedComponent( config, ctx.params.appname, - ctx.request.body.name); + name); ctx.response.status = StatusCodes.OK; }) .get("/:appname", async (ctx) => { diff --git a/packages/server/tests/builder.spec.js b/packages/server/tests/builder.spec.js index 5823f2947d..d3bce5c635 100644 --- a/packages/server/tests/builder.spec.js +++ b/packages/server/tests/builder.spec.js @@ -118,5 +118,11 @@ it("should be able to rename derived component", async () => { }); it("should be able to delete derived component", async () => { + await app.delete("/_builder/api/testApp/derivedcomponent/anotherSubFolder/newTextBox") + .expect(statusCodes.OK); + const componentFile = "./appPackages/testApp/components/anotherSubFolder/newTextBox.json"; + const componentDir = "./appPackages/testApp/components/anotherSubFolder"; + expect(await pathExists(componentFile)).toBe(false); + expect(await pathExists(componentDir)).toBe(false); }); diff --git a/packages/server/tests/testApp.js b/packages/server/tests/testApp.js index 510a341b9c..9778eeb3ce 100644 --- a/packages/server/tests/testApp.js +++ b/packages/server/tests/testApp.js @@ -71,6 +71,7 @@ module.exports = () => { post: (url, body) => postRequest(server,url,body), patch: (url, body) => patchRequest(server,url,body), get: (url) => getRequest(server, url), + delete: (url) => deleteRequest(server, url), credentials: { masterOwner: { username: masterOwnerName, @@ -121,6 +122,12 @@ const postRequest = (server, url, body) => .send(body) .set('Accept', 'application/json'); + +const deleteRequest = (server, url) => + request(server) + .delete(url) + .set('Accept', 'application/json'); + const getRequest = (server, url) => request(server) .get(url) diff --git a/packages/server/utilities/builder.js b/packages/server/utilities/builder.js index bf22a19873..4826e62ffd 100644 --- a/packages/server/utilities/builder.js +++ b/packages/server/utilities/builder.js @@ -10,7 +10,8 @@ const { stat, ensureDir, rename, - unlink + unlink, + rmdir } = require("fs-extra"); const { resolve, @@ -94,7 +95,13 @@ module.exports.renameDerivedComponent = async (config, appname, oldName, newName module.exports.deleteDerivedComponent = async (config, appname, name) => { const appPath = appPackageFolder(config, appname); - await unlink(componentPath(appPath, name)); + const componentFile = componentPath(appPath, name); + await unlink(componentFile); + + const dir = dirname(componentFile); + if((await readdir(dir)).length === 0) { + await rmdir(dir); + } } const getRootComponents = async (appPath, pages ,lib) => {