forked from tsai/budibase
10 changed files with 149 additions and 52 deletions
@ -0,0 +1,44 @@ |
|||||
|
const fetch = require("node-fetch") |
||||
|
const { |
||||
|
downloadTemplate, |
||||
|
exportTemplateFromApp, |
||||
|
} = require("../../utilities/templates") |
||||
|
|
||||
|
const DEFAULT_TEMPLATES_BUCKET = |
||||
|
"prod-budi-templates.s3-eu-west-1.amazonaws.com" |
||||
|
|
||||
|
exports.fetch = async function(ctx) { |
||||
|
const { type = "app" } = ctx.query |
||||
|
|
||||
|
const response = await fetch( |
||||
|
`https://${DEFAULT_TEMPLATES_BUCKET}/manifest.json` |
||||
|
) |
||||
|
const json = await response.json() |
||||
|
ctx.body = Object.values(json.templates[type]) |
||||
|
} |
||||
|
|
||||
|
exports.downloadTemplate = async function(ctx) { |
||||
|
const { type, name } = ctx.params |
||||
|
|
||||
|
await downloadTemplate(type, name) |
||||
|
|
||||
|
ctx.body = { |
||||
|
message: `template ${type}:${name} downloaded successfully.`, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
exports.exportTemplateFromApp = async function(ctx) { |
||||
|
const { appId, instanceId } = ctx.user.appId |
||||
|
const { templateName } = ctx.request.body |
||||
|
|
||||
|
await exportTemplateFromApp({ |
||||
|
appId, |
||||
|
instanceId, |
||||
|
templateName, |
||||
|
}) |
||||
|
|
||||
|
ctx.status = 200 |
||||
|
ctx.body = { |
||||
|
message: `Created template: ${templateName}`, |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
const Router = require("@koa/router") |
||||
|
const controller = require("../controllers/templates") |
||||
|
const authorized = require("../../middleware/authorized") |
||||
|
const { BUILDER } = require("../../utilities/accessLevels") |
||||
|
|
||||
|
const router = Router() |
||||
|
|
||||
|
router |
||||
|
.get("/api/templates", authorized(BUILDER), controller.fetch) |
||||
|
.get( |
||||
|
"/api/templates/:type/:name", |
||||
|
// authorized(BUILDER),
|
||||
|
controller.downloadTemplate |
||||
|
) |
||||
|
.post("/api/templates", authorized(BUILDER), controller.exportTemplateFromApp) |
||||
|
|
||||
|
module.exports = router |
||||
@ -0,0 +1,60 @@ |
|||||
|
const path = require("path") |
||||
|
const fs = require("fs-extra") |
||||
|
const os = require("os") |
||||
|
const fetch = require("node-fetch") |
||||
|
const stream = require("stream") |
||||
|
const tar = require("tar-fs") |
||||
|
const zlib = require("zlib") |
||||
|
const { promisify } = require("util") |
||||
|
const streamPipeline = promisify(stream.pipeline) |
||||
|
const { budibaseAppsDir } = require("./budibaseDir") |
||||
|
const CouchDB = require("../db") |
||||
|
|
||||
|
const DEFAULT_TEMPLATES_BUCKET = |
||||
|
"prod-budi-templates.s3-eu-west-1.amazonaws.com" |
||||
|
|
||||
|
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) { |
||||
|
throw new Error( |
||||
|
`Error downloading template ${type}:${name}: ${response.statusText}` |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
// stream the response, unzip and extract
|
||||
|
await streamPipeline( |
||||
|
response.body, |
||||
|
zlib.Unzip(), |
||||
|
tar.extract(path.join(budibaseAppsDir(), "templates", type)) |
||||
|
) |
||||
|
|
||||
|
return path.join(budibaseAppsDir(), "templates", type, name) |
||||
|
} |
||||
|
|
||||
|
exports.exportTemplateFromApp = async function({ |
||||
|
appId, |
||||
|
templateName, |
||||
|
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) |
||||
|
|
||||
|
const templateOutputPath = path.join(templatesDir, templateName) |
||||
|
fs.copySync(appToExport, `${templateOutputPath}/pages`) |
||||
|
|
||||
|
fs.ensureDirSync(path.join(templateOutputPath, "db")) |
||||
|
const writeStream = fs.createWriteStream(`${templateOutputPath}/db/dump.txt`) |
||||
|
|
||||
|
// perform couch dump
|
||||
|
const instanceDb = new CouchDB(instanceId) |
||||
|
|
||||
|
console.log("Performing database dump..") |
||||
|
await instanceDb.dump(writeStream) |
||||
|
console.log("Export complete!") |
||||
|
} |
||||
Loading…
Reference in new issue