|
| 1 | +import { promises as fs } from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { execa } from "execa"; |
| 5 | + |
| 6 | +const args = process.argv.slice(2); |
| 7 | +const command = args[0]; |
| 8 | +const pluginNamespace = args[1]; |
| 9 | + |
| 10 | +const here = path.dirname(fileURLToPath(import.meta.url)); |
| 11 | +const root = path.resolve(here, ".."); |
| 12 | +const pluginsDir = path.join(root, "plugins"); |
| 13 | + |
| 14 | +function printUsage() { |
| 15 | + console.log("Usage: node tools/plugin-db-manage.mjs <command> <plugin-namespace>"); |
| 16 | + console.log("Commands:"); |
| 17 | + console.log(" generate - Generate Prisma client for the plugin"); |
| 18 | + console.log(" migrate - Run migrations (dev)"); |
| 19 | + console.log(" deploy - Deploy migrations (prod)"); |
| 20 | + console.log(" studio - Open Prisma Studio"); |
| 21 | + process.exit(1); |
| 22 | +} |
| 23 | + |
| 24 | +if (!command || !pluginNamespace) { |
| 25 | + printUsage(); |
| 26 | +} |
| 27 | + |
| 28 | +async function findPluginDir(namespace) { |
| 29 | + // Try direct match first (e.g. "blog" -> plugins/blog) |
| 30 | + let target = path.join(pluginsDir, namespace); |
| 31 | + try { |
| 32 | + const stat = await fs.stat(target); |
| 33 | + if (stat.isDirectory()) return target; |
| 34 | + } catch {} |
| 35 | + |
| 36 | + // Try searching for matching namespace in plugin.json |
| 37 | + const entries = await fs.readdir(pluginsDir, { withFileTypes: true }); |
| 38 | + for (const entry of entries) { |
| 39 | + if (!entry.isDirectory()) continue; |
| 40 | + const pDir = path.join(pluginsDir, entry.name); |
| 41 | + try { |
| 42 | + const manifest = JSON.parse(await fs.readFile(path.join(pDir, "plugin.json"), "utf8")); |
| 43 | + if (manifest.namespace === namespace || manifest.id === namespace) { |
| 44 | + return pDir; |
| 45 | + } |
| 46 | + } catch {} |
| 47 | + } |
| 48 | + return null; |
| 49 | +} |
| 50 | + |
| 51 | +async function main() { |
| 52 | + const pluginDir = await findPluginDir(pluginNamespace); |
| 53 | + if (!pluginDir) { |
| 54 | + console.error(`Error: Plugin "${pluginNamespace}" not found.`); |
| 55 | + process.exit(1); |
| 56 | + } |
| 57 | + |
| 58 | + const schemaPath = path.join(pluginDir, "prisma/schema.prisma"); |
| 59 | + try { |
| 60 | + await fs.access(schemaPath); |
| 61 | + } catch { |
| 62 | + console.error(`Error: No prisma/schema.prisma found in ${pluginDir}`); |
| 63 | + console.error("This tool is only for plugins with dedicated databases."); |
| 64 | + process.exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + console.log(`[plugin-db] Managing database for ${pluginNamespace} in ${pluginDir}`); |
| 68 | + |
| 69 | + const prismaBin = path.join(root, "node_modules/.bin/prisma"); |
| 70 | + const envFile = path.join(root, ".env"); |
| 71 | + |
| 72 | + // Load env from root .env to ensure DATABASE_URL or other env vars are available if needed |
| 73 | + // But typically dedicated plugins should have their own env config or use a different var. |
| 74 | + // For now, we assume the user runs this with appropriate env vars set, or we load root .env. |
| 75 | + // We'll let prisma load .env from root if we run it from root. |
| 76 | + |
| 77 | + let prismaArgs = []; |
| 78 | + switch (command) { |
| 79 | + case "generate": |
| 80 | + prismaArgs = ["generate", "--schema", schemaPath]; |
| 81 | + break; |
| 82 | + case "migrate": |
| 83 | + prismaArgs = ["migrate", "dev", "--schema", schemaPath]; |
| 84 | + break; |
| 85 | + case "deploy": |
| 86 | + prismaArgs = ["migrate", "deploy", "--schema", schemaPath]; |
| 87 | + break; |
| 88 | + case "studio": |
| 89 | + prismaArgs = ["studio", "--schema", schemaPath]; |
| 90 | + break; |
| 91 | + default: |
| 92 | + printUsage(); |
| 93 | + } |
| 94 | + |
| 95 | + console.log(`Running: prisma ${prismaArgs.join(" ")}`); |
| 96 | + |
| 97 | + try { |
| 98 | + await execa(prismaBin, prismaArgs, { |
| 99 | + cwd: root, // Run from root so it picks up root .env |
| 100 | + stdio: "inherit", |
| 101 | + }); |
| 102 | + } catch (err) { |
| 103 | + console.error("Command failed."); |
| 104 | + process.exit(1); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +main().catch((err) => { |
| 109 | + console.error(err); |
| 110 | + process.exit(1); |
| 111 | +}); |
0 commit comments