|
| 1 | +import { parseArgs } from "@std/cli/parse-args"; |
| 2 | +import { Spinner } from "@std/cli/unstable-spinner"; |
| 3 | +import { seedWaitlist } from "./kv.ts"; |
| 4 | + |
| 5 | +async function main() { |
| 6 | + const args = parseArgs(Deno.args, { |
| 7 | + string: ["file"], |
| 8 | + alias: { |
| 9 | + f: "file", |
| 10 | + }, |
| 11 | + }); |
| 12 | + |
| 13 | + console.log("🌱 FartLabs Database CLI"); |
| 14 | + console.log("============================\n"); |
| 15 | + |
| 16 | + try { |
| 17 | + const spinner = new Spinner({ |
| 18 | + message: "Opening Deno KV database...", |
| 19 | + color: "yellow", |
| 20 | + }); |
| 21 | + spinner.start(); |
| 22 | + |
| 23 | + const kv = await Deno.openKv(Deno.env.get("DENO_KV_PATH")!); |
| 24 | + |
| 25 | + spinner.stop(); |
| 26 | + console.log("✅ Database connection established\n"); |
| 27 | + await seedDatabase(kv, args.file!); |
| 28 | + |
| 29 | + // Close the database connection |
| 30 | + await kv.close(); |
| 31 | + console.log("\n✅ Database operations completed successfully"); |
| 32 | + } catch (error) { |
| 33 | + console.error( |
| 34 | + "\n❌ Error:", |
| 35 | + error instanceof Error ? error.message : String(error), |
| 36 | + ); |
| 37 | + Deno.exit(1); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +async function seedDatabase(kv: Deno.Kv, filePath: string) { |
| 42 | + try { |
| 43 | + await Deno.stat(filePath); |
| 44 | + } catch { |
| 45 | + throw new Error(`Seed file not found: ${filePath}`); |
| 46 | + } |
| 47 | + |
| 48 | + const spinner = new Spinner({ |
| 49 | + message: "Reading seed data...", |
| 50 | + color: "cyan", |
| 51 | + }); |
| 52 | + spinner.start(); |
| 53 | + |
| 54 | + const fileContent = await Deno.readTextFile(filePath); |
| 55 | + const seedData: string[] = JSON.parse(fileContent); |
| 56 | + |
| 57 | + spinner.stop(); |
| 58 | + console.log(`📖 Loaded ${seedData.length} entries from ${filePath}\n`); |
| 59 | + |
| 60 | + // Seed the database |
| 61 | + spinner.message = "Seeding database..."; |
| 62 | + spinner.start(); |
| 63 | + |
| 64 | + await seedWaitlist(kv, seedData); |
| 65 | + |
| 66 | + spinner.stop(); |
| 67 | + console.log(`🌱 Successfully seeded ${seedData.length} entries`); |
| 68 | + |
| 69 | + // Verify the seeding |
| 70 | + spinner.message = "Verifying seed data..."; |
| 71 | + spinner.start(); |
| 72 | + |
| 73 | + spinner.stop(); |
| 74 | + console.log(`✅ Verification complete`); |
| 75 | +} |
| 76 | + |
| 77 | +// Show help if help requested |
| 78 | +if (Deno.args.includes("--help") || Deno.args.includes("-h")) { |
| 79 | + console.log(` |
| 80 | +Usage: deno run --allow-read --allow-env --unstable-kv database/cli.ts [options] |
| 81 | +
|
| 82 | +Options: |
| 83 | + -f, --file <path> Seed data file (default: database/seed-data.json) |
| 84 | + -h, --help Show this help message |
| 85 | +
|
| 86 | +Examples: |
| 87 | + deno run --allow-read --allow-env --unstable-kv database/cli.ts |
| 88 | + deno run --allow-read --allow-env --unstable-kv database/cli.ts --file custom-data.json |
| 89 | +`); |
| 90 | + Deno.exit(0); |
| 91 | +} |
| 92 | + |
| 93 | +// Run the main function |
| 94 | +if (import.meta.main) { |
| 95 | + main(); |
| 96 | +} |
0 commit comments