First part of plugin CLI - generates the skeleton project and enriches it with user input.
This commit is contained in:
parent
0ec6ac3372
commit
b3f6a481b4
|
@ -1,51 +1,31 @@
|
||||||
{
|
{
|
||||||
"name": "@budibase/cli",
|
"name": "budibase-component",
|
||||||
"version": "1.2.27",
|
"version": "1.0.0",
|
||||||
"description": "Budibase CLI, for developers, self hosting and migrations.",
|
"description": "An amazing Budibase component!",
|
||||||
"main": "src/index.js",
|
"license": "UNLICENSED",
|
||||||
"bin": {
|
"svelte": "src/index.js",
|
||||||
"budi": "src/index.js"
|
"module": "dist/plugin.min.js",
|
||||||
},
|
|
||||||
"author": "Budibase",
|
|
||||||
"license": "GPL-3.0",
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prebuild": "rm -rf prebuilds 2> /dev/null && cp -r node_modules/leveldown/prebuilds prebuilds",
|
"build": "npm-run-all build:js build:bundle",
|
||||||
"build": "yarn prebuild && renamer --find .node --replace .fake 'prebuilds/**' && pkg . --out-path build && yarn postbuild",
|
"build:js": "rollup -c",
|
||||||
"postbuild": "rm -rf prebuilds 2> /dev/null"
|
"build:bundle": "node scripts/bundle.cjs"
|
||||||
},
|
|
||||||
"pkg": {
|
|
||||||
"targets": [
|
|
||||||
"node16-linux",
|
|
||||||
"node16-win",
|
|
||||||
"node16-macos"
|
|
||||||
],
|
|
||||||
"assets": [
|
|
||||||
"node_modules/@budibase/backend-core/dist/**/*",
|
|
||||||
"prebuilds/**/*"
|
|
||||||
],
|
|
||||||
"outputPath": "build"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@budibase/backend-core": "1.1.32-alpha.6",
|
"svelte": "^3.49.0"
|
||||||
"axios": "0.21.2",
|
|
||||||
"chalk": "4.1.0",
|
|
||||||
"cli-progress": "3.11.2",
|
|
||||||
"commander": "7.1.0",
|
|
||||||
"docker-compose": "0.23.6",
|
|
||||||
"dotenv": "16.0.1",
|
|
||||||
"inquirer": "8.0.0",
|
|
||||||
"lookpath": "1.1.0",
|
|
||||||
"node-fetch": "2",
|
|
||||||
"pkg": "5.7.0",
|
|
||||||
"posthog-node": "1.0.7",
|
|
||||||
"pouchdb": "7.3.0",
|
|
||||||
"pouchdb-replication-stream": "1.2.9",
|
|
||||||
"randomstring": "1.1.5",
|
|
||||||
"tar": "6.1.11"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"copyfiles": "^2.4.1",
|
"@rollup/plugin-commonjs": "^18.0.0",
|
||||||
"eslint": "^7.20.0",
|
"@rollup/plugin-node-resolve": "^11.2.1",
|
||||||
"renamer": "^4.0.0"
|
"npm-run-all": "^4.1.5",
|
||||||
|
"postcss": "^8.2.10",
|
||||||
|
"rollup": "^2.44.0",
|
||||||
|
"rollup-plugin-copy2": "^0.3.1",
|
||||||
|
"rollup-plugin-json": "^4.0.0",
|
||||||
|
"rollup-plugin-polyfill-node": "^0.8.0",
|
||||||
|
"rollup-plugin-postcss": "^4.0.0",
|
||||||
|
"rollup-plugin-svelte": "^7.1.0",
|
||||||
|
"rollup-plugin-svg": "^2.0.0",
|
||||||
|
"rollup-plugin-terser": "^7.0.2",
|
||||||
|
"tar": "^6.1.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ exports.CommandWords = {
|
||||||
HOSTING: "hosting",
|
HOSTING: "hosting",
|
||||||
ANALYTICS: "analytics",
|
ANALYTICS: "analytics",
|
||||||
HELP: "help",
|
HELP: "help",
|
||||||
|
PLUGIN: "plugins",
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.InitTypes = {
|
exports.InitTypes = {
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
const analytics = require("./analytics")
|
const analytics = require("./analytics")
|
||||||
const hosting = require("./hosting")
|
const hosting = require("./hosting")
|
||||||
const backups = require("./backups")
|
const backups = require("./backups")
|
||||||
|
const plugins = require("./plugins")
|
||||||
|
|
||||||
exports.getCommands = () => {
|
exports.getCommands = () => {
|
||||||
return [hosting.command, analytics.command, backups.command]
|
return [hosting.command, analytics.command, backups.command, plugins.command]
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
const Command = require("../structures/Command")
|
||||||
|
const { CommandWords } = require("../constants")
|
||||||
|
const { getSkeleton, fleshOutSkeleton } = require("./skeleton")
|
||||||
|
const questions = require("../questions")
|
||||||
|
const fs = require("fs")
|
||||||
|
|
||||||
|
const PLUGIN_TYPES = ["component", "datasource"]
|
||||||
|
|
||||||
|
async function init(opts) {
|
||||||
|
const type = opts["init"] || opts
|
||||||
|
if (!type || !PLUGIN_TYPES.includes(type)) {
|
||||||
|
console.error(
|
||||||
|
"Please provide a type to init, either 'component' or 'datasource'."
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
console.log("Lets get some details about your new plugin:")
|
||||||
|
const name = await questions.string("Name", `budibase-${type}`)
|
||||||
|
if (fs.existsSync(name)) {
|
||||||
|
console.error("Directory by plugin name already exists, pick a new name.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const desc = await questions.string(
|
||||||
|
"Description",
|
||||||
|
`An amazing Budibase ${type}!`
|
||||||
|
)
|
||||||
|
const version = await questions.string("Version", "1.0.0")
|
||||||
|
// get the skeleton
|
||||||
|
await getSkeleton(type, name)
|
||||||
|
await fleshOutSkeleton(name, desc, version)
|
||||||
|
console.log(`Plugin created in directory "${name}"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function build() {}
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
exports.command = command
|
|
@ -0,0 +1,60 @@
|
||||||
|
const fetch = require("node-fetch")
|
||||||
|
const download = require("download")
|
||||||
|
const fs = require("fs")
|
||||||
|
const os = require("os")
|
||||||
|
const { join } = require("path")
|
||||||
|
const tar = require("tar")
|
||||||
|
const { processStringSync } = require("@budibase/string-templates")
|
||||||
|
|
||||||
|
const HBS_FILES = ["package.json.hbs", "schema.json.hbs"]
|
||||||
|
|
||||||
|
async function getSkeletonUrl(type) {
|
||||||
|
const resp = await fetch(
|
||||||
|
"https://api.github.com/repos/budibase/budibase-skeleton/releases/latest"
|
||||||
|
)
|
||||||
|
if (resp.status >= 300) {
|
||||||
|
throw new Error("Failed to retrieve skeleton metadata")
|
||||||
|
}
|
||||||
|
const json = await resp.json()
|
||||||
|
for (let asset of json["assets"]) {
|
||||||
|
if (asset.name && asset.name.includes(type)) {
|
||||||
|
return asset["browser_download_url"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error("No skeleton found in latest release.")
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.getSkeleton = async (type, name) => {
|
||||||
|
const url = await getSkeletonUrl(type)
|
||||||
|
const tarballFile = join(os.tmpdir(), "skeleton.tar.gz")
|
||||||
|
|
||||||
|
// download the full skeleton tarball
|
||||||
|
fs.writeFileSync(tarballFile, await download(url))
|
||||||
|
fs.mkdirSync(name)
|
||||||
|
// extract it and get what we need
|
||||||
|
await tar.extract({
|
||||||
|
file: tarballFile,
|
||||||
|
C: name,
|
||||||
|
})
|
||||||
|
// clear up
|
||||||
|
fs.rmSync(tarballFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.fleshOutSkeleton = async (name, description, version) => {
|
||||||
|
for (let file of HBS_FILES) {
|
||||||
|
const oldFile = join(name, file),
|
||||||
|
newFile = join(name, file.substring(0, file.length - 4))
|
||||||
|
const hbsContents = fs.readFileSync(oldFile, "utf8")
|
||||||
|
if (!hbsContents) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const output = processStringSync(hbsContents, {
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
version,
|
||||||
|
})
|
||||||
|
// write the updated file and remove the HBS file
|
||||||
|
fs.writeFileSync(newFile, output)
|
||||||
|
fs.rmSync(oldFile)
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue