Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/cli/src/__tests__/docs-command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, test } from "vitest";

import { normalizeLibraryId } from "../commands/docs.js";

describe("normalizeLibraryId", () => {
test("preserves already valid library IDs", () => {
expect(normalizeLibraryId("/facebook/react")).toBe("/facebook/react");
expect(normalizeLibraryId("/facebook/react/v19.0.0")).toBe("/facebook/react/v19.0.0");
});

test("normalizes Git Bash rewritten Windows paths into library IDs", () => {
expect(normalizeLibraryId("C:/Program Files/Git/facebook/react")).toBe("/facebook/react");
expect(normalizeLibraryId("C:/Program Files/Git/vercel/next.js/v15.0.0")).toBe(
"/vercel/next.js/v15.0.0"
);
});

test("leaves unrelated filesystem paths unchanged", () => {
expect(normalizeLibraryId("C:/Users/alice/project/docs")).toBe("C:/Users/alice/project/docs");
});
});
28 changes: 25 additions & 3 deletions packages/cli/src/commands/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ import type { LibrarySearchResult, ContextResponse } from "../types.js";

const isTTY = process.stdout.isTTY;

export function normalizeLibraryId(input: string): string {
if (/^\/[^/]+\/.+/.test(input)) {
return input;
}

const normalizedPath = input.replace(/\\/g, "/");
const gitBashPrefixMatch = normalizedPath.match(/^[A-Za-z]:\/Program Files\/Git\/(.+)$/i);

if (!gitBashPrefixMatch) {
return input;
}

const segments = gitBashPrefixMatch[1].split("/").filter(Boolean);
if (segments.length < 2) {
return input;
}

return `/${segments.slice(0).join("/")}`;
}

function getReputationLabel(score: number | undefined): "High" | "Medium" | "Low" | "Unknown" {
if (score === undefined || score < 0) return "Unknown";
if (score >= 7) return "High";
Expand Down Expand Up @@ -120,21 +140,23 @@ async function queryCommand(
): Promise<void> {
trackEvent("command", { name: "docs" });

if (!libraryId.startsWith("/") || !/^\/[^/]+\/[^/]/.test(libraryId)) {
const normalizedLibraryId = normalizeLibraryId(libraryId);

if (!normalizedLibraryId.startsWith("/") || !/^\/[^/]+\/[^/]/.test(normalizedLibraryId)) {
log.error(`Invalid library ID: "${libraryId}"`);
log.info(`Expected format: /owner/repo or /owner/repo/version (e.g., /facebook/react)`);
log.info(`Run "ctx7 library <name>" to find the correct ID`);
process.exitCode = 1;
return;
}

const spinner = isTTY ? ora(`Fetching docs for "${libraryId}"...`).start() : null;
const spinner = isTTY ? ora(`Fetching docs for "${normalizedLibraryId}"...`).start() : null;
const accessToken = getAccessToken();
const outputType = options.json ? "json" : "txt";

let result;
try {
result = await getLibraryContext(libraryId, query, { type: outputType }, accessToken);
result = await getLibraryContext(normalizedLibraryId, query, { type: outputType }, accessToken);
} catch (err) {
spinner?.fail(`Error: ${err instanceof Error ? err.message : String(err)}`);
if (!spinner) log.error(err instanceof Error ? err.message : String(err));
Expand Down