@next/codemod 16.5.0-canary.9 → 16.5.0-preview.0

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/upgrade.js CHANGED
@@ -56,6 +56,7 @@ const optionalNextjsPackages = [
56
56
  '@next/codemod',
57
57
  '@next/env',
58
58
  '@next/eslint-plugin-next',
59
+ '@next/mcp',
59
60
  '@next/mdx',
60
61
  '@next/plugin-storybook',
61
62
  '@next/polyfill-module',
@@ -4,9 +4,22 @@ jest.autoMockOff()
4
4
  const fs = require('fs')
5
5
  const path = require('path')
6
6
  const os = require('os')
7
+ const { execFileSync } = require('child_process')
7
8
  const { runAgentsMd } = require('../../bin/agents-md')
8
9
  const { getNextjsVersion } = require('../../lib/agents-md')
9
10
 
11
+ function execNodeWithoutNodePath(script, options = {}) {
12
+ // pnpm 12's Jest launcher exposes hoisted dependencies through NODE_PATH.
13
+ // A fresh Node process must omit it to resolve only the fixture's dependencies.
14
+ const env = { ...process.env, ...options.env }
15
+ delete env.NODE_PATH
16
+ return execFileSync(process.execPath, ['-e', script], {
17
+ ...options,
18
+ env,
19
+ encoding: 'utf8',
20
+ })
21
+ }
22
+
10
23
  /**
11
24
  * TRUE E2E TESTS
12
25
  * These tests invoke the actual CLI entry point (runAgentsMd),
@@ -14,6 +27,12 @@ const { getNextjsVersion } = require('../../lib/agents-md')
14
27
  * npx @next/codemod agents-md --version 15.0.0 --output AGENTS.md
15
28
  */
16
29
  describe('agents-md e2e (CLI invocation)', () => {
30
+ // A stable cwd to return to after every test. Tests chdir into their temp
31
+ // project, and the cleanup below deletes it. If a test times out, its body
32
+ // keeps running and never restores the cwd itself, so without this every
33
+ // later test would start in (and capture as its "original" cwd) a
34
+ // directory that no longer exists.
35
+ const suiteCwd = process.cwd()
17
36
  let testProjectDir
18
37
  let originalConsoleLog
19
38
  let consoleOutput
@@ -39,6 +58,8 @@ describe('agents-md e2e (CLI invocation)', () => {
39
58
  // Restore console.log
40
59
  console.log = originalConsoleLog
41
60
 
61
+ process.chdir(suiteCwd)
62
+
42
63
  // Clean up test directory
43
64
  if (testProjectDir && fs.existsSync(testProjectDir)) {
44
65
  fs.rmSync(testProjectDir, { recursive: true, force: true })
@@ -287,7 +308,7 @@ This is my project documentation.
287
308
  } finally {
288
309
  process.chdir(originalCwd)
289
310
  }
290
- }, 30000) // Increase timeout for git clone
311
+ })
291
312
 
292
313
  describe('bundled docs (Next.js >= 16.2.0)', () => {
293
314
  // Simulate an install of a Next.js version that ships docs inside the
@@ -382,12 +403,24 @@ This is my project documentation.
382
403
  it('falls back to downloading when --version differs from the installed version', async () => {
383
404
  setupBundledNext(testProjectDir, '16.2.0')
384
405
 
406
+ // This test is about choosing the download path, not about the download
407
+ // itself (covered by the tests above), so stub it to stay offline.
408
+ const pullDocsSpy = jest
409
+ .spyOn(require('../../lib/agents-md'), 'pullDocs')
410
+ .mockImplementation(async ({ docsDir }) => {
411
+ fs.mkdirSync(path.join(docsDir, '01-app'), { recursive: true })
412
+ fs.writeFileSync(path.join(docsDir, '01-app', 'index.md'), '# Docs')
413
+ return { success: true, docsPath: docsDir }
414
+ })
415
+
385
416
  const originalCwd = process.cwd()
386
417
  process.chdir(testProjectDir)
387
418
 
388
419
  try {
389
420
  await runAgentsMd({ version: '15.0.0', output: 'AGENTS.md' })
390
421
 
422
+ expect(pullDocsSpy).toHaveBeenCalledTimes(1)
423
+ expect(pullDocsSpy.mock.calls[0][0].version).toBe('15.0.0')
391
424
  expect(fs.existsSync(path.join(testProjectDir, '.next-docs'))).toBe(
392
425
  true
393
426
  )
@@ -402,8 +435,9 @@ This is my project documentation.
402
435
  expect(output).toContain('Downloading')
403
436
  } finally {
404
437
  process.chdir(originalCwd)
438
+ pullDocsSpy.mockRestore()
405
439
  }
406
- }, 30000) // Increase timeout for git clone
440
+ })
407
441
  })
408
442
 
409
443
  // The index `root` is resolved by agents from the directory of the output
@@ -711,11 +745,25 @@ This is my project documentation.
711
745
  return require('strip-ansi')(consoleOutput.join('\n'))
