(
libraryId: string,
query: string,
options: { json?: boolean }
)
| 115 | } |
| 116 | |
| 117 | async function queryCommand( |
| 118 | libraryId: string, |
| 119 | query: string, |
| 120 | options: { json?: boolean } |
| 121 | ): Promise<void> { |
| 122 | trackEvent("command", { name: "docs" }); |
| 123 | |
| 124 | // Git Bash on Windows rewrites "/owner/repo" into a Windows path; recover it. |
| 125 | libraryId = recoverLibraryId(libraryId); |
| 126 | |
| 127 | if (!libraryId.startsWith("/") || !/^\/[^/]+\/[^/]/.test(libraryId)) { |
| 128 | log.error(`Invalid library ID: "${libraryId}"`); |
| 129 | log.info(`Expected format: /owner/repo or /owner/repo/version (e.g., /facebook/react)`); |
| 130 | log.info(`Run "ctx7 library <name>" to find the correct ID`); |
| 131 | if (process.platform === "win32") { |
| 132 | log.info( |
| 133 | `On Git Bash, prefix the ID with an extra slash to avoid path conversion: ctx7 docs "//facebook/react" "<your question>"` |
| 134 | ); |
| 135 | } |
| 136 | process.exitCode = 1; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | const accessToken = getAccessToken(); |
| 141 | |
| 142 | const spinner = isTTY ? ora(`Fetching docs for "${libraryId}"...`).start() : null; |
| 143 | const outputType = options.json ? "json" : "txt"; |
| 144 | |
| 145 | let result; |
| 146 | try { |
| 147 | result = await getLibraryContext(libraryId, query, { type: outputType }, accessToken); |
| 148 | } catch (err) { |
| 149 | spinner?.fail(`Error: ${err instanceof Error ? err.message : String(err)}`); |
| 150 | if (!spinner) log.error(err instanceof Error ? err.message : String(err)); |
| 151 | process.exitCode = 1; |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | if (typeof result === "string") { |
| 156 | spinner?.stop(); |
| 157 | console.log(result); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | const ctx = result as ContextResponse; |
| 162 | |
| 163 | if (ctx.error) { |
| 164 | if (ctx.redirectUrl) { |
| 165 | spinner?.warn("Library has been redirected"); |
| 166 | if (!spinner) log.warn("Library has been redirected"); |
| 167 | log.info(`New ID: ${pc.cyan(ctx.redirectUrl)}`); |
| 168 | log.info(`Run: ${pc.cyan(`ctx7 docs "${ctx.redirectUrl}" "${query}"`)}`); |
| 169 | process.exitCode = 1; |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | spinner?.fail(ctx.message || ctx.error); |
| 174 | if (!spinner) log.error(ctx.message || ctx.error); |
no test coverage detected