@laiyon/create-wasapi
Version:
CLI to create WhatsApp bot projects with Wasapi and BuilderBot
105 lines (104 loc) ⢠4.47 kB
JavaScript
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
};
}
}