refractor: migrate inquirer to @clack/prompts

This commit is contained in:
Aman Varshney
2025-02-13 19:05:21 +05:30
parent 8245d18528
commit 76a14ea26e
7 changed files with 264 additions and 350 deletions

View File

@@ -1,11 +1,8 @@
import os from "node:os";
import path from "node:path";
import { confirm, input } from "@inquirer/prompts";
import chalk from "chalk";
import * as p from "@clack/prompts";
import { $ } from "execa";
import fs from "fs-extra";
import ora, { type Ora } from "ora";
import { logger } from "../utils/logger";
import { isTursoInstalled, isTursoLoggedIn } from "../utils/turso-cli";
interface TursoConfig {
@@ -13,21 +10,21 @@ interface TursoConfig {
authToken: string;
}
async function loginToTurso(spinner: Ora) {
async function loginToTurso() {
const spinner = p.spinner();
try {
spinner.start("Logging in to Turso...");
await $`turso auth login`;
spinner.succeed("Logged in to Turso successfully!");
console.log();
spinner.stop("Logged in to Turso successfully!");
} catch (error) {
spinner.fail("Failed to log in to Turso");
spinner.stop("Failed to log in to Turso");
throw error;
}
}
async function installTursoCLI(isMac: boolean, spinner: Ora) {
async function installTursoCLI(isMac: boolean) {
const spinner = p.spinner();
try {
console.log();
spinner.start("Installing Turso CLI...");
if (isMac) {
@@ -38,15 +35,14 @@ async function installTursoCLI(isMac: boolean, spinner: Ora) {
await $`bash -c '${installScript}'`;
}
spinner.succeed("Turso CLI installed successfully!");
console.log();
spinner.stop("Turso CLI installed successfully!");
} catch (error) {
if (error instanceof Error && error.message.includes("User force closed")) {
spinner.stop();
logger.warn("\nTurso CLI installation cancelled by user");
p.log.warn("Turso CLI installation cancelled by user");
throw new Error("Installation cancelled");
}
spinner.fail("Failed to install Turso CLI");
spinner.stop("Failed to install Turso CLI");
throw error;
}
}
@@ -62,7 +58,6 @@ async function createTursoDatabase(dbName: string): Promise<TursoConfig> {
}
const { stdout: dbUrl } = await $`turso db show ${dbName} --url`;
const { stdout: authToken } = await $`turso db tokens create ${dbName}`;
return {
@@ -83,27 +78,28 @@ TURSO_AUTH_TOKEN=`;
}
function displayManualSetupInstructions() {
logger.info("\n📝 Manual Turso Setup Instructions:");
logger.info("1. Visit https://turso.tech and create an account");
logger.info("2. Create a new database from the dashboard");
logger.info("3. Get your database URL and authentication token");
logger.info(
p.log.info("📝 Manual Turso Setup Instructions:");
p.log.info("1. Visit https://turso.tech and create an account");
p.log.info("2. Create a new database from the dashboard");
p.log.info("3. Get your database URL and authentication token");
p.log.info(
"4. Add these credentials to the .env file in packages/server/.env",
);
logger.info("\nThe .env file has been created with placeholder variables:");
logger.info("TURSO_DATABASE_URL=your_database_url");
logger.info("TURSO_AUTH_TOKEN=your_auth_token");
p.log.info("\nThe .env file has been created with placeholder variables:");
p.log.info("TURSO_DATABASE_URL=your_database_url");
p.log.info("TURSO_AUTH_TOKEN=your_auth_token");
}
export async function setupTurso(projectDir: string) {
const spinner = ora();
p.intro("Setting up Turso...");
const platform = os.platform();
const isMac = platform === "darwin";
const canInstallCLI = platform !== "win32";
try {
if (!canInstallCLI) {
logger.warn("\nAutomatic Turso setup is not supported on Windows.");
p.log.warn("Automatic Turso setup is not supported on Windows.");
await writeEnvFile(projectDir);
displayManualSetupInstructions();
return;
@@ -112,78 +108,68 @@ export async function setupTurso(projectDir: string) {
const isCliInstalled = await isTursoInstalled();
if (!isCliInstalled) {
const shouldInstall = await confirm({
message: chalk.blue.bold("🔧 Would you like to install Turso CLI?"),
default: true,
}).catch((error) => {
spinner.stop();
throw error;
const shouldInstall = await p.confirm({
message: "Would you like to install Turso CLI?",
});
if (p.isCancel(shouldInstall)) {
p.cancel("Operation cancelled");
process.exit(0);
}
if (!shouldInstall) {
await writeEnvFile(projectDir);
displayManualSetupInstructions();
return;
}
await installTursoCLI(isMac, spinner);
await installTursoCLI(isMac);
}
const isLoggedIn = await isTursoLoggedIn();
if (!isLoggedIn) {
await loginToTurso(spinner);
await loginToTurso();
}
const defaultDbName = path.basename(projectDir);
let dbName = await input({
message: chalk.blue.bold("💾 Enter database name:"),
default: defaultDbName,
}).catch((error) => {
spinner.stop();
throw error;
});
let success = false;
let dbName = "";
let suggestedName = path.basename(projectDir);
while (!success) {
const dbNameResponse = await p.text({
message: "Enter database name:",
defaultValue: suggestedName,
});
if (p.isCancel(dbNameResponse)) {
p.cancel("Operation cancelled");
process.exit(0);
}
dbName = dbNameResponse as string;
const spinner = p.spinner();
try {
console.log();
spinner.start(`Creating Turso database "${dbName}"...`);
const config = await createTursoDatabase(dbName);
await writeEnvFile(projectDir, config);
spinner.succeed("Turso database configured successfully!");
spinner.stop("Turso database configured successfully!");
success = true;
} catch (error) {
if (error instanceof Error && error.message === "DATABASE_EXISTS") {
spinner.warn(`Database "${dbName}" already exists`);
dbName = await input({
message: "Please enter a different database name:",
default: `${dbName}-${Math.floor(Math.random() * 1000)}`,
}).catch((error) => {
spinner.stop();
throw error;
});
spinner.stop(`Database "${dbName}" already exists`);
suggestedName = `${dbName}-${Math.floor(Math.random() * 1000)}`;
} else {
throw error;
}
}
}
p.outro("Turso setup completed successfully!");
} catch (error) {
spinner.stop();
if (
error instanceof Error &&
(error.name === "ExitPromptError" ||
error.message.includes("User force closed"))
) {
logger.warn("\nTurso setup cancelled");
await writeEnvFile(projectDir);
displayManualSetupInstructions();
return;
}
logger.error("Error during Turso setup:", error);
p.log.error(`Error during Turso setup: ${error}`);
await writeEnvFile(projectDir);
displayManualSetupInstructions();
p.outro("Setup completed with manual configuration required.");
}
}