budibase/packages/server/scripts/exportAppTemplate.js

65 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2020-09-25 15:47:42 +02:00
#!/usr/bin/env node
2020-09-29 11:32:42 +02:00
const yargs = require("yargs")
const fs = require("fs")
const { join } = require("path")
require("../src/db").init()
const { db: dbCore } = require("@budibase/backend-core")
// load environment
const env = require("../src/environment")
const {
USER_METDATA_PREFIX,
LINK_USER_METADATA_PREFIX,
} = require("../src/db/utils")
2020-09-25 15:47:42 +02:00
2020-09-28 18:04:08 +02:00
// Script to export a chosen budibase app into a package
2021-01-11 16:34:43 +01:00
// Usage: ./scripts/exportAppTemplate.js export --name=Funky --appId=appId
2020-09-25 15:47:42 +02:00
2020-09-29 11:32:42 +02:00
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",
},
appId: {
description: "The appId of the application you want to export",
alias: "app",
type: "string",
},
},
2021-05-04 12:32:22 +02:00
async args => {
if (!env.isDev()) {
throw "Only works in dev"
}
const name = args.name,
appId = args.appId
2020-09-29 11:32:42 +02:00
console.log("Exporting app..")
if (name == null || appId == null) {
console.error(
"Unable to export without a name and app ID being specified, check help for more info."
)
return
}
const exportPath = join(process.cwd(), name, "db")
fs.ensureDirSync(exportPath)
const writeStream = fs.createWriteStream(join(exportPath, "dump.text"))
// perform couch dump
await dbCore.doWithDB(appId, async db => {
return db.dump(writeStream, {
filter: doc =>
!(
doc._id.includes(USER_METDATA_PREFIX) ||
doc.includes(LINK_USER_METADATA_PREFIX)
),
})
})
console.log(`Template ${name} exported to ${exportPath}`)
2020-09-29 11:32:42 +02:00
}
)
.help()
.alias("help", "h").argv