Skip to content

Commit e484e38

Browse files
royangerclaude
andcommitted
feat(upgrade): add --agents-md flag
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a7ddcab commit e484e38

6 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎.changeset/upgrade-agents-md.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/upgrade': minor
3+
---
4+
5+
Add an `--agents-md` flag that writes Clerk sections to the project's `AGENTS.md` for each installed Clerk SDK, then exits. Interactive upgrades also offer to write these sections.

‎packages/upgrade/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
]
3434
},
3535
"dependencies": {
36+
"@clerk/shared": "workspace:^",
3637
"chalk": "^5.6.2",
3738
"ejs": "3.1.10",
3839
"gray-matter": "^4.0.3",

‎packages/upgrade/src/__tests__/integration/cli.test.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,39 @@ describe('CLI Integration', () => {
175175
});
176176
});
177177

178+
describe('--agents-md flag', () => {
179+
let fixture;
180+
181+
beforeEach(() => {
182+
fixture = createTempFixture('nextjs-v7');
183+
});
184+
185+
afterEach(() => {
186+
fixture?.cleanup();
187+
});
188+
189+
it('writes AGENTS.md and CLAUDE.md without running SDK upgrade detection', async () => {
190+
const fs = await import('node:fs');
191+
const result = await runCli(['--dir', fixture.path, '--agents-md'], { timeout: 15000 });
192+
193+
expect(result.exitCode).toBe(0);
194+
expect(result.stdout).toContain('Wrote Clerk AGENTS.md sections');
195+
expect(fs.readFileSync(path.join(fixture.path, 'AGENTS.md'), 'utf8')).toContain(
196+
'<!-- BEGIN:clerk-agent-rules:nextjs -->',
197+
);
198+
expect(fs.readFileSync(path.join(fixture.path, 'CLAUDE.md'), 'utf8')).toBe('@AGENTS.md\n');
199+
});
200+
201+
it('does not write files in dry-run mode', async () => {
202+
const fs = await import('node:fs');
203+
const result = await runCli(['--dir', fixture.path, '--agents-md', '--dry-run'], { timeout: 15000 });
204+
205+
expect(result.exitCode).toBe(0);
206+
expect(result.stdout).toContain('[dry run] Would write');
207+
expect(fs.existsSync(path.join(fixture.path, 'AGENTS.md'))).toBe(false);
208+
});
209+
});
210+
178211
describe('Version Display', () => {
179212
it('shows current version in output', async () => {
180213
const dir = getFixturePath('nextjs-v6');

‎packages/upgrade/src/cli.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
renderWarning,
1818
} from './render.js';
1919
import { runCodemods, runScans } from './runner.js';
20+
import { detectClerkAgentRuleSdks, writeClerkAgentRules } from './util/agent-rules.js';
2021
import {
2122
detectSdk,
2223
findWorkspaceRoot,
@@ -50,6 +51,7 @@ const cli = meow(
5051
--release Name of the release you're upgrading to (e.g. core-3)
5152
--canary Upgrade to the latest canary version instead of the stable release
5253
--dry-run Show what would be done without making changes
54+
--agents-md Write Clerk AGENTS.md sections and exit
5355
5456
Examples
5557
$ npx @clerk/upgrade
@@ -70,6 +72,7 @@ const cli = meow(
7072
flags: {
7173
dir: { type: 'string', default: process.cwd() },
7274
dryRun: { type: 'boolean', default: false },
75+
agentsMd: { type: 'boolean', default: false },
7376
glob: { type: 'string', default: '**/*.(js|jsx|ts|tsx|mjs|cjs)' },
7477
ignore: { type: 'string', isMultiple: true },
7578
release: { type: 'string' },
@@ -93,13 +96,19 @@ async function main() {
9396
release: cli.flags.release,
9497
skipCodemods: cli.flags.skipCodemods,
9598
skipUpgrade: cli.flags.skipUpgrade,
99+
agentsMd: cli.flags.agentsMd,
96100
};
97101

98102
if (options.dryRun) {
99103
renderWarning(' Upgrade running in dry run mode - no changes will be made');
100104
renderNewline();
101105
}
102106

107+
if (options.agentsMd) {
108+
writeAgentsMd(options);
109+
return;
110+
}
111+
103112
// Step 1: Detect or prompt for SDK
104113
let sdk = normalizeSdkName(cli.flags.sdk);
105114

@@ -237,6 +246,13 @@ async function main() {
237246

238247
console.log('');
239248

249+
if (isInteractive) {
250+
const sdks = detectClerkAgentRuleSdks(options.dir);
251+
if (sdks.length > 0 && (await promptConfirm('Write Clerk AGENTS.md sections?', true))) {
252+
writeAgentsMd(options, sdks);
253+
}
254+
}
255+
240256
// Step 6: Handle upgrade status
241257
if (options.skipUpgrade) {
242258
renderText('Skipping package upgrade (--skip-upgrade flag)', 'yellow');
@@ -271,6 +287,23 @@ async function main() {
271287
renderComplete(sdk, config.docsUrl);
272288
}
273289

290+
function writeAgentsMd(options, detectedSdks) {
291+
const sdks = detectedSdks || detectClerkAgentRuleSdks(options.dir);
292+
293+
if (sdks.length === 0) {
294+
renderWarning('No Clerk SDK packages found for AGENTS.md');
295+
return;
296+
}
297+
298+
const result = writeClerkAgentRules({ dir: options.dir, dryRun: options.dryRun, sdks });
299+
const prefix = options.dryRun ? '[dry run] Would write' : 'Wrote';
300+
301+
renderText(`${prefix} Clerk AGENTS.md sections for ${sdks.map(s => `@clerk/${s}`).join(', ')}`, 'blue');
302+
renderText(`Target: ${result.targetDir}`);
303+
renderText(`AGENTS.md: ${result.agentsMd}; CLAUDE.md: ${result.claudeMd}`);
304+
renderNewline();
305+
}
306+
274307
async function performUpgrade(sdk, packageManager, config, options) {
275308
const targetPackage = getTargetPackageName(sdk);
276309
const oldPackage = getOldPackageName(sdk);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
import { isClerkAgentRulesSdk, writeClerkAgentRules as writeRules } from '@clerk/shared/agentRules';
5+
6+
import { findWorkspaceRoot, getInstalledClerkPackages, getWorkspacePackageDirs } from './detect-sdk.js';
7+
8+
export function detectClerkAgentRuleSdks(dir) {
9+
const workspaceRoot = findWorkspaceRoot(dir);
10+
const packageDirs = workspaceRoot ? getWorkspacePackageDirs(workspaceRoot) : [dir];
11+
const sdks = new Set();
12+
13+
for (const packageDir of packageDirs) {
14+
for (const packageName of Object.keys(getInstalledClerkPackages(packageDir))) {
15+
const sdk = packageName.replace(/^@clerk\//, '');
16+
if (isClerkAgentRulesSdk(sdk)) {
17+
sdks.add(sdk);
18+
}
19+
}
20+
}
21+
22+
return [...sdks].sort();
23+
}
24+
25+
export function writeClerkAgentRules({ dir, dryRun = false, sdks = detectClerkAgentRuleSdks(dir) }) {
26+
return { ...writeRules({ cwd: dir, dryRun, fs, path, sdks }), sdks };
27+
}

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)