|
| 1 | +import { execSync } from "node:child_process"; |
| 2 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 3 | +import { homedir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { INSTALL_SKILL_URL } from "../constants.js"; |
| 6 | +import { highlighter } from "./highlighter.js"; |
| 7 | +import { logger } from "./logger.js"; |
| 8 | +import { prompts } from "./prompts.js"; |
| 9 | + |
| 10 | +const CONFIG_DIRECTORY = join(homedir(), ".react-doctor"); |
| 11 | +const CONFIG_FILE = join(CONFIG_DIRECTORY, "config.json"); |
| 12 | + |
| 13 | +interface SkillPromptConfig { |
| 14 | + skillPromptDismissed?: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +const readSkillPromptConfig = (): SkillPromptConfig => { |
| 18 | + try { |
| 19 | + if (!existsSync(CONFIG_FILE)) return {}; |
| 20 | + return JSON.parse(readFileSync(CONFIG_FILE, "utf-8")); |
| 21 | + } catch { |
| 22 | + return {}; |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +const writeSkillPromptConfig = (config: SkillPromptConfig): void => { |
| 27 | + try { |
| 28 | + if (!existsSync(CONFIG_DIRECTORY)) { |
| 29 | + mkdirSync(CONFIG_DIRECTORY, { recursive: true }); |
| 30 | + } |
| 31 | + writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2)); |
| 32 | + } catch {} |
| 33 | +}; |
| 34 | + |
| 35 | +const installSkill = (): void => { |
| 36 | + try { |
| 37 | + execSync(`curl -fsSL ${INSTALL_SKILL_URL} | bash`, { stdio: "inherit" }); |
| 38 | + } catch { |
| 39 | + logger.break(); |
| 40 | + logger.dim("Skill install failed. You can install manually:"); |
| 41 | + logger.dim(` curl -fsSL ${INSTALL_SKILL_URL} | bash`); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +export const maybePromptSkillInstall = async (shouldSkipPrompts: boolean): Promise<void> => { |
| 46 | + const config = readSkillPromptConfig(); |
| 47 | + if (config.skillPromptDismissed) return; |
| 48 | + if (shouldSkipPrompts) return; |
| 49 | + |
| 50 | + logger.break(); |
| 51 | + logger.log( |
| 52 | + `${highlighter.info("💡")} Have your coding agent fix these issues automatically?`, |
| 53 | + ); |
| 54 | + logger.dim( |
| 55 | + ` Install the ${highlighter.info("react-doctor")} skill to teach Cursor, Claude Code,`, |
| 56 | + ); |
| 57 | + logger.dim(" Ami, and other AI agents how to diagnose and fix React issues."); |
| 58 | + logger.break(); |
| 59 | + |
| 60 | + const { shouldInstall } = await prompts({ |
| 61 | + type: "confirm", |
| 62 | + name: "shouldInstall", |
| 63 | + message: "Install skill? (recommended)", |
| 64 | + initial: true, |
| 65 | + }); |
| 66 | + |
| 67 | + if (shouldInstall) { |
| 68 | + logger.break(); |
| 69 | + installSkill(); |
| 70 | + } |
| 71 | + |
| 72 | + writeSkillPromptConfig({ ...config, skillPromptDismissed: true }); |
| 73 | +}; |
0 commit comments