|
1 | 1 | import path from "node:path"; |
2 | | -import { confirm, select } from "@inquirer/prompts"; |
| 2 | +import { cancel, confirm, isCancel, log, spinner, tasks } from "@clack/prompts"; |
3 | 3 | import chalk from "chalk"; |
4 | 4 | import { $ } from "execa"; |
5 | 5 | import fs from "fs-extra"; |
6 | | -import ora from "ora"; |
7 | | -import { DEFAULT_CONFIG } from "../consts"; |
8 | | -import type { PackageManager, ProjectConfig } from "../types"; |
9 | | -import { getUserPkgManager } from "../utils/get-package-manager"; |
10 | | -import { logger } from "../utils/logger"; |
| 6 | +import type { ProjectConfig } from "../types"; |
11 | 7 | import { setupTurso } from "./db-setup"; |
12 | 8 |
|
13 | 9 | export async function createProject(options: ProjectConfig) { |
14 | | - const spinner = ora("Creating project directory...").start(); |
| 10 | + const s = spinner(); |
15 | 11 | const projectDir = path.resolve(process.cwd(), options.projectName); |
| 12 | + let shouldInstallDeps = false; |
16 | 13 |
|
17 | 14 | try { |
18 | | - await fs.ensureDir(projectDir); |
19 | | - spinner.succeed(); |
20 | | - console.log(); |
21 | | - |
22 | | - spinner.start("Cloning template repository..."); |
23 | | - await $`npx degit https://github.com/AmanVarshney01/Better-T-Stack.git ${projectDir}`; |
24 | | - spinner.succeed(); |
25 | | - console.log(); |
26 | | - |
27 | | - let shouldInitGit = options.git; |
28 | | - |
29 | | - if (!options.yes && shouldInitGit) { |
30 | | - shouldInitGit = await confirm({ |
31 | | - message: chalk.blue.bold("🔄 Initialize a git repository?"), |
32 | | - default: true, |
33 | | - }).catch((error) => { |
34 | | - spinner.stop(); |
35 | | - console.log(); |
36 | | - throw error; |
37 | | - }); |
38 | | - } |
| 15 | + await tasks([ |
| 16 | + { |
| 17 | + title: "Creating project directory", |
| 18 | + task: async () => { |
| 19 | + await fs.ensureDir(projectDir); |
| 20 | + }, |
| 21 | + }, |
| 22 | + { |
| 23 | + title: "Cloning template repository", |
| 24 | + task: async () => { |
| 25 | + try { |
| 26 | + await $`npx degit AmanVarshney01/Better-T-Stack ${projectDir}`; |
| 27 | + } catch (error) { |
| 28 | + log.error("Failed to clone template repository"); |
| 29 | + if (error instanceof Error) { |
| 30 | + log.error(error.message); |
| 31 | + } |
| 32 | + throw error; |
| 33 | + } |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + title: "Initializing git repository", |
| 38 | + task: async () => { |
| 39 | + if (options.git) { |
| 40 | + await $`git init ${projectDir}`; |
| 41 | + } |
| 42 | + }, |
| 43 | + }, |
| 44 | + ]); |
| 45 | + |
| 46 | + const installDepsResponse = await confirm({ |
| 47 | + message: `📦 Install dependencies using ${options.packageManager}?`, |
| 48 | + }); |
39 | 49 |
|
40 | | - if (shouldInitGit) { |
41 | | - spinner.start("Initializing git repository..."); |
42 | | - await $`git init ${projectDir}`; |
43 | | - spinner.succeed(); |
| 50 | + if (isCancel(installDepsResponse)) { |
| 51 | + cancel("Operation cancelled"); |
| 52 | + process.exit(0); |
44 | 53 | } |
45 | 54 |
|
46 | | - const detectedPackageManager = getUserPkgManager(); |
47 | | - let packageManager = options.packageManager ?? detectedPackageManager; |
48 | | - |
49 | | - if (!options.yes) { |
50 | | - const useDetectedPackageManager = await confirm({ |
51 | | - message: chalk.blue.bold( |
52 | | - `📦 Use detected package manager (${chalk.cyan( |
53 | | - detectedPackageManager, |
54 | | - )})?`, |
55 | | - ), |
56 | | - default: true, |
57 | | - }).catch((error) => { |
58 | | - spinner.stop(); |
| 55 | + shouldInstallDeps = installDepsResponse; |
| 56 | + |
| 57 | + if (shouldInstallDeps) { |
| 58 | + s.start(`Installing dependencies using ${options.packageManager}...`); |
| 59 | + try { |
| 60 | + await $({ |
| 61 | + cwd: projectDir, |
| 62 | + stdio: "inherit", |
| 63 | + })`${options.packageManager} install`; |
| 64 | + s.stop("Dependencies installed successfully"); |
| 65 | + } catch (error) { |
| 66 | + s.stop("Failed to install dependencies"); |
| 67 | + if (error instanceof Error) { |
| 68 | + log.error(`Installation error: ${error.message}`); |
| 69 | + } |
59 | 70 | throw error; |
60 | | - }); |
61 | | - |
62 | | - if (!useDetectedPackageManager) { |
63 | | - console.log(); |
64 | | - packageManager = await select<PackageManager>({ |
65 | | - message: chalk.blue.bold("📦 Select package manager:"), |
66 | | - choices: [ |
67 | | - { |
68 | | - value: "npm", |
69 | | - name: chalk.yellow("npm"), |
70 | | - description: chalk.dim("Node Package Manager"), |
71 | | - }, |
72 | | - { |
73 | | - value: "yarn", |
74 | | - name: chalk.blue("yarn"), |
75 | | - description: chalk.dim( |
76 | | - "Fast, reliable, and secure dependency management", |
77 | | - ), |
78 | | - }, |
79 | | - { |
80 | | - value: "pnpm", |
81 | | - name: chalk.magenta("pnpm"), |
82 | | - description: chalk.dim( |
83 | | - "Fast, disk space efficient package manager", |
84 | | - ), |
85 | | - }, |
86 | | - { |
87 | | - value: "bun", |
88 | | - name: chalk.cyan("bun"), |
89 | | - description: chalk.dim("All-in-one JavaScript runtime & toolkit"), |
90 | | - }, |
91 | | - ], |
92 | | - }).catch((error) => { |
93 | | - spinner.stop(); |
94 | | - throw error; |
95 | | - }); |
96 | 71 | } |
97 | 72 | } |
98 | 73 |
|
99 | | - const installDeps = await confirm({ |
100 | | - message: chalk.blue.bold( |
101 | | - `📦 Install dependencies using ${chalk.cyan(packageManager)}?`, |
102 | | - ), |
103 | | - default: true, |
104 | | - }).catch((error) => { |
105 | | - spinner.stop(); |
106 | | - throw error; |
107 | | - }); |
108 | | - |
109 | | - console.log(); |
110 | | - |
111 | | - if (installDeps) { |
112 | | - spinner.start(`📦 Installing dependencies using ${packageManager}...`); |
113 | | - switch (packageManager ?? DEFAULT_CONFIG.packageManager) { |
114 | | - case "npm": |
115 | | - await $`cd ${projectDir} && npm install`; |
116 | | - break; |
117 | | - case "yarn": |
118 | | - await $`cd ${projectDir} && yarn install`; |
119 | | - break; |
120 | | - case "pnpm": |
121 | | - await $`cd ${projectDir} && pnpm install`; |
122 | | - break; |
123 | | - case "bun": |
124 | | - await $`cd ${projectDir} && bun install`; |
125 | | - break; |
126 | | - default: |
127 | | - throw new Error("Unsupported package manager"); |
128 | | - } |
129 | | - spinner.succeed(); |
130 | | - console.log(); |
131 | | - } |
132 | | - |
133 | 74 | if (options.database === "libsql") { |
134 | 75 | await setupTurso(projectDir); |
135 | 76 | } |
136 | 77 |
|
137 | | - logger.success("\n✨ Project created successfully!\n"); |
138 | | - logger.info("Next steps:"); |
139 | | - logger.info(` cd ${options.projectName}`); |
140 | | - if (!installDeps) { |
141 | | - logger.info(` ${packageManager} install`); |
| 78 | + log.success("✨ Project created successfully!\n"); |
| 79 | + log.info(chalk.dim("Next steps:")); |
| 80 | + log.info(` cd ${options.projectName}`); |
| 81 | + if (!shouldInstallDeps) { |
| 82 | + log.info(` ${options.packageManager} install`); |
142 | 83 | } |
143 | | - logger.info( |
144 | | - ` ${packageManager === "npm" ? "npm run" : packageManager} dev`, |
| 84 | + log.info( |
| 85 | + ` ${ |
| 86 | + options.packageManager === "npm" ? "npm run" : options.packageManager |
| 87 | + } dev`, |
145 | 88 | ); |
146 | 89 | } catch (error) { |
147 | | - spinner.stop(); |
148 | | - |
149 | | - if ( |
150 | | - error instanceof Error && |
151 | | - (error.name === "ExitPromptError" || |
152 | | - error.message.includes("User force closed")) |
153 | | - ) { |
154 | | - console.log("\n"); |
155 | | - logger.warn("Operation cancelled"); |
156 | | - process.exit(0); |
157 | | - return; |
| 90 | + s.stop("Failed"); |
| 91 | + if (error instanceof Error) { |
| 92 | + log.error(`Error during project creation: ${error.message}`); |
| 93 | + process.exit(1); |
158 | 94 | } |
159 | | - |
160 | | - spinner.fail("Failed to create project"); |
161 | | - logger.error("Error during project creation:", error); |
162 | | - process.exit(1); |
163 | 95 | } |
164 | 96 | } |
0 commit comments