From 91c86172e45014d74b11959fd43ea0d0afd1e688 Mon Sep 17 00:00:00 2001 From: Hyko Date: Tue, 14 Apr 2026 19:29:02 -0400 Subject: [PATCH] fix: update database CLI entry point path Move the database CLI from `src/cli/database.js` to `src/core/database/cli.js` to better reflect its association with the database module. Update both the `package.json` bin path and `tsup.config.js` entry points accordingly. --- src/cli/database.js | 131 -------------------------------------------- 1 file changed, 131 deletions(-) delete mode 100644 src/cli/database.js diff --git a/src/cli/database.js b/src/cli/database.js deleted file mode 100644 index 4edc55c..0000000 --- a/src/cli/database.js +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/env node - -/** - * Zen Database CLI - * Command-line tool for database management - */ - -// Load environment variables from the project's .env file -import dotenv from 'dotenv'; -import { resolve } from 'node:path'; - -// Load .env from the current working directory (user's project) -dotenv.config({ path: resolve(process.cwd(), '.env') }); -dotenv.config({ path: resolve(process.cwd(), '.env.local') }); - -// The CLI always runs locally, so default to development to use ZEN_DATABASE_URL_DEV if set -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; - -import { testConnection, closePool } from '../core/database/index.js'; -import readline from 'readline'; -import { step, done, warn, fail } from '../shared/lib/logger.js'; - -function printHelp() { - console.log(` -Zen Database CLI - -Usage: - npx zen-db - -Commands: - init Initialize database (create all required tables) - test Test database connection - drop Drop all tables (DANGER!) - help Show this help message - -Example: - npx zen-db init - `); -} - -/** - * Prompt the user for a confirmation answer. - * @param {string} question - * @returns {Promise} The trimmed, lowercased answer - */ -function askConfirmation(question) { - return new Promise((resolve) => { - const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); - rl.question(question, (answer) => { - rl.close(); - resolve(answer.trim().toLowerCase()); - }); - }); -} - -async function runCLI() { - const command = process.argv[2]; - - if (!command) { - printHelp(); - process.exit(0); - } - - try { - switch (command) { - case 'init': { - step('Initializing database...'); - - const { initFeatures } = await import('../features/init.js'); - const featuresResult = await initFeatures(); - - done(`DB ready — ${featuresResult.created.length} tables created, ${featuresResult.skipped.length} skipped`); - break; - } - - case 'test': - step('Testing database connection...'); - const isConnected = await testConnection(); - if (isConnected) { - done('Database connection successful'); - } else { - fail('Database connection failed'); - process.exit(1); - } - break; - - case 'drop': { - warn('This will delete all tables!'); - process.stdout.write(' Type "yes" to confirm or Ctrl+C to cancel...\n'); - const answer = await askConfirmation('Confirm (yes/no): '); - if (answer === 'yes') { - const { dropFeatures } = await import('../features/init.js'); - await dropFeatures(); - done('Tables dropped successfully'); - } else { - warn('Operation cancelled'); - } - break; - } - - case 'help': - printHelp(); - break; - - default: - fail(`Unknown command: ${command}`); - process.stdout.write(' Run "npx zen-db help" for usage information.\n'); - process.exit(1); - } - - await closePool(); - process.exit(0); - - } catch (error) { - fail(`Error: ${error.message}`); - await closePool(); - process.exit(1); - } -} - -// Run CLI if called directly -import { fileURLToPath } from 'url'; -import { realpathSync } from 'node:fs'; -const __filename = realpathSync(fileURLToPath(import.meta.url)); -const isMainModule = process.argv[1] && realpathSync(process.argv[1]) === __filename; - -if (isMainModule) { - runCLI(); -} - -export { runCLI };