UNPKG

@laiyon/create-wasapi

Version:

CLI to create WhatsApp bot projects with Wasapi and BuilderBot

105 lines (104 loc) • 4.47 kB
import chalk from "chalk"; import { DATABASE_CHOICES } from "../ui/prompts/databaseSetup.js"; import { validateTemplate } from "../templates/templateConfig.js"; import { selectPhone } from "./phoneSelector.js"; import { DatabaseConfigService } from "./database/services/DatabaseConfigService.js"; import inquirer from "inquirer"; export class ProjectSetupService { static async setupProject(projectNameFromArgs) { // STEP 3: Show project name or create project name console.log(chalk.bgGreen.black(" šŸ“ PROJECT SETUP ")); console.log(""); // Validate project name if provided via command line if (projectNameFromArgs) { if (!/^[a-zA-Z0-9_-]+$/.test(projectNameFromArgs)) { console.error("āŒ Project name can only contain letters, numbers, hyphens, and underscores"); process.exit(1); } console.log(chalk.green(` Project: ${chalk.bold(projectNameFromArgs)}`)); console.log(""); } // Get project name first let projectName; if (projectNameFromArgs) { projectName = projectNameFromArgs; } else { const { projectName: name } = await inquirer.prompt([ { type: "input", name: "projectName", message: chalk.blue("šŸ“ Project name:"), default: "wasabot-app" } ]); projectName = name; } // STEP 4: Wasapi environment API key and then select number (FIRST) console.log(""); console.log(chalk.bgBlue.white(" šŸ”‘ WHATSAPP CONFIGURATION ")); console.log(""); const selectedPhone = await selectPhone(); if (!selectedPhone) { console.error("\nāŒ No phone selected"); process.exit(1); } const { phone, phoneNumber, apiKey } = selectedPhone; console.log(`\nāœ… Phone selected: ${phoneNumber}`); // STEP 5: Database selection (SECOND) console.log(""); console.log(chalk.bgYellow.black(" šŸ—„ļø DATABASE SELECTION ")); console.log(""); const { dbType } = await inquirer.prompt([ { type: "list", name: "dbType", message: chalk.blue("šŸ—„ļø Database:"), choices: DATABASE_CHOICES, default: "base-ts-wasapi-memory" } ]); // Validate the selected template if (!validateTemplate(dbType)) { console.error(`āŒ Template "${dbType}" is not valid`); process.exit(1); } // STEP 6: Database configuration (if not memory or json) let dbConfig = undefined; let databaseConnectionFailed = false; if (dbType !== "base-ts-wasapi-memory" && dbType !== "base-ts-wasapi-json") { console.log(""); console.log(chalk.bgCyan.black(" āš™ļø DATABASE CONFIGURATION ")); console.log(""); try { const dbTypeName = dbType.replace('base-ts-wasapi-', ''); dbConfig = await DatabaseConfigService.configureDatabase(dbTypeName); // Test real database connection using optimized validation const isValid = await DatabaseConfigService.validateDatabaseConnection(dbTypeName, dbConfig); if (isValid) { console.log(`āœ… Successfully connected to ${dbTypeName.toUpperCase()}`); } else { throw new Error(`Could not connect to ${dbTypeName.toUpperCase()}`); } } catch (error) { console.error(`\nāŒ Error configuring database: ${error}`); console.log("šŸ“ Database connection failed. You'll need to fix this manually."); console.log("šŸ“ Continuing with basic configuration..."); // Mark that database connection failed, but keep the config for .env file console.log("šŸ“ Database configuration will be saved to .env file for manual troubleshooting."); databaseConnectionFailed = true; } } return { projectName, dbType, phone, phoneNumber, apiKey, dbConfig, databaseConnectionFailed }; } }