Unverified Commit ed10d58e authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

refactor: improve transformer selection prompt (#70932)

### Why?

Previously, we were showing ALL the descriptions of the codemods which
made hard to read each lines of codemods.

### How?

Show description only of current highlighted option. Also, display the
version of the codemod.

### Before

`@next/codemod`


https://github.com/user-attachments/assets/2537c59e-c4a8-4cdd-af34-68cabf56354b

`@next/codemod upgrade`


https://github.com/user-attachments/assets/0fc05776-b398-4e13-b6ea-1e351fff2194

### After

`@next/codemod`


https://github.com/user-attachments/assets/553e5952-bd2e-4a19-99b0-e3fdab10d868

`@next/codemod upgrade`


https://github.com/user-attachments/assets/375c9abc-da3e-4c23-a03d-12def4c6d2a3

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 78b73f03
...@@ -63,7 +63,15 @@ export async function runTransform( ...@@ -63,7 +63,15 @@ export async function runTransform(
type: 'select', type: 'select',
name: 'transformer', name: 'transformer',
message: 'Which transform would you like to apply?', message: 'Which transform would you like to apply?',
choices: TRANSFORMER_INQUIRER_CHOICES, choices: TRANSFORMER_INQUIRER_CHOICES.reverse().map(
({ title, value, version }) => {
return {
title: `(v${version}) ${value}`,
description: title,
value,
}
}
),
}, },
{ onCancel } { onCancel }
) )
......
...@@ -4,10 +4,9 @@ import { execSync } from 'child_process' ...@@ -4,10 +4,9 @@ import { execSync } from 'child_process'
import path from 'path' import path from 'path'
import { compareVersions } from 'compare-versions' import { compareVersions } from 'compare-versions'
import chalk from 'chalk' import chalk from 'chalk'
import { availableCodemods } from '../lib/codemods'
import { getPkgManager, installPackages } from '../lib/handle-package' import { getPkgManager, installPackages } from '../lib/handle-package'
import { runTransform } from './transform' import { runTransform } from './transform'
import { onCancel } from '../lib/utils' import { onCancel, TRANSFORMER_INQUIRER_CHOICES } from '../lib/utils'
type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun' type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun'
...@@ -200,7 +199,7 @@ async function suggestCodemods( ...@@ -200,7 +199,7 @@ async function suggestCodemods(
initialNextVersion: string, initialNextVersion: string,
targetNextVersion: string targetNextVersion: string
): Promise<string[]> { ): Promise<string[]> {
const initialVersionIndex = availableCodemods.findIndex( const initialVersionIndex = TRANSFORMER_INQUIRER_CHOICES.findIndex(
(versionCodemods) => (versionCodemods) =>
compareVersions(versionCodemods.version, initialNextVersion) > 0 compareVersions(versionCodemods.version, initialNextVersion) > 0
) )
...@@ -208,17 +207,18 @@ async function suggestCodemods( ...@@ -208,17 +207,18 @@ async function suggestCodemods(
return [] return []
} }
let targetVersionIndex = availableCodemods.findIndex( let targetVersionIndex = TRANSFORMER_INQUIRER_CHOICES.findIndex(
(versionCodemods) => (versionCodemods) =>
compareVersions(versionCodemods.version, targetNextVersion) > 0 compareVersions(versionCodemods.version, targetNextVersion) > 0
) )
if (targetVersionIndex === -1) { if (targetVersionIndex === -1) {
targetVersionIndex = availableCodemods.length targetVersionIndex = TRANSFORMER_INQUIRER_CHOICES.length
} }
const relevantCodemods = availableCodemods const relevantCodemods = TRANSFORMER_INQUIRER_CHOICES.slice(
.slice(initialVersionIndex, targetVersionIndex) initialVersionIndex,
.flatMap((versionCodemods) => versionCodemods.codemods) targetVersionIndex
)
if (relevantCodemods.length === 0) { if (relevantCodemods.length === 0) {
return [] return []
...@@ -229,10 +229,11 @@ async function suggestCodemods( ...@@ -229,10 +229,11 @@ async function suggestCodemods(
type: 'multiselect', type: 'multiselect',
name: 'codemods', name: 'codemods',
message: `\nThe following ${chalk.blue('codemods')} are recommended for your upgrade. Would you like to apply them?`, message: `\nThe following ${chalk.blue('codemods')} are recommended for your upgrade. Would you like to apply them?`,
choices: relevantCodemods.map((codemod) => { choices: relevantCodemods.reverse().map(({ title, value, version }) => {
return { return {
title: `${codemod.title} ${chalk.grey(`(${codemod.value})`)}`, title: `(v${version}) ${value}`,
value: codemod.value, description: title,
value,
selected: true, selected: true,
} }
}), }),
......
type Codemod = {
title: string
value: string
}
type VersionCodemods = {
version: string
codemods: Codemod[]
}
export const availableCodemods: VersionCodemods[] = [
{
version: '6',
codemods: [
{
title: 'Use withRouter',
value: 'url-to-withrouter',
},
],
},
{
version: '8',
codemods: [
{
title: 'Transform AMP HOC into page config',
value: 'withamp-to-config',
},
],
},
{
version: '9',
codemods: [
{
title: 'Transform Anonymous Components into Named Components',
value: 'name-default-component',
},
],
},
{
version: '10',
codemods: [
{
title: 'Add React Import',
value: 'add-missing-react-import',
},
],
},
{
version: '11',
codemods: [
{
title: 'Migrate from CRA',
value: 'cra-to-next',
},
],
},
{
version: '13.0',
codemods: [
{
title: 'Remove <a> Tags From Link Components',
value: 'new-link',
},
{
title: 'Migrate to the New Image Component',
value: 'next-image-experimental',
},
{
title: 'Rename Next Image Imports',
value: 'next-image-to-legacy-image',
},
],
},
{
version: '13.2',
codemods: [
{
title: 'Use Built-in Font',
value: 'built-in-next-font',
},
],
},
{
version: '14.0',
codemods: [
{
title: 'Migrate ImageResponse imports',
value: 'next-og-import',
},
{
title: 'Use viewport export',
value: 'metadata-to-viewport-export',
},
],
},
{
version: '15.0.0-canary.153',
codemods: [
{
title: 'Migrate `geo` and `ip` properties on `NextRequest`',
value: 'next-request-geo-ip',
},
],
},
{
version: '15.0.0-canary.171',
codemods: [
{
title: 'Transforms usage of Next.js async Request APIs',
value: 'next-async-request-api',
},
],
},
]
...@@ -38,71 +38,82 @@ export function onCancel() { ...@@ -38,71 +38,82 @@ export function onCancel() {
export const TRANSFORMER_INQUIRER_CHOICES = [ export const TRANSFORMER_INQUIRER_CHOICES = [
{ {
title: title:
'name-default-component: Transforms anonymous components into named components to make sure they work with Fast Refresh', 'Transform the deprecated automatically injected url property on top level pages to using withRouter',
value: 'name-default-component', value: 'url-to-withrouter',
version: '6.0',
}, },
{ {
title: title: 'Transforms the withAmp HOC into Next.js 9 page configuration',
'add-missing-react-import: Transforms files that do not import `React` to include the import in order for the new React JSX transform', value: 'withamp-to-config',
value: 'add-missing-react-import', version: '8.0',
}, },
{ {
title: title:
'withamp-to-config: Transforms the withAmp HOC into Next.js 9 page configuration', 'Transforms anonymous components into named components to make sure they work with Fast Refresh',
value: 'withamp-to-config', value: 'name-default-component',
version: '9.0',
}, },
{ {
title: title:
'url-to-withrouter: Transforms the deprecated automatically injected url property on top level pages to using withRouter', 'Transforms files that do not import `React` to include the import in order for the new React JSX transform',
value: 'url-to-withrouter', value: 'add-missing-react-import',
version: '10.0',
}, },
{ {
title: title:
'cra-to-next (experimental): automatically migrates a Create React App project to Next.js', 'Automatically migrates a Create React App project to Next.js (experimental)',
value: 'cra-to-next', value: 'cra-to-next',
version: '11.0',
}, },
{ {
title: 'new-link: Ensures your <Link> usage is backwards compatible.', title: 'Ensures your <Link> usage is backwards compatible',
value: 'new-link', value: 'new-link',
version: '13.0',
}, },
{ {
title: title:
'next-og-import: Transforms imports from `next/server` to `next/og` for usage of Dynamic OG Image Generation.', 'Dangerously migrates from `next/legacy/image` to the new `next/image` by adding inline styles and removing unused props (experimental)',
value: 'next-og-import', value: 'next-image-experimental',
version: '13.0',
}, },
{ {
title: title:
'metadata-to-viewport-export: Migrates certain viewport related metadata from the `metadata` export to a new `viewport` export.', 'Safely migrate Next.js 10, 11, 12 applications importing `next/image` to the renamed `next/legacy/image` import in Next.js 13',
value: 'metadata-to-viewport-export', value: 'next-image-to-legacy-image',
version: '13.0',
}, },
{ {
title: title: 'Uninstall `@next/font` and transform imports to `next/font`',
'next-dynamic-access-named-export: Transforms dynamic imports that return the named export itself to a module like object.', value: 'built-in-next-font',
value: 'next-dynamic-access-named-export', version: '13.2',
}, },
{ {
title: title:
'next-image-to-legacy-image: safely migrate Next.js 10, 11, 12 applications importing `next/image` to the renamed `next/legacy/image` import in Next.js 13', 'Migrates certain viewport related metadata from the `metadata` export to a new `viewport` export',
value: 'next-image-to-legacy-image', value: 'metadata-to-viewport-export',
version: '14.0',
}, },
{ {
title: title:
'next-image-experimental (experimental): dangerously migrates from `next/legacy/image` to the new `next/image` by adding inline styles and removing unused props', 'Transforms imports from `next/server` to `next/og` for usage of Dynamic OG Image Generation',
value: 'next-image-experimental', value: 'next-og-import',
version: '14.0',
}, },
{ {
title: title:
'built-in-next-font: Uninstall `@next/font` and transform imports to `next/font`', 'Transforms dynamic imports that return the named export itself to a module like object',
value: 'built-in-next-font', value: 'next-dynamic-access-named-export',
version: '15.0.0-canary.44',
}, },
{ {
title: title:
'next-async-request-api: Transforms usage of Next.js async Request APIs', 'Install `@vercel/functions` to replace `geo` and `ip` properties on `NextRequest`',
value: 'next-async-request-api', value: 'next-request-geo-ip',
version: '15.0.0-canary.153',
}, },
{ {
title: title: 'Transforms usage of Next.js async Request APIs',
'next-request-geo-ip: Install `@vercel/functions` to replace `geo` and `ip` properties on `NextRequest`', value: 'next-async-request-api',
value: 'next-request-geo-ip', version: '15.0.0-canary.171',
}, },
] ]
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