2022-08-10 14:11:57 +02:00
|
|
|
const Command = require("../structures/Command")
|
|
|
|
const { CommandWords } = require("../constants")
|
|
|
|
const { getSkeleton, fleshOutSkeleton } = require("./skeleton")
|
|
|
|
const questions = require("../questions")
|
|
|
|
const fs = require("fs")
|
2022-09-09 12:25:17 +02:00
|
|
|
const { PLUGIN_TYPE_ARR } = require("@budibase/types")
|
|
|
|
const { validate } = require("@budibase/backend-core/plugins")
|
2022-08-10 17:19:08 +02:00
|
|
|
const { runPkgCommand } = require("../exec")
|
|
|
|
const { join } = require("path")
|
2022-09-13 19:22:15 +02:00
|
|
|
const { success, error, info, moveDirectory } = require("../utils")
|
2022-08-10 14:11:57 +02:00
|
|
|
|
2022-08-18 14:29:49 +02:00
|
|
|
function checkInPlugin() {
|
|
|
|
if (!fs.existsSync("package.json")) {
|
|
|
|
throw new Error(
|
|
|
|
"Please run in a plugin directory - must contain package.json"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (!fs.existsSync("schema.json")) {
|
|
|
|
throw new Error(
|
|
|
|
"Please run in a plugin directory - must contain schema.json"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:11:57 +02:00
|
|
|
async function init(opts) {
|
|
|
|
const type = opts["init"] || opts
|
2022-09-09 12:25:17 +02:00
|
|
|
if (!type || !PLUGIN_TYPE_ARR.includes(type)) {
|
2022-08-10 17:19:08 +02:00
|
|
|
console.log(
|
|
|
|
error(
|
|
|
|
"Please provide a type to init, either 'component' or 'datasource'."
|
|
|
|
)
|
2022-08-10 14:11:57 +02:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2022-08-10 17:19:08 +02:00
|
|
|
console.log(info("Lets get some details about your new plugin:"))
|
2022-08-10 14:11:57 +02:00
|
|
|
const name = await questions.string("Name", `budibase-${type}`)
|
|
|
|
if (fs.existsSync(name)) {
|
2022-08-10 17:19:08 +02:00
|
|
|
console.log(
|
|
|
|
error("Directory by plugin name already exists, pick a new name.")
|
|
|
|
)
|
2022-08-10 14:11:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
const desc = await questions.string(
|
|
|
|
"Description",
|
|
|
|
`An amazing Budibase ${type}!`
|
|
|
|
)
|
|
|
|
const version = await questions.string("Version", "1.0.0")
|
2022-09-13 19:22:15 +02:00
|
|
|
console.log(
|
|
|
|
info(`By default the plugin will be created in the directory "${name}"`)
|
|
|
|
)
|
|
|
|
console.log(
|
|
|
|
info(
|
|
|
|
"if you are already in an empty directory, such as a new Git repo, you can disable this functionality."
|
|
|
|
)
|
|
|
|
)
|
|
|
|
const topLevel = await questions.confirmation("Create top level directory?")
|
2022-08-10 14:11:57 +02:00
|
|
|
// get the skeleton
|
2022-08-11 12:03:33 +02:00
|
|
|
console.log(info("Retrieving project..."))
|
2022-08-10 14:11:57 +02:00
|
|
|
await getSkeleton(type, name)
|
2022-08-18 19:23:07 +02:00
|
|
|
await fleshOutSkeleton(type, name, desc, version)
|
2022-08-11 12:03:33 +02:00
|
|
|
console.log(info("Installing dependencies..."))
|
2022-08-11 18:29:07 +02:00
|
|
|
await runPkgCommand("install", join(process.cwd(), name))
|
2022-09-13 19:22:15 +02:00
|
|
|
// if no parent directory desired move to cwd
|
|
|
|
if (!topLevel) {
|
|
|
|
moveDirectory(name, process.cwd())
|
|
|
|
console.log(info(`Plugin created in current directory.`))
|
|
|
|
} else {
|
|
|
|
console.log(info(`Plugin created in directory "${name}"`))
|
|
|
|
}
|
2022-08-10 14:11:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 17:27:03 +02:00
|
|
|
async function verify() {
|
2022-08-18 14:29:49 +02:00
|
|
|
// will throw errors if not acceptable
|
|
|
|
checkInPlugin()
|
2022-08-10 17:19:08 +02:00
|
|
|
console.log(info("Verifying plugin..."))
|
|
|
|
const schema = fs.readFileSync("schema.json", "utf8")
|
|
|
|
const pkg = fs.readFileSync("package.json", "utf8")
|
|
|
|
let name, version
|
|
|
|
try {
|
|
|
|
const schemaJson = JSON.parse(schema)
|
|
|
|
const pkgJson = JSON.parse(pkg)
|
|
|
|
if (!pkgJson.name || !pkgJson.version || !pkgJson.description) {
|
|
|
|
throw new Error(
|
|
|
|
"package.json is missing one of 'name', 'version' or 'description'."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
name = pkgJson.name
|
|
|
|
version = pkgJson.version
|
|
|
|
validate(schemaJson)
|
2022-08-16 17:27:03 +02:00
|
|
|
return { name, version }
|
2022-08-10 17:19:08 +02:00
|
|
|
} catch (err) {
|
|
|
|
if (err && err.message && err.message.includes("not valid JSON")) {
|
|
|
|
console.log(error(`schema.json is not valid JSON: ${err.message}`))
|
|
|
|
} else {
|
|
|
|
console.log(error(`Invalid schema/package.json: ${err.message}`))
|
|
|
|
}
|
2022-08-16 17:27:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function build() {
|
|
|
|
const verified = await verify()
|
|
|
|
if (!verified.name) {
|
2022-08-10 17:19:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(success("Verified!"))
|
|
|
|
console.log(info("Building plugin..."))
|
|
|
|
await runPkgCommand("build")
|
2022-08-16 17:27:03 +02:00
|
|
|
const output = join("dist", `${verified.name}-${verified.version}.tar.gz`)
|
2022-08-10 17:19:08 +02:00
|
|
|
console.log(success(`Build complete - output in: ${output}`))
|
|
|
|
}
|
2022-08-10 14:11:57 +02:00
|
|
|
|
2022-08-16 17:27:03 +02:00
|
|
|
async function watch() {
|
|
|
|
const verified = await verify()
|
|
|
|
if (!verified.name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const output = join("dist", `${verified.name}-${verified.version}.tar.gz`)
|
|
|
|
console.log(info(`Watching - build in: ${output}`))
|
|
|
|
try {
|
|
|
|
await runPkgCommand("watch")
|
|
|
|
} catch (err) {
|
|
|
|
// always errors when user escapes
|
|
|
|
console.log(success("Watch exited."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:11:57 +02:00
|
|
|
const command = new Command(`${CommandWords.PLUGIN}`)
|
|
|
|
.addHelp(
|
|
|
|
"Custom plugins for Budibase, init, build and verify your components and datasources with this tool."
|
|
|
|
)
|
|
|
|
.addSubOption(
|
|
|
|
"--init [type]",
|
|
|
|
"Init a new plugin project, with a type of either component or datasource.",
|
|
|
|
init
|
|
|
|
)
|
|
|
|
.addSubOption(
|
|
|
|
"--build",
|
|
|
|
"Build your plugin, this will verify and produce a final tarball for your project.",
|
|
|
|
build
|
|
|
|
)
|
2022-08-16 17:27:03 +02:00
|
|
|
.addSubOption(
|
|
|
|
"--watch",
|
|
|
|
"Automatically build any changes to your plugin.",
|
|
|
|
watch
|
|
|
|
)
|
2022-08-10 14:11:57 +02:00
|
|
|
|
|
|
|
exports.command = command
|