|
| 1 | +/* libs */ |
| 2 | +import { $ } from "bun"; |
| 3 | +import { oraPromise } from "ora"; |
| 4 | +import { mkdir } from "node:fs/promises"; |
| 5 | +import inquirer from "inquirer"; |
| 6 | +import path from "node:path"; |
| 7 | +import chalk from "chalk"; |
| 8 | + |
| 9 | +/* constants */ |
| 10 | +import { DEV_MODE } from "@/constants"; |
| 11 | + |
| 12 | +/* extras */ |
| 13 | +import { existsAsync } from "@/utils/extras"; |
| 14 | + |
| 15 | +/* types */ |
| 16 | +import type { ShellOutput } from "bun"; |
| 17 | +import type { T_PackageManager } from "@/@types/index"; |
| 18 | + |
| 19 | +// =============================== |
| 20 | + |
| 21 | +/** |
| 22 | + * Principal Class for interacting with the task-master tool |
| 23 | + */ |
| 24 | +export class TaskMaster { |
| 25 | + constructor() { |
| 26 | + console.log("TaskMaster initialized !"); |
| 27 | + } |
| 28 | + |
| 29 | + // ============================================== |
| 30 | + // Method for Installation and Configuration |
| 31 | + // ============================================== |
| 32 | + |
| 33 | + /** |
| 34 | + * @description - Installs or updates task-master AI using the chosen package manager |
| 35 | + */ |
| 36 | + async installAsync(): Promise<void> { |
| 37 | + const packageManagerChoices: T_PackageManager[] = ["npm", "pnpm", "bun"]; |
| 38 | + |
| 39 | + const { packageManagerChoice } = await inquirer.prompt<{ |
| 40 | + packageManagerChoice: T_PackageManager; |
| 41 | + }>({ |
| 42 | + type: "list", |
| 43 | + name: "packageManagerChoice", |
| 44 | + message: "Choose your package manager for installation:", |
| 45 | + loop: false, |
| 46 | + pageSize: 4, |
| 47 | + choices: packageManagerChoices, |
| 48 | + default: "npm", |
| 49 | + }); |
| 50 | + |
| 51 | + const oraOptions = { |
| 52 | + text: `Installing task-master AI with ${chalk.bold( |
| 53 | + packageManagerChoice, |
| 54 | + )} ...`, |
| 55 | + successText: "Task-master AI installed successfully !", |
| 56 | + failText: `Failed to install task-master AI with ${chalk.bold( |
| 57 | + packageManagerChoice, |
| 58 | + )}.`, |
| 59 | + }; |
| 60 | + |
| 61 | + await oraPromise( |
| 62 | + (async () => { |
| 63 | + let cmd: string[] = []; |
| 64 | + let args: string[] = []; |
| 65 | + |
| 66 | + switch (packageManagerChoice) { |
| 67 | + case "npm": |
| 68 | + cmd = ["npm"]; |
| 69 | + args = ["install", "-g", "task-master-ai@latest"]; |
| 70 | + break; |
| 71 | + case "pnpm": |
| 72 | + cmd = ["pnpm"]; |
| 73 | + args = ["add", "-g", "task-master-ai@latest"]; |
| 74 | + break; |
| 75 | + case "bun": |
| 76 | + cmd = [process.execPath]; |
| 77 | + args = ["add", "-g", "task-master-ai@latest"]; |
| 78 | + break; |
| 79 | + } |
| 80 | + |
| 81 | + const proc = Bun.spawn([...cmd, ...args], { |
| 82 | + stdout: "inherit", |
| 83 | + stderr: "inherit", |
| 84 | + env: process.env, |
| 85 | + }); |
| 86 | + |
| 87 | + await proc.exited; |
| 88 | + if (proc.exitCode !== 0) { |
| 89 | + throw new Error( |
| 90 | + `Installation failed with exit code ${proc.exitCode}`, |
| 91 | + ); |
| 92 | + } |
| 93 | + })(), |
| 94 | + oraOptions, |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @description - Initializes the task-master AI by creating a PRD file |
| 100 | + */ |
| 101 | + async initAsync(): Promise<ShellOutput> { |
| 102 | + const prdFile = DEV_MODE ? "PRD-test.md" : "PRD.md"; |
| 103 | + const prdDestination = DEV_MODE ? "tests" : "docs"; |
| 104 | + const prdFilePath = path.join(prdDestination, prdFile); |
| 105 | + |
| 106 | + if (await existsAsync(prdFilePath)) { |
| 107 | + console.log(chalk.yellow(`PRD file already exists at ${prdFilePath}.`)); |
| 108 | + return await $`task-master init`; |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + await oraPromise( |
| 113 | + async () => { |
| 114 | + await mkdir(prdDestination, { recursive: true }); |
| 115 | + const file = Bun.file(prdFilePath); |
| 116 | + await Bun.write(file, ""); |
| 117 | + }, |
| 118 | + { |
| 119 | + text: "Generating PRD file ...", |
| 120 | + successText: `PRD file created at ${prdFilePath}`, |
| 121 | + failText: (error) => `Failed to create PRD file: ${error.message}`, |
| 122 | + }, |
| 123 | + ); |
| 124 | + } catch (error: unknown) { |
| 125 | + const errorMessage = |
| 126 | + error instanceof Error ? error.message : String(error); |
| 127 | + console.error(chalk.red(`Error creating PRD file: ${errorMessage}`)); |
| 128 | + throw error; |
| 129 | + } |
| 130 | + |
| 131 | + return await $`task-master init`; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @description - Configures the AI models for task-master AI |
| 136 | + */ |
| 137 | + async configAsync(): Promise<ShellOutput> { |
| 138 | + return await $`task-master models --setup`; |
| 139 | + } |
| 140 | +} |
0 commit comments