mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
refractor: migrate inquirer to @clack/prompts
This commit is contained in:
@@ -1,164 +1,96 @@
|
||||
import path from "node:path";
|
||||
import { confirm, select } from "@inquirer/prompts";
|
||||
import { cancel, confirm, isCancel, log, spinner, tasks } from "@clack/prompts";
|
||||
import chalk from "chalk";
|
||||
import { $ } from "execa";
|
||||
import fs from "fs-extra";
|
||||
import ora from "ora";
|
||||
import { DEFAULT_CONFIG } from "../consts";
|
||||
import type { PackageManager, ProjectConfig } from "../types";
|
||||
import { getUserPkgManager } from "../utils/get-package-manager";
|
||||
import { logger } from "../utils/logger";
|
||||
import type { ProjectConfig } from "../types";
|
||||
import { setupTurso } from "./db-setup";
|
||||
|
||||
export async function createProject(options: ProjectConfig) {
|
||||
const spinner = ora("Creating project directory...").start();
|
||||
const s = spinner();
|
||||
const projectDir = path.resolve(process.cwd(), options.projectName);
|
||||
let shouldInstallDeps = false;
|
||||
|
||||
try {
|
||||
await fs.ensureDir(projectDir);
|
||||
spinner.succeed();
|
||||
console.log();
|
||||
await tasks([
|
||||
{
|
||||
title: "Creating project directory",
|
||||
task: async () => {
|
||||
await fs.ensureDir(projectDir);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: "Cloning template repository",
|
||||
task: async () => {
|
||||
try {
|
||||
await $`npx degit AmanVarshney01/Better-T-Stack ${projectDir}`;
|
||||
} catch (error) {
|
||||
log.error("Failed to clone template repository");
|
||||
if (error instanceof Error) {
|
||||
log.error(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
title: "Initializing git repository",
|
||||
task: async () => {
|
||||
if (options.git) {
|
||||
await $`git init ${projectDir}`;
|
||||
}
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
spinner.start("Cloning template repository...");
|
||||
await $`npx degit https://github.com/AmanVarshney01/Better-T-Stack.git ${projectDir}`;
|
||||
spinner.succeed();
|
||||
console.log();
|
||||
|
||||
let shouldInitGit = options.git;
|
||||
|
||||
if (!options.yes && shouldInitGit) {
|
||||
shouldInitGit = await confirm({
|
||||
message: chalk.blue.bold("🔄 Initialize a git repository?"),
|
||||
default: true,
|
||||
}).catch((error) => {
|
||||
spinner.stop();
|
||||
console.log();
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
if (shouldInitGit) {
|
||||
spinner.start("Initializing git repository...");
|
||||
await $`git init ${projectDir}`;
|
||||
spinner.succeed();
|
||||
}
|
||||
|
||||
const detectedPackageManager = getUserPkgManager();
|
||||
let packageManager = options.packageManager ?? detectedPackageManager;
|
||||
|
||||
if (!options.yes) {
|
||||
const useDetectedPackageManager = await confirm({
|
||||
message: chalk.blue.bold(
|
||||
`📦 Use detected package manager (${chalk.cyan(
|
||||
detectedPackageManager,
|
||||
)})?`,
|
||||
),
|
||||
default: true,
|
||||
}).catch((error) => {
|
||||
spinner.stop();
|
||||
throw error;
|
||||
});
|
||||
|
||||
if (!useDetectedPackageManager) {
|
||||
console.log();
|
||||
packageManager = await select<PackageManager>({
|
||||
message: chalk.blue.bold("📦 Select package manager:"),
|
||||
choices: [
|
||||
{
|
||||
value: "npm",
|
||||
name: chalk.yellow("npm"),
|
||||
description: chalk.dim("Node Package Manager"),
|
||||
},
|
||||
{
|
||||
value: "yarn",
|
||||
name: chalk.blue("yarn"),
|
||||
description: chalk.dim(
|
||||
"Fast, reliable, and secure dependency management",
|
||||
),
|
||||
},
|
||||
{
|
||||
value: "pnpm",
|
||||
name: chalk.magenta("pnpm"),
|
||||
description: chalk.dim(
|
||||
"Fast, disk space efficient package manager",
|
||||
),
|
||||
},
|
||||
{
|
||||
value: "bun",
|
||||
name: chalk.cyan("bun"),
|
||||
description: chalk.dim("All-in-one JavaScript runtime & toolkit"),
|
||||
},
|
||||
],
|
||||
}).catch((error) => {
|
||||
spinner.stop();
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const installDeps = await confirm({
|
||||
message: chalk.blue.bold(
|
||||
`📦 Install dependencies using ${chalk.cyan(packageManager)}?`,
|
||||
),
|
||||
default: true,
|
||||
}).catch((error) => {
|
||||
spinner.stop();
|
||||
throw error;
|
||||
const installDepsResponse = await confirm({
|
||||
message: `📦 Install dependencies using ${options.packageManager}?`,
|
||||
});
|
||||
|
||||
console.log();
|
||||
if (isCancel(installDepsResponse)) {
|
||||
cancel("Operation cancelled");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (installDeps) {
|
||||
spinner.start(`📦 Installing dependencies using ${packageManager}...`);
|
||||
switch (packageManager ?? DEFAULT_CONFIG.packageManager) {
|
||||
case "npm":
|
||||
await $`cd ${projectDir} && npm install`;
|
||||
break;
|
||||
case "yarn":
|
||||
await $`cd ${projectDir} && yarn install`;
|
||||
break;
|
||||
case "pnpm":
|
||||
await $`cd ${projectDir} && pnpm install`;
|
||||
break;
|
||||
case "bun":
|
||||
await $`cd ${projectDir} && bun install`;
|
||||
break;
|
||||
default:
|
||||
throw new Error("Unsupported package manager");
|
||||
shouldInstallDeps = installDepsResponse;
|
||||
|
||||
if (shouldInstallDeps) {
|
||||
s.start(`Installing dependencies using ${options.packageManager}...`);
|
||||
try {
|
||||
await $({
|
||||
cwd: projectDir,
|
||||
stdio: "inherit",
|
||||
})`${options.packageManager} install`;
|
||||
s.stop("Dependencies installed successfully");
|
||||
} catch (error) {
|
||||
s.stop("Failed to install dependencies");
|
||||
if (error instanceof Error) {
|
||||
log.error(`Installation error: ${error.message}`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
spinner.succeed();
|
||||
console.log();
|
||||
}
|
||||
|
||||
if (options.database === "libsql") {
|
||||
await setupTurso(projectDir);
|
||||
}
|
||||
|
||||
logger.success("\n✨ Project created successfully!\n");
|
||||
logger.info("Next steps:");
|
||||
logger.info(` cd ${options.projectName}`);
|
||||
if (!installDeps) {
|
||||
logger.info(` ${packageManager} install`);
|
||||
log.success("✨ Project created successfully!\n");
|
||||
log.info(chalk.dim("Next steps:"));
|
||||
log.info(` cd ${options.projectName}`);
|
||||
if (!shouldInstallDeps) {
|
||||
log.info(` ${options.packageManager} install`);
|
||||
}
|
||||
logger.info(
|
||||
` ${packageManager === "npm" ? "npm run" : packageManager} dev`,
|
||||
log.info(
|
||||
` ${
|
||||
options.packageManager === "npm" ? "npm run" : options.packageManager
|
||||
} dev`,
|
||||
);
|
||||
} catch (error) {
|
||||
spinner.stop();
|
||||
|
||||
if (
|
||||
error instanceof Error &&
|
||||
(error.name === "ExitPromptError" ||
|
||||
error.message.includes("User force closed"))
|
||||
) {
|
||||
console.log("\n");
|
||||
logger.warn("Operation cancelled");
|
||||
process.exit(0);
|
||||
return;
|
||||
s.stop("Failed");
|
||||
if (error instanceof Error) {
|
||||
log.error(`Error during project creation: ${error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
spinner.fail("Failed to create project");
|
||||
logger.error("Error during project creation:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user