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
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
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.
......
......@@ -55,7 +55,7 @@ const program = new Command(packageJson.name)
// x-ref: https://github.com/tj/commander.js/pull/1427
.enablePositionalOptions()
program
const upgradeCommand = program
.command('upgrade')
.description(
'Upgrade Next.js apps to desired versions with a single command.'
......@@ -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')
.description(
'Generate Next.js documentation index for AI coding agents (Claude, Cursor, etc.).'
)
.option(
'--version <version>',
'Next.js version (auto-detected if not provided)'
'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('--output <file>', 'Target file path (e.g., CLAUDE.md, AGENTS.md)')
.action(async (options) => {
.action(async () => {
try {
await runAgentsMd(options)
await runUpgradeAgentsMd()
} catch (error) {
if (error instanceof BadInput) {
console.error(error.message)
......@@ -103,13 +103,18 @@ program
})
program
.command('upgrade-agents-md')
.command('agents-md')
.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 {
await runUpgradeAgentsMd()
await runAgentsMd(options)
} catch (error) {
if (error instanceof BadInput) {
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
* upserts the canonical create-next-app-style agent-rules block into
......
......@@ -93,7 +93,7 @@ describe('warnIfMissingAgentRules', () => {
// Install command + both filenames.
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('CLAUDE.md')
......@@ -226,7 +226,7 @@ describe('checkAgentRulesForDev (dev-side fatal gate)', () => {
expect(message).toContain('fail at runtime')
// 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
// grab CLI flags more readily than env vars), and explicitly
......
......@@ -139,7 +139,7 @@ function tryReadFile(filePath: string): string | null {
* the sub-package (e.g. `apps/web/`), never the monorepo root.
*
* 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
* the codemod anchored on the same location.
*/
......@@ -168,7 +168,7 @@ function hasAgentRulesInstalled(dir: string): boolean {
*/
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
......
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