| 1 | import { consola } from "consola"; |
| 2 | import { execa } from "execa"; |
| 3 | import pc from "picocolors"; |
| 4 | import yoctoSpinner from "yocto-spinner"; |
| 5 | import { tryCatch } from "@/utils/try-catch"; |
| 6 | |
| 7 | export async function initializeGit( |
| 8 | projectPath: string, |
| 9 | skipConfirmation?: boolean, |
| 10 | ): Promise<boolean> { |
| 11 | if (!skipConfirmation) { |
| 12 | const { data: gitResponse, error } = await tryCatch( |
| 13 | consola.prompt("Initialize a git repository?", { |
| 14 | type: "confirm", |
| 15 | initial: true, |
| 16 | cancel: "reject", |
| 17 | }), |
| 18 | ); |
| 19 | |
| 20 | if (error) { |
| 21 | console.log(pc.yellow("Project creation cancelled.")); |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | if (!gitResponse) { |
| 26 | return false; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | const spinner = yoctoSpinner({ |
| 31 | text: "Initializing git repository...", |
| 32 | }).start(); |
| 33 | |
| 34 | try { |
| 35 | await execa("git", ["init"], { cwd: projectPath }); |
| 36 | spinner.success("Git repository initialized"); |
| 37 | return true; |
| 38 | } catch (err: unknown) { |
| 39 | spinner.error("Failed to initialize git repository. Is git installed?"); |
| 40 | if (err instanceof Error) { |
| 41 | consola.error(pc.red("Git error:"), err.message); |
| 42 | } else { |
| 43 | consola.error(pc.red("Git error: Unknown error")); |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | } |