| 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 | async function getPackageManager(): Promise<"bun"> { |
| 8 | const { error } = await tryCatch(execa("bun", ["--version"])); |
| 9 | |
| 10 | if (error) { |
| 11 | consola.error(new Error("Bun is not installed.")); |
| 12 | consola.warn("Please install bun from https://bun.sh/"); |
| 13 | process.exit(1); |
| 14 | } |
| 15 | |
| 16 | return "bun"; |
| 17 | } |
| 18 | |
| 19 | export async function installDependencies( |
| 20 | projectPath: string, |
| 21 | skipConfirmation?: boolean, |
| 22 | ): Promise<boolean> { |
| 23 | if (!skipConfirmation) { |
| 24 | const { data: depsResponse, error } = await tryCatch( |
| 25 | consola.prompt("Install dependencies?", { |
| 26 | type: "confirm", |
| 27 | initial: true, |
| 28 | cancel: "reject", |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | if (error) { |
| 33 | console.log(pc.yellow("Project creation cancelled.")); |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | if (!depsResponse) { |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const packageManager = await getPackageManager(); |
| 43 | |
| 44 | const spinner = yoctoSpinner({ |
| 45 | text: `Installing dependencies with ${packageManager}...`, |
| 46 | }).start(); |
| 47 | |
| 48 | try { |
| 49 | await execa(packageManager, ["install"], { cwd: projectPath }); |
| 50 | spinner.success(`Dependencies installed with ${packageManager}`); |
| 51 | return true; |
| 52 | } catch (_err) { |
| 53 | spinner.error("Failed to install dependencies."); |
| 54 | console.log( |
| 55 | pc.yellow( |
| 56 | "You can install them manually after navigating to the project directory.", |
| 57 | ), |
| 58 | ); |
| 59 | return false; |
| 60 | } |
| 61 | } |