mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
fix turso cli issue
This commit is contained in:
@@ -5,32 +5,21 @@ import { execa } 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";
|
||||
|
||||
async function isTursoInstalled() {
|
||||
async function loginToTurso(spinner: Ora) {
|
||||
try {
|
||||
await execa("turso", ["--version"]);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function isTursoLoggedIn() {
|
||||
try {
|
||||
await execa("turso", ["auth", "whoami"]);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
spinner.start("Logging in to Turso...");
|
||||
await execa("turso", ["auth", "login"]);
|
||||
spinner.succeed("Logged in to Turso successfully!");
|
||||
} catch (error) {
|
||||
spinner.fail("Failed to log in to Turso");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function installTursoCLI(isMac: boolean, spinner: Ora) {
|
||||
try {
|
||||
if (await isTursoLoggedIn()) {
|
||||
spinner.succeed("Turso CLI already logged in!");
|
||||
return;
|
||||
}
|
||||
|
||||
spinner.start("Installing Turso CLI...");
|
||||
|
||||
if (isMac) {
|
||||
@@ -44,10 +33,6 @@ async function installTursoCLI(isMac: boolean, spinner: Ora) {
|
||||
}
|
||||
|
||||
spinner.succeed("Turso CLI installed successfully!");
|
||||
|
||||
spinner.start("Logging in to Turso...");
|
||||
await execa("turso", ["auth", "login"]);
|
||||
spinner.succeed("Logged in to Turso!");
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes("User force closed")) {
|
||||
spinner.stop();
|
||||
@@ -55,92 +40,100 @@ async function installTursoCLI(isMac: boolean, spinner: Ora) {
|
||||
logger.warn("Turso CLI installation cancelled by user");
|
||||
throw error;
|
||||
}
|
||||
logger.error("Error during Turso CLI installation:", error);
|
||||
spinner.fail(
|
||||
"Failed to install Turso CLI. Proceeding with manual setup...",
|
||||
);
|
||||
|
||||
spinner.fail("Failed to install Turso CLI");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function createTursoDatabase(projectDir: string, spinner: Ora) {
|
||||
try {
|
||||
const defaultDbName = path.basename(projectDir);
|
||||
const dbName = await input({
|
||||
message: `Enter database name (default: ${defaultDbName}):`,
|
||||
default: defaultDbName,
|
||||
});
|
||||
|
||||
spinner.start(`Creating Turso database "${dbName}"...`);
|
||||
await execa("turso", ["db", "create", dbName]);
|
||||
|
||||
const { stdout: dbUrl } = await execa("turso", [
|
||||
"db",
|
||||
"show",
|
||||
dbName,
|
||||
"--url",
|
||||
]);
|
||||
const { stdout: authToken } = await execa("turso", [
|
||||
"db",
|
||||
"tokens",
|
||||
"create",
|
||||
dbName,
|
||||
]);
|
||||
|
||||
const envPath = path.join(projectDir, "packages/server", ".env");
|
||||
const envContent = `TURSO_DATABASE_URL="${dbUrl.trim()}"
|
||||
TURSO_AUTH_TOKEN="${authToken.trim()}"`;
|
||||
|
||||
await fs.writeFile(envPath, envContent);
|
||||
spinner.succeed("Turso database configured successfully!");
|
||||
} catch (error) {
|
||||
spinner.fail("Failed to create Turso database");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function setupManualConfig(projectDir: string) {
|
||||
const envPath = path.join(projectDir, "packages/server", ".env");
|
||||
const envContent = `TURSO_DATABASE_URL=
|
||||
TURSO_AUTH_TOKEN=`;
|
||||
|
||||
await fs.writeFile(envPath, envContent);
|
||||
|
||||
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("4. Add these credentials to the .env file in your project root");
|
||||
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");
|
||||
}
|
||||
|
||||
export async function setupTurso(projectDir: string) {
|
||||
const spinner = ora();
|
||||
const platform = os.platform();
|
||||
|
||||
const isMac = platform === "darwin";
|
||||
let canInstallCLI = platform !== "win32";
|
||||
let installTurso = true;
|
||||
const canInstallCLI = platform !== "win32";
|
||||
|
||||
const isCliInstalled = await isTursoInstalled();
|
||||
|
||||
if (canInstallCLI && !isCliInstalled) {
|
||||
installTurso = await confirm({
|
||||
message: "Would you like to install Turso CLI?",
|
||||
default: true,
|
||||
});
|
||||
if (!canInstallCLI) {
|
||||
await setupManualConfig(projectDir);
|
||||
return;
|
||||
}
|
||||
|
||||
canInstallCLI = canInstallCLI && installTurso;
|
||||
try {
|
||||
const isCliInstalled = await isTursoInstalled();
|
||||
|
||||
if (canInstallCLI) {
|
||||
try {
|
||||
await installTursoCLI(isMac, spinner);
|
||||
|
||||
const defaultDbName = path.basename(projectDir);
|
||||
const dbName = await input({
|
||||
message: `Enter database name (default: ${defaultDbName}):`,
|
||||
default: defaultDbName,
|
||||
if (!isCliInstalled) {
|
||||
const shouldInstall = await confirm({
|
||||
message: "Would you like to install Turso CLI?",
|
||||
default: true,
|
||||
});
|
||||
|
||||
spinner.start(`Creating Turso database "${dbName}"...`);
|
||||
await execa("turso", ["db", "create", dbName]);
|
||||
if (!shouldInstall) {
|
||||
await setupManualConfig(projectDir);
|
||||
return;
|
||||
}
|
||||
|
||||
const { stdout: dbUrl } = await execa("turso", [
|
||||
"db",
|
||||
"show",
|
||||
dbName,
|
||||
"--url",
|
||||
]);
|
||||
const { stdout: authToken } = await execa("turso", [
|
||||
"db",
|
||||
"tokens",
|
||||
"create",
|
||||
dbName,
|
||||
]);
|
||||
|
||||
const envPath = path.join(projectDir, "packages/server", ".env");
|
||||
const envContent = `TURSO_DATABASE_URL="${dbUrl.trim()}"
|
||||
TURSO_AUTH_TOKEN="${authToken.trim()}"`;
|
||||
|
||||
await fs.writeFile(envPath, envContent);
|
||||
spinner.succeed("Turso database configured successfully!");
|
||||
return;
|
||||
} catch (error) {
|
||||
logger.error("Error during Turso database creation:", error);
|
||||
spinner.fail(
|
||||
"Failed to install Turso CLI. Proceeding with manual setup...",
|
||||
);
|
||||
installTurso = false;
|
||||
await installTursoCLI(isMac, spinner);
|
||||
}
|
||||
}
|
||||
|
||||
if (!installTurso) {
|
||||
const envPath = path.join(projectDir, "packages/server", ".env");
|
||||
const envContent = `TURSO_DATABASE_URL=
|
||||
TURSO_AUTH_TOKEN=`;
|
||||
const isLoggedIn = await isTursoLoggedIn();
|
||||
if (!isLoggedIn) {
|
||||
await loginToTurso(spinner);
|
||||
}
|
||||
|
||||
await fs.writeFile(envPath, envContent);
|
||||
|
||||
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(
|
||||
"4. Add these credentials to the .env file in your project root",
|
||||
);
|
||||
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");
|
||||
await createTursoDatabase(projectDir, spinner);
|
||||
} catch (error) {
|
||||
logger.error("Error during Turso setup:", error);
|
||||
await setupManualConfig(projectDir);
|
||||
}
|
||||
}
|
||||
|
||||
20
apps/cli/src/utils/turso-cli.ts
Normal file
20
apps/cli/src/utils/turso-cli.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { execa } from "execa";
|
||||
|
||||
export async function isTursoInstalled() {
|
||||
try {
|
||||
await execa("turso", ["--version"]);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function isTursoLoggedIn() {
|
||||
try {
|
||||
const output = await execa("turso", ["auth", "whoami"]);
|
||||
console.log(output.stdout.includes("You are not logged in"));
|
||||
return !output.stdout.includes("You are not logged in");
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user