@next/codemod 16.3.0-preview.4 → 16.3.0-preview.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/next-codemod.js +1 -0
- package/bin/upgrade.js +103 -29
- package/lib/agents-md.js +44 -0
- package/package.json +1 -1
package/bin/next-codemod.js
CHANGED
|
@@ -43,6 +43,7 @@ program
|
|
|
43
43
|
.argument('[revision]', 'Specify the upgrade type ("patch", "minor", "major"), an NPM dist tag (e.g. "latest", "canary", "rc"), or an exact version (e.g. "15.0.0"). Defaults to "minor".')
|
|
44
44
|
.usage('[revision] [options]')
|
|
45
45
|
.option('--verbose', 'Verbose output', false)
|
|
46
|
+
.option('-y, --yes', 'Skip every interactive prompt and accept its default. Also auto-enabled when stdin is not a TTY (e.g. running under an agent or in CI).', false)
|
|
46
47
|
.action(async (revision, options) => {
|
|
47
48
|
try {
|
|
48
49
|
await (0, upgrade_1.runUpgrade)(revision, options);
|
package/bin/upgrade.js
CHANGED
|
@@ -47,6 +47,7 @@ const picocolors_1 = __importDefault(require("picocolors"));
|
|
|
47
47
|
const handle_package_1 = require("../lib/handle-package");
|
|
48
48
|
const transform_1 = require("./transform");
|
|
49
49
|
const utils_1 = require("../lib/utils");
|
|
50
|
+
const agents_md_1 = require("../lib/agents-md");
|
|
50
51
|
const shared_1 = require("./shared");
|
|
51
52
|
const optionalNextjsPackages = [
|
|
52
53
|
'create-next-app',
|
|
@@ -114,6 +115,10 @@ function resolveSemanticRevision(revision, installedVersion) {
|
|
|
114
115
|
}
|
|
115
116
|
async function runUpgrade(revision, options) {
|
|
116
117
|
const { verbose } = options;
|
|
118
|
+
const nonInteractive = options.yes === true || !process.stdin.isTTY;
|
|
119
|
+
if (nonInteractive) {
|
|
120
|
+
console.log(` Running in non-interactive mode. Every prompt will accept its default.`);
|
|
121
|
+
}
|
|
117
122
|
const appPackageJsonPath = path_1.default.resolve(cwd, 'package.json');
|
|
118
123
|
let appPackageJson = JSON.parse(fs_1.default.readFileSync(appPackageJsonPath, 'utf8'));
|
|
119
124
|
const installedNextVersion = getInstalledNextVersion();
|
|
@@ -189,18 +194,24 @@ async function runUpgrade(revision, options) {
|
|
|
189
194
|
// The mixed case is tricky to handle from a types perspective.
|
|
190
195
|
// We'll recommend to upgrade in the prompt but users can decide to try 18.
|
|
191
196
|
!isPureAppRouter) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
197
|
+
if (nonInteractive) {
|
|
198
|
+
// Default: upgrade React past 18.
|
|
199
|
+
shouldStayOnReact18 = false;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
const shouldStayOnReact18Res = await (0, prompts_1.default)({
|
|
203
|
+
type: 'confirm',
|
|
204
|
+
name: 'shouldStayOnReact18',
|
|
205
|
+
message: `Do you prefer to stay on React 18?` +
|
|
206
|
+
(isMixedApp
|
|
207
|
+
? " Since you're using both pages/ and app/, we recommend upgrading React to use a consistent version throughout your app."
|
|
208
|
+
: ''),
|
|
209
|
+
initial: false,
|
|
210
|
+
active: 'Yes',
|
|
211
|
+
inactive: 'No',
|
|
212
|
+
}, { onCancel: utils_1.onCancel });
|
|
213
|
+
shouldStayOnReact18 = shouldStayOnReact18Res.shouldStayOnReact18;
|
|
214
|
+
}
|
|
204
215
|
}
|
|
205
216
|
// We're resolving a specific version here to avoid including "ugly" version queries
|
|
206
217
|
// in the manifest.
|
|
@@ -212,9 +223,9 @@ async function runUpgrade(revision, options) {
|
|
|
212
223
|
: await loadHighestNPMVersionMatching(`react@${targetNextPackageJson.peerDependencies['react']}`);
|
|
213
224
|
if ((0, semver_1.compare)(targetNextVersion, '15.0.0-canary') >= 0 &&
|
|
214
225
|
(0, semver_1.compare)(targetNextVersion, '16.0.0-canary') < 0) {
|
|
215
|
-
await suggestTurbopack(appPackageJson, targetNextVersion);
|
|
226
|
+
await suggestTurbopack(appPackageJson, targetNextVersion, nonInteractive);
|
|
216
227
|
}
|
|
217
|
-
const codemods = await suggestCodemods(installedNextVersion, targetNextVersion);
|
|
228
|
+
const codemods = await suggestCodemods(installedNextVersion, targetNextVersion, nonInteractive);
|
|
218
229
|
const packageManager = (0, handle_package_1.getPkgManager)(cwd);
|
|
219
230
|
let shouldRunReactCodemods = false;
|
|
220
231
|
let shouldRunReactTypesCodemods = false;
|
|
@@ -223,8 +234,9 @@ async function runUpgrade(revision, options) {
|
|
|
223
234
|
if (!shouldStayOnReact18 &&
|
|
224
235
|
(0, semver_1.compare)(targetReactVersion, '19.0.0-0') >= 0 &&
|
|
225
236
|
(0, semver_1.compare)(installedReactVersion, '19.0.0-0') < 0) {
|
|
226
|
-
shouldRunReactCodemods = await suggestReactCodemods();
|
|
227
|
-
shouldRunReactTypesCodemods =
|
|
237
|
+
shouldRunReactCodemods = await suggestReactCodemods(nonInteractive);
|
|
238
|
+
shouldRunReactTypesCodemods =
|
|
239
|
+
await suggestReactTypesCodemods(nonInteractive);
|
|
228
240
|
execCommand = getNpxCommand(packageManager);
|
|
229
241
|
}
|
|
230
242
|
fs_1.default.writeFileSync(appPackageJsonPath, JSON.stringify(appPackageJson, null, 2));
|
|
@@ -284,6 +296,37 @@ async function runUpgrade(revision, options) {
|
|
|
284
296
|
};
|
|
285
297
|
}
|
|
286
298
|
}
|
|
299
|
+
// Bump `eslint` alongside `eslint-config-next` so the install doesn't fail
|
|
300
|
+
// on a peer-dep mismatch. e.g. `eslint-config-next@16.x` requires
|
|
301
|
+
// `eslint@>=9`, but a project upgrading from Next 15 will still have
|
|
302
|
+
// `eslint@^8` from create-next-app. Skip silently if anything goes wrong;
|
|
303
|
+
// the worst case is the user hits the same peer-dep error they would have
|
|
304
|
+
// without this bump.
|
|
305
|
+
//
|
|
306
|
+
// Only act when the project is actually using `eslint-config-next` — we
|
|
307
|
+
// don't want to silently upgrade eslint majors for projects that use
|
|
308
|
+
// eslint for unrelated reasons.
|
|
309
|
+
if (allDependencies['eslint'] && allDependencies['eslint-config-next']) {
|
|
310
|
+
try {
|
|
311
|
+
const eslintConfigNextPeerDepsJSON = (0, child_process_1.execSync)(`npm --silent view "eslint-config-next@${targetNextVersion}" peerDependencies --json`, { encoding: 'utf-8' });
|
|
312
|
+
const eslintConfigNextPeerDeps = eslintConfigNextPeerDepsJSON.trim() === ''
|
|
313
|
+
? {}
|
|
314
|
+
: JSON.parse(eslintConfigNextPeerDepsJSON);
|
|
315
|
+
const eslintRange = eslintConfigNextPeerDeps?.eslint;
|
|
316
|
+
if (eslintRange) {
|
|
317
|
+
const targetEslintVersion = await loadHighestNPMVersionMatching(`eslint@${eslintRange}`);
|
|
318
|
+
versionMapping['eslint'] = {
|
|
319
|
+
version: targetEslintVersion,
|
|
320
|
+
required: false,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
catch (e) {
|
|
325
|
+
if (verbose) {
|
|
326
|
+
console.warn(` Could not determine eslint peer range from eslint-config-next@${targetNextVersion}. Leaving eslint version alone.`, e);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
287
330
|
// Even though we only need those if we alias `@types/react` to types-react,
|
|
288
331
|
// we still do it out of safety due to https://github.com/microsoft/DefinitelyTyped-tools/issues/433.
|
|
289
332
|
const overrides = {};
|
|
@@ -320,10 +363,11 @@ async function runUpgrade(revision, options) {
|
|
|
320
363
|
// understanding of the codemods, we run all of the applicable codemods.
|
|
321
364
|
if (shouldRunReactCodemods) {
|
|
322
365
|
// https://react.dev/blog/2024/04/25/react-19-upgrade-guide#run-all-react-19-codemods
|
|
323
|
-
(0, child_process_1.execSync)(
|
|
324
366
|
// `--no-interactive` skips the interactive prompt that asks for confirmation
|
|
325
367
|
// https://github.com/codemod-com/codemod/blob/c0cf00d13161a0ec0965b6cc6bc5d54076839cc8/apps/cli/src/flags.ts#L160
|
|
326
|
-
|
|
368
|
+
// `--allow-dirty` is required because the upgrade above modified package.json
|
|
369
|
+
// and the lockfile; the recipe refuses to run on a dirty tree otherwise.
|
|
370
|
+
(0, child_process_1.execSync)(`${execCommand} codemod@latest react/19/migration-recipe --no-interactive --allow-dirty`, { stdio: 'inherit' });
|
|
327
371
|
}
|
|
328
372
|
if (shouldRunReactTypesCodemods) {
|
|
329
373
|
// https://react.dev/blog/2024/04/25/react-19-upgrade-guide#typescript-changes
|
|
@@ -337,6 +381,14 @@ async function runUpgrade(revision, options) {
|
|
|
337
381
|
if (codemods.length > 0) {
|
|
338
382
|
console.log(`${picocolors_1.default.green('✔')} Codemods have been applied successfully.`);
|
|
339
383
|
}
|
|
384
|
+
try {
|
|
385
|
+
if ((0, agents_md_1.refreshAgentRulesBlock)(cwd) === 'refreshed') {
|
|
386
|
+
console.log(`${picocolors_1.default.green('✔')} Refreshed the managed agent-rules block in AGENTS.md / CLAUDE.md to match the upgraded Next.js.`);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
// The block refresh is best-effort — never fail the upgrade over it.
|
|
391
|
+
}
|
|
340
392
|
warnDependenciesOutOfRange(appPackageJson, versionMapping);
|
|
341
393
|
endMessage(targetNextVersion);
|
|
342
394
|
}
|
|
@@ -383,7 +435,7 @@ function isUsingAppDir(projectPath) {
|
|
|
383
435
|
* 3. Otherwise, we ask the user to manually add `--turbopack` to their dev command,
|
|
384
436
|
* showing the current dev command as the initial value.
|
|
385
437
|
*/
|
|
386
|
-
async function suggestTurbopack(packageJson, targetNextVersion) {
|
|
438
|
+
async function suggestTurbopack(packageJson, targetNextVersion, nonInteractive) {
|
|
387
439
|
const devScript = packageJson.scripts?.['dev'];
|
|
388
440
|
// Turbopack flag was changed from `--turbo` to `--turbopack` in v15.0.1-canary.3
|
|
389
441
|
// PR: https://github.com/vercel/next.js/pull/71657
|
|
@@ -406,19 +458,28 @@ async function suggestTurbopack(packageJson, targetNextVersion) {
|
|
|
406
458
|
}
|
|
407
459
|
return;
|
|
408
460
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
461
|
+
let enable = true;
|
|
462
|
+
if (!nonInteractive) {
|
|
463
|
+
const responseTurbopack = await (0, prompts_1.default)({
|
|
464
|
+
type: 'confirm',
|
|
465
|
+
name: 'enable',
|
|
466
|
+
message: `Enable Turbopack for ${picocolors_1.default.bold('next dev')}?`,
|
|
467
|
+
initial: true,
|
|
468
|
+
}, { onCancel: utils_1.onCancel });
|
|
469
|
+
enable = responseTurbopack.enable;
|
|
470
|
+
}
|
|
471
|
+
if (!enable) {
|
|
416
472
|
return;
|
|
417
473
|
}
|
|
418
474
|
packageJson.scripts['dev'] = devScript.replace('next dev', `next dev ${turboPackFlag}`);
|
|
419
475
|
return;
|
|
420
476
|
}
|
|
421
477
|
console.log(`${picocolors_1.default.yellow('⚠')} Could not find "${picocolors_1.default.bold('next dev')}" in your dev script.`);
|
|
478
|
+
if (nonInteractive) {
|
|
479
|
+
// Without a TTY we can't ask the user for a replacement script.
|
|
480
|
+
// Keep the existing dev script untouched.
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
422
483
|
const responseCustomDevScript = await (0, prompts_1.default)({
|
|
423
484
|
type: 'text',
|
|
424
485
|
name: 'customDevScript',
|
|
@@ -428,7 +489,7 @@ async function suggestTurbopack(packageJson, targetNextVersion) {
|
|
|
428
489
|
packageJson.scripts['dev'] =
|
|
429
490
|
responseCustomDevScript.customDevScript || devScript;
|
|
430
491
|
}
|
|
431
|
-
async function suggestCodemods(initialNextVersion, targetNextVersion) {
|
|
492
|
+
async function suggestCodemods(initialNextVersion, targetNextVersion, nonInteractive) {
|
|
432
493
|
// example:
|
|
433
494
|
// codemod version: 15.0.0-canary.45
|
|
434
495
|
// 14.3 -> 15.0.0-canary.45: apply
|
|
@@ -451,6 +512,13 @@ async function suggestCodemods(initialNextVersion, targetNextVersion) {
|
|
|
451
512
|
if (relevantCodemods.length === 0) {
|
|
452
513
|
return [];
|
|
453
514
|
}
|
|
515
|
+
if (nonInteractive) {
|
|
516
|
+
// Default: apply every recommended codemod, matching `selected: true` below.
|
|
517
|
+
const all = relevantCodemods.map(({ value }) => value);
|
|
518
|
+
console.log(` Applying all ${picocolors_1.default.blue('codemods')} recommended for your upgrade:\n` +
|
|
519
|
+
all.map((value) => ` - ${value}`).join('\n'));
|
|
520
|
+
return all;
|
|
521
|
+
}
|
|
454
522
|
const { codemods } = await (0, prompts_1.default)({
|
|
455
523
|
type: 'multiselect',
|
|
456
524
|
name: 'codemods',
|
|
@@ -466,7 +534,10 @@ async function suggestCodemods(initialNextVersion, targetNextVersion) {
|
|
|
466
534
|
}, { onCancel: utils_1.onCancel });
|
|
467
535
|
return codemods;
|
|
468
536
|
}
|
|
469
|
-
async function suggestReactCodemods() {
|
|
537
|
+
async function suggestReactCodemods(nonInteractive) {
|
|
538
|
+
if (nonInteractive) {
|
|
539
|
+
return true;
|
|
540
|
+
}
|
|
470
541
|
const { runReactCodemod } = await (0, prompts_1.default)({
|
|
471
542
|
type: 'confirm',
|
|
472
543
|
name: 'runReactCodemod',
|
|
@@ -475,7 +546,10 @@ async function suggestReactCodemods() {
|
|
|
475
546
|
}, { onCancel: utils_1.onCancel });
|
|
476
547
|
return runReactCodemod;
|
|
477
548
|
}
|
|
478
|
-
async function suggestReactTypesCodemods() {
|
|
549
|
+
async function suggestReactTypesCodemods(nonInteractive) {
|
|
550
|
+
if (nonInteractive) {
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
479
553
|
const { runReactTypesCodemod } = await (0, prompts_1.default)({
|
|
480
554
|
type: 'confirm',
|
|
481
555
|
name: 'runReactTypesCodemod',
|
package/lib/agents-md.js
CHANGED
|
@@ -9,6 +9,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
9
9
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.refreshAgentRulesBlock = refreshAgentRulesBlock;
|
|
12
13
|
exports.getNextjsVersion = getNextjsVersion;
|
|
13
14
|
exports.getBundledDocsInfo = getBundledDocsInfo;
|
|
14
15
|
exports.getBundledDocsLinkPath = getBundledDocsLinkPath;
|
|
@@ -22,6 +23,49 @@ const execa_1 = __importDefault(require("execa"));
|
|
|
22
23
|
const fs_1 = __importDefault(require("fs"));
|
|
23
24
|
const path_1 = __importDefault(require("path"));
|
|
24
25
|
const os_1 = __importDefault(require("os"));
|
|
26
|
+
const AGENT_RULES_START_MARKER = '<!-- BEGIN:nextjs-agent-rules -->';
|
|
27
|
+
/**
|
|
28
|
+
* After an upgrade, refresh the managed agent-rules block in
|
|
29
|
+
* AGENTS.md / CLAUDE.md so its content matches the Next.js version
|
|
30
|
+
* that is now installed.
|
|
31
|
+
*
|
|
32
|
+
* Delegates to the installed package's own generator
|
|
33
|
+
* (`next/dist/server/lib/generate-agent-files`), so the block text is
|
|
34
|
+
* always the one shipped with that version — this codemod never
|
|
35
|
+
* carries its own copy. Returns `'refreshed'` when a file was
|
|
36
|
+
* rewritten, `'current'` when the block was already up to date, and
|
|
37
|
+
* `'skipped'` when there is nothing to do: the project never adopted
|
|
38
|
+
* the managed block, or the installed Next.js predates the generator
|
|
39
|
+
* (< 16.3).
|
|
40
|
+
*/
|
|
41
|
+
function refreshAgentRulesBlock(cwd) {
|
|
42
|
+
const hostsBlock = ['AGENTS.md', 'CLAUDE.md'].some((file) => {
|
|
43
|
+
try {
|
|
44
|
+
return fs_1.default
|
|
45
|
+
.readFileSync(path_1.default.join(cwd, file), 'utf-8')
|
|
46
|
+
.includes(AGENT_RULES_START_MARKER);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
if (!hostsBlock)
|
|
53
|
+
return 'skipped';
|
|
54
|
+
let writeAgentFiles;
|
|
55
|
+
try {
|
|
56
|
+
const generatorPath = require.resolve('next/dist/server/lib/generate-agent-files', { paths: [cwd] });
|
|
57
|
+
writeAgentFiles = require(generatorPath).writeAgentFiles;
|
|
58
|
+
if (typeof writeAgentFiles !== 'function')
|
|
59
|
+
return 'skipped';
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return 'skipped';
|
|
63
|
+
}
|
|
64
|
+
const result = writeAgentFiles(cwd);
|
|
65
|
+
return result.agentsMd === 'updated' || result.claudeMd === 'updated'
|
|
66
|
+
? 'refreshed'
|
|
67
|
+
: 'current';
|
|
68
|
+
}
|
|
25
69
|
function getNextjsVersion(cwd) {
|
|
26
70
|
try {
|
|
27
71
|
const nextPkgPath = require.resolve('next/package.json', { paths: [cwd] });
|