Commit 91b79650 authored by Jude Gao's avatar Jude Gao

[agents-md] Nest `agents-md` as a subcommand of `upgrade`

Moves the bundled-docs fast path from a top-level `upgrade-agents-md`
command to `upgrade agents-md`, nested under the existing `upgrade`
command. Cleaner mental model — it's part of the upgrade workflow,
not a separate CLI verb — and less name collision pressure on the
top level. The dev error message, the post-upgrade hook narrative in
the docs, and the help output all reference the nested form now.
The existing top-level `agents-md` (legacy git-clone) is untouched.
parent 8f412935
...@@ -62,10 +62,10 @@ npx create-next-app@canary --no-agents-md ...@@ -62,10 +62,10 @@ npx create-next-app@canary --no-agents-md
On Next.js `v16.2.0-canary.37` or later, run the codemod from your Next.js app directory to install the agent rules automatically: On Next.js `v16.2.0-canary.37` or later, run the codemod from your Next.js app directory to install the agent rules automatically:
```bash ```bash
npx @next/codemod@canary upgrade-agents-md npx @next/codemod@canary upgrade agents-md
``` ```
In a monorepo, run the command from inside the app package (e.g. `apps/web/`), not the monorepo root. `AGENTS.md` belongs next to the `package.json` that declares `next`, because that's where AI coding agents look for it. `upgrade-agents-md` aborts with an actionable error if it can't resolve `next` from the current directory. In a monorepo, run the command from inside the app package (e.g. `apps/web/`), not the monorepo root. `AGENTS.md` belongs next to the `package.json` that declares `next`, because that's where AI coding agents look for it. The command aborts with an actionable error if it can't resolve `next` from the current directory.
`codemod upgrade` also runs this scaffold automatically as a post-upgrade step, so upgrading Next.js in an existing project picks up the agent rules for free. `codemod upgrade` also runs this scaffold automatically as a post-upgrade step, so upgrading Next.js in an existing project picks up the agent rules for free.
......
...@@ -55,7 +55,7 @@ const program = new Command(packageJson.name) ...@@ -55,7 +55,7 @@ const program = new Command(packageJson.name)
// x-ref: https://github.com/tj/commander.js/pull/1427 // x-ref: https://github.com/tj/commander.js/pull/1427
.enablePositionalOptions() .enablePositionalOptions()
program const upgradeCommand = program
.command('upgrade') .command('upgrade')
.description( .description(
'Upgrade Next.js apps to desired versions with a single command.' 'Upgrade Next.js apps to desired versions with a single command.'
...@@ -79,19 +79,19 @@ program ...@@ -79,19 +79,19 @@ program
} }
}) })
program // Nested subcommand: `@next/codemod upgrade agents-md`.
// Scaffolds AGENTS.md / CLAUDE.md from the bundled Next.js docs in
// the current app directory. Nested under `upgrade` so it shares the
// strict-cwd contract and mental model with the rest of the upgrade
// workflow — you cd into the app and run `codemod upgrade ...`.
upgradeCommand
.command('agents-md') .command('agents-md')
.description( .description(
'Generate Next.js documentation index for AI coding agents (Claude, Cursor, etc.).' 'Scaffold AGENTS.md / CLAUDE.md from the bundled Next.js docs (Next.js 16.2+). Must be run from the Next.js app directory.'
)
.option(
'--version <version>',
'Next.js version (auto-detected if not provided)'
) )
.option('--output <file>', 'Target file path (e.g., CLAUDE.md, AGENTS.md)') .action(async () => {
.action(async (options) => {
try { try {
await runAgentsMd(options) await runUpgradeAgentsMd()
} catch (error) { } catch (error) {
if (error instanceof BadInput) { if (error instanceof BadInput) {
console.error(error.message) console.error(error.message)
...@@ -103,13 +103,18 @@ program ...@@ -103,13 +103,18 @@ program
}) })
program program
.command('upgrade-agents-md') .command('agents-md')
.description( .description(
'Scaffold AGENTS.md / CLAUDE.md from the bundled Next.js docs (Next.js 16.2+). Must be run from the Next.js app directory.' 'Generate Next.js documentation index for AI coding agents (Claude, Cursor, etc.).'
) )
.action(async () => { .option(
'--version <version>',
'Next.js version (auto-detected if not provided)'
)
.option('--output <file>', 'Target file path (e.g., CLAUDE.md, AGENTS.md)')
.action(async (options) => {
try { try {
await runUpgradeAgentsMd() await runAgentsMd(options)
} catch (error) { } catch (error) {
if (error instanceof BadInput) { if (error instanceof BadInput) {
console.error(error.message) console.error(error.message)
......
/** /**
* CLI handler for `npx @next/codemod upgrade-agents-md`. * CLI handler for `npx @next/codemod upgrade agents-md` — the
* `agents-md` subcommand nested under `upgrade`.
* *
* This is the "fast path" variant of the `agents-md` codemod. It * This is the "fast path" variant of the `agents-md` codemod. It
* upserts the canonical create-next-app-style agent-rules block into * upserts the canonical create-next-app-style agent-rules block into
......
...@@ -93,7 +93,7 @@ describe('warnIfMissingAgentRules', () => { ...@@ -93,7 +93,7 @@ describe('warnIfMissingAgentRules', () => {
// Install command + both filenames. // Install command + both filenames.
expect(loggedMessage).toContain( expect(loggedMessage).toContain(
'npx @next/codemod@canary upgrade-agents-md' 'npx @next/codemod@canary upgrade agents-md'
) )
expect(loggedMessage).toContain('AGENTS.md') expect(loggedMessage).toContain('AGENTS.md')
expect(loggedMessage).toContain('CLAUDE.md') expect(loggedMessage).toContain('CLAUDE.md')
...@@ -226,7 +226,7 @@ describe('checkAgentRulesForDev (dev-side fatal gate)', () => { ...@@ -226,7 +226,7 @@ describe('checkAgentRulesForDev (dev-side fatal gate)', () => {
expect(message).toContain('fail at runtime') expect(message).toContain('fail at runtime')
// Primary fix — the codemod command. // Primary fix — the codemod command.
expect(message).toContain('npx @next/codemod@canary upgrade-agents-md') expect(message).toContain('npx @next/codemod@canary upgrade agents-md')
// Escape hatch — disclosed, but env var not a CLI flag (agents // Escape hatch — disclosed, but env var not a CLI flag (agents
// grab CLI flags more readily than env vars), and explicitly // grab CLI flags more readily than env vars), and explicitly
......
...@@ -139,7 +139,7 @@ function tryReadFile(filePath: string): string | null { ...@@ -139,7 +139,7 @@ function tryReadFile(filePath: string): string | null {
* the sub-package (e.g. `apps/web/`), never the monorepo root. * the sub-package (e.g. `apps/web/`), never the monorepo root.
* *
* The only supported way to install the marker is via * The only supported way to install the marker is via
* `npx @next/codemod@canary upgrade-agents-md`, which writes the * `npx @next/codemod@canary upgrade agents-md`, which writes the
* managed block to the same project directory — keeping the check and * managed block to the same project directory — keeping the check and
* the codemod anchored on the same location. * the codemod anchored on the same location.
*/ */
...@@ -168,7 +168,7 @@ function hasAgentRulesInstalled(dir: string): boolean { ...@@ -168,7 +168,7 @@ function hasAgentRulesInstalled(dir: string): boolean {
*/ */
const BYPASS_ENV_VAR = 'NEXT_DISABLE_AGENT_RULE_CHECK' const BYPASS_ENV_VAR = 'NEXT_DISABLE_AGENT_RULE_CHECK'
const AGENTS_MD_INSTALL_COMMAND = 'npx @next/codemod@canary upgrade-agents-md' const AGENTS_MD_INSTALL_COMMAND = 'npx @next/codemod@canary upgrade agents-md'
/** /**
* Print a yellow `⚠` warning at the end of `next build` when an AI * Print a yellow `⚠` warning at the end of `next build` when an AI
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment