Adding check to make sure build/watch occurs inside a plugin directory.

This commit is contained in:
mike12345567 2022-08-18 13:29:49 +01:00
parent d063d4fa67
commit 9d22f83e4e
4 changed files with 53 additions and 26 deletions

View File

@ -9,6 +9,19 @@ const { runPkgCommand } = require("../exec")
const { join } = require("path")
const { success, error, info } = require("../utils")
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"
)
}
}
async function init(opts) {
const type = opts["init"] || opts
if (!type || !PLUGIN_TYPES_ARR.includes(type)) {
@ -42,6 +55,8 @@ async function init(opts) {
}
async function verify() {
// will throw errors if not acceptable
checkInPlugin()
console.log(info("Verifying plugin..."))
const schema = fs.readFileSync("schema.json", "utf8")
const pkg = fs.readFileSync("package.json", "utf8")

View File

@ -22,6 +22,8 @@ function validateComponent(schema) {
const validator = joi.object({
type: joi.string().allow("component").required(),
metadata: joi.object().unknown(true).required(),
hash: joi.string().optional(),
version: joi.string().optional(),
schema: joi
.object({
name: joi.string().required(),
@ -53,6 +55,8 @@ function validateDatasource(schema) {
const validator = joi.object({
type: joi.string().allow("datasource").required(),
metadata: joi.object().unknown(true).required(),
hash: joi.string().optional(),
version: joi.string().optional(),
schema: joi.object({
docs: joi.string(),
friendlyName: joi.string().required(),

View File

@ -17,15 +17,12 @@ const bullboard = require("./automations/bullboard")
const { logAlert } = require("@budibase/backend-core/logging")
const { pinoSettings } = require("@budibase/backend-core")
const { Thread } = require("./threads")
const chokidar = require("chokidar")
const fs = require("fs")
const path = require("path")
import redis from "./utilities/redis"
import * as migrations from "./migrations"
import { events, installation, tenancy } from "@budibase/backend-core"
import { createAdminUser, getChecklist } from "./utilities/workerRequests"
import { processPlugin } from "./api/controllers/plugin"
import { DEFAULT_TENANT_ID } from "@budibase/backend-core/constants"
import { watch } from "./watch"
const app = new Koa()
@ -144,28 +141,7 @@ module.exports = server.listen(env.PORT || 0, async () => {
env.PLUGINS_DIR &&
fs.existsSync(env.PLUGINS_DIR)
) {
const watchPath = path.join(env.PLUGINS_DIR, "./**/*.tar.gz")
chokidar
.watch(watchPath, {
ignored: "**/node_modules",
awaitWriteFinish: true,
})
.on("all", async (event: string, path: string) => {
// Sanity checks
if (!path?.endsWith(".tar.gz") || !fs.existsSync(path)) {
return
}
await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => {
try {
const split = path.split("/")
const name = split[split.length - 1]
console.log("Importing plugin:", path)
await processPlugin({ name, path })
} catch (err) {
console.log("Failed to import plugin:", err)
}
})
})
watch()
}
// check for version updates

View File

@ -0,0 +1,32 @@
import path from "path"
import * as env from "./environment"
import chokidar from "chokidar"
import fs from "fs"
import { tenancy } from "@budibase/backend-core"
import { DEFAULT_TENANT_ID } from "@budibase/backend-core/constants"
import { processPlugin } from "./api/controllers/plugin"
export function watch() {
const watchPath = path.join(env.PLUGINS_DIR, "./**/*.tar.gz")
chokidar
.watch(watchPath, {
ignored: "**/node_modules",
awaitWriteFinish: true,
})
.on("all", async (event: string, path: string) => {
// Sanity checks
if (!path?.endsWith(".tar.gz") || !fs.existsSync(path)) {
return
}
await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => {
try {
const split = path.split("/")
const name = split[split.length - 1]
console.log("Importing plugin:", path)
await processPlugin({ name, path })
} catch (err) {
console.log("Failed to import plugin:", err)
}
})
})
}