|
1 | | -import { existsSync } from "node:fs"; |
2 | | -import { readConfig, getConfigPath } from "../utils/config.js"; |
3 | | -import { getSnapshotPath } from "../cache/snapshot.js"; |
| 1 | +import { arch, platform } from "node:os"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | +import { |
| 4 | + DEFAULT_AI_MODELS, |
| 5 | + inspectGroqProvider, |
| 6 | + type GroqProviderInspection |
| 7 | +} from "../ai/groq.js"; |
| 8 | +import { scanFiles } from "../analyzers/fileScanner.js"; |
| 9 | +import { detectFramework } from "../analyzers/frameworkDetector.js"; |
| 10 | +import { detectProjectMetadata } from "../analyzers/projectMetadata.js"; |
| 11 | +import { inspectSnapshot } from "../cache/snapshot.js"; |
| 12 | +import { readConfig, type DevmapConfig } from "../utils/config.js"; |
| 13 | +import { DevmapError } from "../utils/errors.js"; |
4 | 14 | import { output } from "../utils/output.js"; |
5 | 15 |
|
6 | | -export async function doctorCommand(): Promise<void> { |
7 | | - const config = await readConfig(); |
| 16 | +const DEVMAP_VERSION = "0.1.0"; |
| 17 | +const MINIMUM_NODE_MAJOR = 18; |
| 18 | + |
| 19 | +export type DoctorDependencies = { |
| 20 | + projectRoot?: string; |
| 21 | + loadConfig?: () => Promise<DevmapConfig | null>; |
| 22 | + inspectProvider?: ( |
| 23 | + apiKey: string, |
| 24 | + model: string |
| 25 | + ) => Promise<GroqProviderInspection>; |
| 26 | +}; |
| 27 | + |
| 28 | +export async function doctorCommand( |
| 29 | + dependencies: DoctorDependencies = {} |
| 30 | +): Promise<void> { |
| 31 | + const projectRoot = resolve(dependencies.projectRoot ?? process.cwd()); |
| 32 | + const loadConfig = dependencies.loadConfig ?? readConfig; |
| 33 | + const inspectProvider = dependencies.inspectProvider ?? inspectGroqProvider; |
| 34 | + const [config, snapshotResult, files] = await Promise.all([ |
| 35 | + loadConfig(), |
| 36 | + inspectSnapshot(projectRoot), |
| 37 | + scanFiles(projectRoot) |
| 38 | + ]); |
| 39 | + const framework = detectFramework(files); |
| 40 | + const project = detectProjectMetadata(projectRoot, framework, files); |
| 41 | + const selectedModel = config?.model === "auto" |
| 42 | + ? DEFAULT_AI_MODELS.ask |
| 43 | + : config?.model; |
| 44 | + const issues: string[] = []; |
| 45 | + const nodeSupported = readNodeMajor(process.version) >= MINIMUM_NODE_MAJOR; |
8 | 46 |
|
9 | 47 | output.section("DevMap Doctor"); |
10 | | - output.keyValue("Node.js", process.version); |
| 48 | + output.keyValue("DevMap", DEVMAP_VERSION); |
| 49 | + output.keyValue("Node.js", `${process.version} (${nodeSupported ? "supported" : "unsupported"})`); |
| 50 | + output.keyValue("OS", `${platform()}/${arch()}`); |
| 51 | + output.keyValue("Project", project.name); |
| 52 | + output.keyValue("Framework", framework); |
| 53 | + output.keyValue("Package Manager", project.packageManager); |
11 | 54 | output.keyValue("Provider", config?.provider ?? "not configured"); |
12 | | - output.keyValue("API key", config?.apiKey ? "set" : "not set"); |
13 | | - output.keyValue("Config", existsSync(getConfigPath()) ? "exists" : "missing"); |
14 | | - output.keyValue("Snapshot", existsSync(getSnapshotPath(process.cwd())) ? "exists" : "missing"); |
| 55 | + output.keyValue("Config", config ? "exists" : "missing"); |
| 56 | + output.keyValue("Snapshot", snapshotResult.status); |
15 | 57 |
|
16 | 58 | if (!config) { |
17 | | - output.warning("Run devmap init to create ~/.devmap/config.json"); |
18 | | - } else if (!config.apiKey) { |
19 | | - output.warning("Groq API key is not set yet. Add it before Phase 2 AI commands."); |
| 59 | + issues.push("Run devmap init to create ~/.devmap/config.json."); |
| 60 | + output.keyValue("API key", "not configured"); |
| 61 | + output.keyValue("Model", "not configured"); |
| 62 | + } else if (!config.apiKey || !selectedModel) { |
| 63 | + issues.push("Run devmap init again to configure Groq."); |
| 64 | + output.keyValue("API key", "missing"); |
| 65 | + output.keyValue("Model", selectedModel ?? "not configured"); |
20 | 66 | } else { |
21 | | - output.success("Base configuration looks ready"); |
| 67 | + try { |
| 68 | + const provider = await inspectProvider(config.apiKey, selectedModel); |
| 69 | + output.keyValue("API key", provider.reachable ? "valid" : "unreachable"); |
| 70 | + output.keyValue( |
| 71 | + "Model", |
| 72 | + provider.modelAvailable ? selectedModel : `unavailable: ${selectedModel}` |
| 73 | + ); |
| 74 | + |
| 75 | + if (!provider.modelAvailable) { |
| 76 | + issues.push("Run devmap init or choose an available Groq model."); |
| 77 | + } |
| 78 | + } catch (error) { |
| 79 | + const message = error instanceof DevmapError |
| 80 | + ? error.message |
| 81 | + : "Provider diagnostics failed."; |
| 82 | + output.keyValue("API key", "invalid or unreachable"); |
| 83 | + output.keyValue("Model", selectedModel); |
| 84 | + issues.push(message); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (!nodeSupported) { |
| 89 | + issues.push(`Install Node.js ${MINIMUM_NODE_MAJOR} or newer.`); |
| 90 | + } |
| 91 | + |
| 92 | + if (snapshotResult.status === "corrupt" || snapshotResult.status === "unsupported") { |
| 93 | + issues.push("Run devmap analyze --fresh to regenerate the snapshot."); |
22 | 94 | } |
| 95 | + |
| 96 | + if (issues.length === 0) { |
| 97 | + output.success("No issues found"); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + output.section("Issues"); |
| 102 | + for (const issue of issues) { |
| 103 | + output.warning(issue); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function readNodeMajor(version: string): number { |
| 108 | + const match = version.match(/^v?(\d+)/); |
| 109 | + return match ? Number(match[1]) : 0; |
23 | 110 | } |
0 commit comments