Commit cec4b464 authored by Jude Gao's avatar Jude Gao

[agents-md] Remove explicit `upgrade agents-md` subcommand

The codemod still auto-scaffolds AGENTS.md / CLAUDE.md as a
post-upgrade step during `codemod upgrade` — the explicit subcommand
was redundant.
parent 455210e6
...@@ -59,13 +59,13 @@ npx create-next-app@canary --no-agents-md ...@@ -59,13 +59,13 @@ npx create-next-app@canary --no-agents-md
### Existing projects ### Existing projects
On Next.js `v16.2.0-canary.37` or later, run the codemod from your Next.js app directory (in a monorepo, the sub-package that declares `next`): On Next.js `v16.2.0-canary.37` or later, upgrading via the codemod automatically scaffolds the agent-rules block as a post-upgrade step. Run it from your Next.js app directory (in a monorepo, the sub-package that declares `next`):
```bash ```bash
npx @next/codemod@canary upgrade agents-md npx @next/codemod@canary upgrade
``` ```
`codemod upgrade` also runs this scaffold as a post-upgrade step. The codemod upserts into an existing `AGENTS.md` or `CLAUDE.md`, or creates both if neither exists: The codemod upserts into an existing `AGENTS.md` or `CLAUDE.md`, or creates both if neither exists:
```md filename="AGENTS.md" ```md filename="AGENTS.md"
<!-- BEGIN:nextjs-agent-rules --> <!-- BEGIN:nextjs-agent-rules -->
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
import { Command } from 'commander' import { Command } from 'commander'
import { runUpgrade } from './upgrade' import { runUpgrade } from './upgrade'
import { runAgentsMd } from './agents-md' import { runAgentsMd } from './agents-md'
import { runUpgradeAgentsMd } from './upgrade-agents-md'
import { runTransform } from './transform' import { runTransform } from './transform'
import { BadInput } from './shared' import { BadInput } from './shared'
...@@ -55,7 +54,7 @@ const program = new Command(packageJson.name) ...@@ -55,7 +54,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()
const upgradeCommand = program 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,29 +78,6 @@ const upgradeCommand = program ...@@ -79,29 +78,6 @@ const upgradeCommand = 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(
'Scaffold AGENTS.md / CLAUDE.md from the bundled Next.js docs (Next.js 16.2+). Must be run from the Next.js app directory.'
)
.action(async () => {
try {
await runUpgradeAgentsMd()
} catch (error) {
if (error instanceof BadInput) {
console.error(error.message)
} else {
console.error(error)
}
process.exit(1)
}
})
program program
.command('agents-md') .command('agents-md')
.description( .description(
......
/**
* CLI handler for `npx @next/codemod upgrade agents-md`. Upserts the
* create-next-app agent-rules block into `AGENTS.md` / `CLAUDE.md`
* using the docs bundled at `node_modules/next/dist/docs/`. Anchors
* strictly on `cwd` — same contract as `codemod upgrade`.
*/
import pc from 'picocolors'
import { BadInput } from './shared'
import {
getNextjsVersion,
requireNextProjectDir,
hasBundledDocs,
writeBundledDocsAgentFiles,
type BundledDocsFileAction,
} from '../lib/agents-md'
export async function runUpgradeAgentsMd(): Promise<void> {
const cwd = process.cwd()
let projectDir: string
try {
projectDir = requireNextProjectDir(cwd)
} catch (err) {
throw new BadInput((err as Error).message)
}
if (!hasBundledDocs(projectDir)) {
throw new BadInput(
`The Next.js install at ${pc.cyan(projectDir)} does not ship bundled docs. ` +
`Upgrade to Next.js 16.2 or newer, or run \`npx @next/codemod agents-md\` ` +
`to pull docs for an older version via git.`
)
}
const detectedVersion = getNextjsVersion(projectDir).version
const versionLabel = detectedVersion
? pc.cyan(detectedVersion)
: pc.cyan('next')
console.log(
`\nScaffolding agent rules for Next.js ${versionLabel} in ${pc.cyan(projectDir)}.`
)
const result = writeBundledDocsAgentFiles(projectDir)
const describe = (file: string, action: BundledDocsFileAction) => {
// Don't list files we didn't touch at all.
if (action === 'skipped') return
const symbol = action === 'unchanged' ? pc.gray('•') : pc.green('✓')
const verb =
action === 'created'
? 'Created'
: action === 'updated'
? 'Updated'
: 'Up to date:'
console.log(`${symbol} ${verb} ${pc.bold(file)}`)
}
describe('AGENTS.md', result.agentsMd)
describe('CLAUDE.md', result.claudeMd)
console.log('')
}
...@@ -121,9 +121,8 @@ export async function runUpgrade( ...@@ -121,9 +121,8 @@ export async function runUpgrade(
const { verbose } = options const { verbose } = options
// Validate `cwd` once at the top: must have `package.json` and a // Validate `cwd` once at the top: must have `package.json` and a
// resolvable `next` install. Shared with `upgrade agents-md` via // resolvable `next` install. `requireNextProjectDir` throws a plain
// `requireNextProjectDir`, which throws a plain `Error` that we // `Error` that we rewrap as `BadInput` for clean CLI formatting.
// rewrap as `BadInput` for clean CLI formatting.
let projectDir: string let projectDir: string
try { try {
projectDir = requireNextProjectDir(cwd) projectDir = requireNextProjectDir(cwd)
...@@ -440,8 +439,8 @@ export async function runUpgrade( ...@@ -440,8 +439,8 @@ export async function runUpgrade(
warnDependenciesOutOfRange(appPackageJson, versionMapping) warnDependenciesOutOfRange(appPackageJson, versionMapping)
// Scaffold `AGENTS.md` + `CLAUDE.md` when the upgraded Next.js // Scaffold `AGENTS.md` + `CLAUDE.md` when the upgraded Next.js
// ships bundled docs. Same fast path `upgrade agents-md` runs, so // ships bundled docs, so upgrading picks up the agent rules as a
// upgrading picks up the agent rules as a side effect. // side effect.
if (hasBundledDocs(projectDir)) { if (hasBundledDocs(projectDir)) {
const result = writeBundledDocsAgentFiles(projectDir) const result = writeBundledDocsAgentFiles(projectDir)
const touched: string[] = [] const touched: string[] = []
......
...@@ -44,9 +44,9 @@ export function getNextjsVersion(cwd: string): NextjsVersionResult { ...@@ -44,9 +44,9 @@ export function getNextjsVersion(cwd: string): NextjsVersionResult {
} }
/** /**
* Strict-cwd check shared by `codemod upgrade` and `upgrade agents-md`: * Strict-cwd check for `codemod upgrade`: `cwd/package.json` must
* `cwd/package.json` must exist and `next` must be resolvable from * exist and `next` must be resolvable from `cwd`. No walk-up —
* `cwd`. No walk-up — monorepo root or wrong subdir both error out. * monorepo root or wrong subdir both error out.
* *
* Throws a plain `Error` (this module is in the `lib/` layer and * Throws a plain `Error` (this module is in the `lib/` layer and
* can't import `BadInput`); `bin/` callers rewrap for clean CLI * can't import `BadInput`); `bin/` callers rewrap for clean CLI
......
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