(options: AgentsMdOptions)
| 35 | } |
| 36 | |
| 37 | export async function runAgentsMd(options: AgentsMdOptions): Promise<void> { |
| 38 | const cwd = process.cwd() |
| 39 | |
| 40 | // Mode logic: |
| 41 | // 1. No flags → interactive mode (prompts for version + target file) |
| 42 | // 2. --version provided → --output is REQUIRED (error if missing) |
| 43 | // 3. --output alone → auto-detect version, error if not found |
| 44 | |
| 45 | let nextjsVersion: string |
| 46 | let targetFile: string |
| 47 | |
| 48 | if (options.version) { |
| 49 | // --version provided: --output is required |
| 50 | if (!options.output) { |
| 51 | throw new BadInput( |
| 52 | 'When using --version, --output is also required.\n' + |
| 53 | 'Example: npx @next/codemod agents-md --version 15.1.3 --output CLAUDE.md' |
| 54 | ) |
| 55 | } |
| 56 | nextjsVersion = options.version |
| 57 | targetFile = options.output |
| 58 | } else if (options.output) { |
| 59 | // --output alone: auto-detect version |
| 60 | const detected = getNextjsVersion(cwd) |
| 61 | if (!detected.version) { |
| 62 | throw new BadInput( |
| 63 | 'Could not detect Next.js version. Use --version to specify.\n' + |
| 64 | `Example: npx @next/codemod agents-md --version 15.1.3 --output ${options.output}` |
| 65 | ) |
| 66 | } |
| 67 | nextjsVersion = detected.version |
| 68 | targetFile = options.output |
| 69 | } else { |
| 70 | // No flags: interactive mode |
| 71 | const promptedOptions = await promptForOptions(cwd) |
| 72 | nextjsVersion = promptedOptions.nextVersion |
| 73 | targetFile = promptedOptions.targetFile |
| 74 | } |
| 75 | |
| 76 | const claudeMdPath = path.join(cwd, targetFile) |
| 77 | const docsPath = path.join(cwd, DOCS_DIR_NAME) |
| 78 | const docsLinkPath = `./${DOCS_DIR_NAME}` |
| 79 | |
| 80 | let sizeBefore = 0 |
| 81 | let isNewFile = true |
| 82 | let existingContent = '' |
| 83 | |
| 84 | if (fs.existsSync(claudeMdPath)) { |
| 85 | existingContent = fs.readFileSync(claudeMdPath, 'utf-8') |
| 86 | sizeBefore = Buffer.byteLength(existingContent, 'utf-8') |
| 87 | isNewFile = false |
| 88 | } |
| 89 | |
| 90 | console.log( |
| 91 | `\nDownloading Next.js ${pc.cyan(nextjsVersion)} documentation to ${pc.cyan(DOCS_DIR_NAME)}...` |
| 92 | ) |
| 93 | |
| 94 | const pullResult = await pullDocs({ |
no test coverage detected