712
746
  }
713
747
 
714
- it('uses the bundled docs of a workspace package when run from the root', async () => {
748
+ function runFromWorkspaceRoot() {
749
+ return require('strip-ansi')(
750
+ execNodeWithoutNodePath(
751
+ `const { runAgentsMd } = require(${JSON.stringify(
752
+ path.join(__dirname, '../../bin/agents-md.js')
753
+ )})
754
+ runAgentsMd({ output: 'AGENTS.md' }).catch((error) => {
755
+ console.error(error)
756
+ process.exitCode = 1
757
+ })`,
758
+ { cwd: testProjectDir }
759
+ )
760
+ )
761
+ }
762
+
763
+ it('uses the bundled docs of a workspace package when run from the root', () => {
715
764
  setupWorkspace(['apps/*'], { 'apps/web': '16.4.0' })
716
- process.chdir(testProjectDir)
717
765
 
718
- await runAgentsMd({ output: 'AGENTS.md' })
766
+ const cliOutput = runFromWorkspaceRoot()
719
767
 
720
768
  const agentsMdPath = path.join(testProjectDir, 'AGENTS.md')
721
769
  expect(readIndex(agentsMdPath).root).toBe(
@@ -724,49 +772,49 @@ This is my project documentation.
724
772
  expect(
725
773
  fs.existsSync(path.join(resolveRoot(agentsMdPath), '01-app'))
726
774
  ).toBe(true)
727
- expect(pullDocsSpy).not.toHaveBeenCalled()
728
775
  expect(fs.existsSync(path.join(testProjectDir, '.next-docs'))).toBe(
729
776
  false
730
777
  )
731
778
  expect(fs.existsSync(path.join(testProjectDir, '.gitignore'))).toBe(
732
779
  false
733
780
  )
734
- expect(output()).toContain('workspace package apps/web')
781
+ expect(cliOutput).toContain('workspace package apps/web')
735
782
  })
736
783
 
737
- it('indexes the package with the highest Next.js version', async () => {
784
+ it('indexes the package with the highest Next.js version', () => {
738
785
  setupWorkspace(['apps/a', 'apps/b'], {
739
786
  'apps/a': '16.3.0',
740
787
  'apps/b': '16.4.0',
741
788
  })
742
- process.chdir(testProjectDir)
743
789
 
744
- await runAgentsMd({ output: 'AGENTS.md' })
790
+ const cliOutput = runFromWorkspaceRoot()
745
791
 
746
792
  const { root, content } = readIndex(
747
793
  path.join(testProjectDir, 'AGENTS.md')
748
794
  )
749
795
  expect(root).toBe('./apps/b/node_modules/next/dist/docs')
750
796
  expect(content).toContain('docs-16.4.0.md')
751
- expect(pullDocsSpy).not.toHaveBeenCalled()
797
+ expect(cliOutput).not.toContain('Downloading')
798
+ expect(fs.existsSync(path.join(testProjectDir, '.next-docs'))).toBe(
799
+ false
800
+ )
752
801
  })
753
802
 
754
- it('picks the first matching package in workspace order and logs it', async () => {
803
+ it('picks the first matching package in workspace order and logs it', () => {
755
804
  // Listed b before a, so the choice follows the workspace patterns
756
805
  // rather than alphabetical order.
757
806
  setupWorkspace(['apps/b', 'apps/a'], {
758
807
  'apps/a': '16.4.0',
759
808
  'apps/b': '16.4.0',
760
809
  })
761
- process.chdir(testProjectDir)
762
810
 
763
- await runAgentsMd({ output: 'AGENTS.md' })
811
+ const cliOutput = runFromWorkspaceRoot()
764
812
 
765
813
  expect(readIndex(path.join(testProjectDir, 'AGENTS.md')).root).toBe(
766
814
  './apps/b/node_modules/next/dist/docs'
767
815
  )
768
- expect(output()).toContain('workspace package apps/b')
769
- expect(output()).not.toContain('workspace package apps/a')
816
+ expect(cliOutput).toContain('workspace package apps/b')
817
+ expect(cliOutput).not.toContain('workspace package apps/a')
770
818
  })
771
819
 
772
820
  it('prefers a matching install resolved from the cwd', async () => {
@@ -840,9 +888,15 @@ This is my project documentation.
840
888
  })
841
889
 
842
890
  it('returns error when Next.js is not installed', () => {
843
- // Use a directory where next is not installed
844
- const nonNextDir = '/tmp'
845
- const result = getNextjsVersion(nonNextDir)
891
+ const result = JSON.parse(
892
+ execNodeWithoutNodePath(
893
+ `const { getNextjsVersion } = require(${JSON.stringify(
894
+ path.join(__dirname, '../agents-md.js')
895
+ )})
896
+ console.log(JSON.stringify(getNextjsVersion(process.cwd())))`,
897
+ { cwd: testProjectDir }
898
+ )
899
+ )
846
900
 
847
901
  expect(result.version).toBeNull()
848
902
  expect(result.error).toBe('Next.js is not installed in this project.')
@@ -27,6 +27,8 @@ function transformer(file, _api, options) {
27
27
  resolvedPath = path_1.default.resolve(path_1.default.dirname(file.path), value);
28
28
  }
29
29
  exports.globalCssContext.cssImports.add(resolvedPath);
30
+ // TODO(as-any): pre-existing cast, not yet reviewed
31
+ // eslint-disable-next-line @next/internal/no-as-any
30
32
  const { start, end } = path.node;
31
33
  if (!path.parentPath.node.comments) {
32
34
  path.parentPath.node.comments = [];
@@ -39,6 +41,8 @@ function transformer(file, _api, options) {
39
41
  }
40
42
  else if (value.endsWith('.svg')) {
41
43
  const isComponentImport = path.node.specifiers.some((specifier) => {
44
+ // TODO(as-any): pre-existing cast, not yet reviewed
45
+ // eslint-disable-next-line @next/internal/no-as-any
42
46
  return specifier.imported?.name === 'ReactComponent';
43
47
  });
44
48
  if (isComponentImport) {
@@ -34,10 +34,16 @@ function transformer(file, _api, options) {
34
34
  let found = false;
35
35
  if (defaultReactDomImport &&
36
36
  node.callee.type === 'MemberExpression' &&
37
+ // TODO(as-any): pre-existing cast, not yet reviewed
38
+ // eslint-disable-next-line @next/internal/no-as-any
37
39
  node.callee.object.name === defaultReactDomImport &&
40
+ // TODO(as-any): pre-existing cast, not yet reviewed
41
+ // eslint-disable-next-line @next/internal/no-as-any
38
42
  node.callee.property.name === 'render') {
39
43
  found = true;
40
44
  }
45
+ // TODO(as-any): pre-existing cast, not yet reviewed
46
+ // eslint-disable-next-line @next/internal/no-as-any
41
47
  if (hasRenderImport && node.callee.name === 'render') {
42
48
  found = true;
43
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next/codemod",
3
- "version": "16.5.0-canary.9",
3
+ "version": "16.5.0-preview.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,7 +28,9 @@
28
28
  "lib/**/*.js",
29
29
  "lib/cra-to-next/gitignore"
30
30
  ],
31
- "bin": "./bin/next-codemod.js",
31
+ "bin": {
32
+ "codemod": "./bin/next-codemod.js"
33
+ },
32
34
  "devDependencies": {
33
35
  "@types/find-up": "4.0.0",
34
36
  "@types/js-yaml": "4.0.9",
@@ -122,9 +122,17 @@ function replaceFlatCompatInConfig(configPath) {
122
122
  const node = astPath.value;
123
123
  // Detect compat.extends() calls and identify which configs are being used
124
124
  if (node.callee.type === 'MemberExpression' &&
125
+ // TODO(as-any): pre-existing cast, not yet reviewed
126
+ // eslint-disable-next-line @next/internal/no-as-any
125
127
  node.callee.object.type === 'Identifier' &&
128
+ // TODO(as-any): pre-existing cast, not yet reviewed
129
+ // eslint-disable-next-line @next/internal/no-as-any
126
130
  node.callee.object.name === 'compat' &&
131
+ // TODO(as-any): pre-existing cast, not yet reviewed
132
+ // eslint-disable-next-line @next/internal/no-as-any
127
133
  node.callee.property.type === 'Identifier' &&
134
+ // TODO(as-any): pre-existing cast, not yet reviewed
135
+ // eslint-disable-next-line @next/internal/no-as-any
128
136
  node.callee.property.name === 'extends') {
129
137
  // Check arguments for all configs
130
138
  node.arguments.forEach((arg) => {
@@ -147,9 +155,17 @@ function replaceFlatCompatInConfig(configPath) {
147
155
  }
148
156
  // Detect compat.config({ extends: [...] }) calls and identify which configs are being used
149
157
  if (node.callee.type === 'MemberExpression' &&
158
+ // TODO(as-any): pre-existing cast, not yet reviewed
159
+ // eslint-disable-next-line @next/internal/no-as-any
150
160
  node.callee.object.type === 'Identifier' &&
161
+ // TODO(as-any): pre-existing cast, not yet reviewed
162
+ // eslint-disable-next-line @next/internal/no-as-any
151
163
  node.callee.object.name === 'compat' &&
164
+ // TODO(as-any): pre-existing cast, not yet reviewed
165
+ // eslint-disable-next-line @next/internal/no-as-any
152
166
  node.callee.property.type === 'Identifier' &&
167
+ // TODO(as-any): pre-existing cast, not yet reviewed
168
+ // eslint-disable-next-line @next/internal/no-as-any
153
169
  node.callee.property.name === 'config') {
154
170
  // Look for extends property in the object argument
155
171
  node.arguments.forEach((arg) => {