From e7c3afb7f1df1ea7cd3f831e5c98097ea00dd180 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 29 Sep 2020 10:32:42 +0100 Subject: [PATCH] CLI tool for exporting apps, tidy up --- packages/server/scripts/exportAppTemplate.js | 42 ++++++++++++++++---- packages/server/src/api/routes/templates.js | 2 +- packages/server/src/utilities/templates.js | 5 +-- 3 files changed, 37 insertions(+), 12 deletions(-) mode change 100644 => 100755 packages/server/scripts/exportAppTemplate.js diff --git a/packages/server/scripts/exportAppTemplate.js b/packages/server/scripts/exportAppTemplate.js old mode 100644 new mode 100755 index 78e6764ffa..95ab8c718c --- a/packages/server/scripts/exportAppTemplate.js +++ b/packages/server/scripts/exportAppTemplate.js @@ -1,12 +1,40 @@ #!/usr/bin/env node const { exportTemplateFromApp } = require("../src/utilities/templates") +const yargs = require("yargs") // Script to export a chosen budibase app into a package +// Usage: ./scripts/exportAppTemplate.js export --name=Funky --instanceId=someInstanceId --appId=appId -const [name, instanceId, appId] = process.argv.slice(1) - -exportTemplateFromApp({ - templateName: "Funky", - instanceId: "inst_b70abba_16feb394866140a1ac3f2e450e99f28a", - appId: "b70abba3874546bf99a339911b579937", -}) +yargs + .command( + "export", + "Export an existing budibase application to the .budibase/templates directory", + { + name: { + description: "The name of the newly exported template", + alias: "n", + type: "string", + }, + instanceId: { + description: "The instanceId to dump the database for", + alias: "inst", + type: "string", + }, + appId: { + description: "The appId of the application you want to export", + alias: "app", + type: "string", + }, + }, + async args => { + console.log("Exporting app..") + const exportPath = await exportTemplateFromApp({ + templateName: args.name, + instanceId: args.instanceId, + appId: args.appId, + }) + console.log(`Template ${args.name} exported to ${exportPath}`) + } + ) + .help() + .alias("help", "h").argv diff --git a/packages/server/src/api/routes/templates.js b/packages/server/src/api/routes/templates.js index 1dc5b46342..3e481610ce 100644 --- a/packages/server/src/api/routes/templates.js +++ b/packages/server/src/api/routes/templates.js @@ -9,7 +9,7 @@ router .get("/api/templates", authorized(BUILDER), controller.fetch) .get( "/api/templates/:type/:name", - // authorized(BUILDER), + authorized(BUILDER), controller.downloadTemplate ) .post("/api/templates", authorized(BUILDER), controller.exportTemplateFromApp) diff --git a/packages/server/src/utilities/templates.js b/packages/server/src/utilities/templates.js index e3d79a7943..4aef7f7db3 100644 --- a/packages/server/src/utilities/templates.js +++ b/packages/server/src/utilities/templates.js @@ -15,7 +15,6 @@ const DEFAULT_TEMPLATES_BUCKET = exports.downloadTemplate = async function(type, name) { const templateUrl = `https://${DEFAULT_TEMPLATES_BUCKET}/templates/${type}/${name}.tar.gz` - console.log(templateUrl, type, name) const response = await fetch(templateUrl) if (!response.ok) { @@ -40,7 +39,6 @@ exports.exportTemplateFromApp = async function({ instanceId, }) { // Copy frontend files - console.log("Copying frontend definition...") const appToExport = path.join(os.homedir(), ".budibase", appId, "pages") const templatesDir = path.join(os.homedir(), ".budibase", "templates") fs.ensureDirSync(templatesDir) @@ -54,7 +52,6 @@ exports.exportTemplateFromApp = async function({ // perform couch dump const instanceDb = new CouchDB(instanceId) - console.log("Performing database dump..") await instanceDb.dump(writeStream) - console.log("Export complete!") + return templateOutputPath }