mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
colorize the prompts
This commit is contained in:
166
apps/cli/src/helpers/create-project.ts
Normal file
166
apps/cli/src/helpers/create-project.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
import path from "node:path";
|
||||
import { confirm, select } from "@inquirer/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 { setupTurso } from "./db-setup";
|
||||
|
||||
export async function createProject(options: ProjectConfig) {
|
||||
const spinner = ora("Creating project directory...").start();
|
||||
const projectDir = path.resolve(process.cwd(), options.projectName);
|
||||
|
||||
try {
|
||||
await fs.ensureDir(projectDir);
|
||||
spinner.succeed();
|
||||
console.log();
|
||||
|
||||
spinner.start("Cloning template repository...");
|
||||
await $`npx degit https://github.com/AmanVarshney01/Better-T-Stack.git ${projectDir}`;
|
||||
spinner.succeed();
|
||||
console.log();
|
||||
|
||||
const initGit = await confirm({
|
||||
message: chalk.blue.bold("🔄 Initialize a git repository?"),
|
||||
default: true,
|
||||
}).catch((error) => {
|
||||
spinner.stop();
|
||||
console.log();
|
||||
throw error;
|
||||
});
|
||||
|
||||
if (initGit) {
|
||||
spinner.start("Initializing git repository...");
|
||||
await $`git init ${projectDir}`;
|
||||
spinner.succeed();
|
||||
console.log();
|
||||
}
|
||||
|
||||
let packageManager = options.packageManager;
|
||||
|
||||
if (!packageManager) {
|
||||
const detectedPackageManager = getUserPkgManager();
|
||||
|
||||
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) {
|
||||
packageManager = detectedPackageManager;
|
||||
} else {
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
|
||||
const installDeps = await confirm({
|
||||
message: chalk.blue.bold(
|
||||
`📦 Install dependencies using ${chalk.cyan(packageManager)}?`,
|
||||
),
|
||||
default: true,
|
||||
}).catch((error) => {
|
||||
spinner.stop();
|
||||
throw error;
|
||||
});
|
||||
|
||||
console.log();
|
||||
|
||||
if (installDeps) {
|
||||
spinner.start(`📦 Installing dependencies using ${packageManager}...`);
|
||||
switch (packageManager ?? DEFAULT_CONFIG.packageManager) {
|
||||
case "npm":
|
||||
await $`npm install ${projectDir}`;
|
||||
break;
|
||||
case "yarn":
|
||||
await $`yarn install ${projectDir}`;
|
||||
break;
|
||||
case "pnpm":
|
||||
await $`pnpm install ${projectDir}`;
|
||||
break;
|
||||
case "bun":
|
||||
await $`bun install ${projectDir}`;
|
||||
break;
|
||||
default:
|
||||
throw new Error("Unsupported package manager");
|
||||
}
|
||||
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`);
|
||||
}
|
||||
logger.info(
|
||||
` ${packageManager === "npm" ? "npm run" : 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;
|
||||
}
|
||||
|
||||
spinner.fail("Failed to create project");
|
||||
logger.error("Error during project creation:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { confirm, input } from "@inquirer/prompts";
|
||||
import chalk from "chalk";
|
||||
import { $ } from "execa";
|
||||
import fs from "fs-extra";
|
||||
import ora, { type Ora } from "ora";
|
||||
@@ -17,6 +18,7 @@ async function loginToTurso(spinner: Ora) {
|
||||
spinner.start("Logging in to Turso...");
|
||||
await $`turso auth login`;
|
||||
spinner.succeed("Logged in to Turso successfully!");
|
||||
console.log();
|
||||
} catch (error) {
|
||||
spinner.fail("Failed to log in to Turso");
|
||||
throw error;
|
||||
@@ -25,6 +27,7 @@ async function loginToTurso(spinner: Ora) {
|
||||
|
||||
async function installTursoCLI(isMac: boolean, spinner: Ora) {
|
||||
try {
|
||||
console.log();
|
||||
spinner.start("Installing Turso CLI...");
|
||||
|
||||
if (isMac) {
|
||||
@@ -36,6 +39,7 @@ async function installTursoCLI(isMac: boolean, spinner: Ora) {
|
||||
}
|
||||
|
||||
spinner.succeed("Turso CLI installed successfully!");
|
||||
console.log();
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes("User force closed")) {
|
||||
spinner.stop();
|
||||
@@ -108,7 +112,7 @@ export async function setupTurso(projectDir: string) {
|
||||
|
||||
if (!isCliInstalled) {
|
||||
const shouldInstall = await confirm({
|
||||
message: "Would you like to install Turso CLI?",
|
||||
message: chalk.blue.bold("🔧 Would you like to install Turso CLI?"),
|
||||
default: true,
|
||||
}).catch((error) => {
|
||||
spinner.stop();
|
||||
@@ -130,8 +134,9 @@ export async function setupTurso(projectDir: string) {
|
||||
}
|
||||
|
||||
const defaultDbName = path.basename(projectDir);
|
||||
|
||||
let dbName = await input({
|
||||
message: `Enter database name (default: ${defaultDbName}):`,
|
||||
message: chalk.blue.bold("💾 Enter database name:"),
|
||||
default: defaultDbName,
|
||||
}).catch((error) => {
|
||||
spinner.stop();
|
||||
@@ -141,6 +146,7 @@ export async function setupTurso(projectDir: string) {
|
||||
let success = false;
|
||||
while (!success) {
|
||||
try {
|
||||
console.log();
|
||||
spinner.start(`Creating Turso database "${dbName}"...`);
|
||||
const config = await createTursoDatabase(dbName);
|
||||
await writeEnvFile(projectDir, config);
|
||||
|
||||
Reference in New Issue
Block a user