(options: PullOptions)
| 61 | } |
| 62 | |
| 63 | export async function pullDocs(options: PullOptions): Promise<PullResult> { |
| 64 | const { cwd, version: versionOverride, docsDir } = options |
| 65 | |
| 66 | let nextjsVersion: string |
| 67 | |
| 68 | if (versionOverride) { |
| 69 | nextjsVersion = versionOverride |
| 70 | } else { |
| 71 | const versionResult = getNextjsVersion(cwd) |
| 72 | if (!versionResult.version) { |
| 73 | return { |
| 74 | success: false, |
| 75 | error: versionResult.error || 'Could not detect Next.js version', |
| 76 | } |
| 77 | } |
| 78 | nextjsVersion = versionResult.version |
| 79 | } |
| 80 | |
| 81 | const docsPath = |
| 82 | docsDir ?? fs.mkdtempSync(path.join(os.tmpdir(), 'next-agents-md-')) |
| 83 | const useTempDir = !docsDir |
| 84 | |
| 85 | try { |
| 86 | if (useTempDir && fs.existsSync(docsPath)) { |
| 87 | fs.rmSync(docsPath, { recursive: true }) |
| 88 | } |
| 89 | |
| 90 | const tag = versionToGitHubTag(nextjsVersion) |
| 91 | await cloneDocsFolder(tag, docsPath) |
| 92 | |
| 93 | return { |
| 94 | success: true, |
| 95 | docsPath, |
| 96 | nextjsVersion, |
| 97 | } |
| 98 | } catch (error) { |
| 99 | if (useTempDir && fs.existsSync(docsPath)) { |
| 100 | fs.rmSync(docsPath, { recursive: true }) |
| 101 | } |
| 102 | |
| 103 | return { |
| 104 | success: false, |
| 105 | error: error instanceof Error ? error.message : String(error), |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | async function cloneDocsFolder(tag: string, destDir: string): Promise<void> { |
| 111 | const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'next-agents-md-')) |
no test coverage detected