Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(next-upgrade): ensures prompts are cancelled when user exits #70930

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions packages/next-codemod/bin/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import globby from 'globby'
import prompts from 'prompts'
import { join } from 'node:path'
import { installPackages, uninstallPackage } from '../lib/handle-package'
import { checkGitStatus, TRANSFORMER_INQUIRER_CHOICES } from '../lib/utils'
import {
checkGitStatus,
onCancel,
TRANSFORMER_INQUIRER_CHOICES,
} from '../lib/utils'

function expandFilePathsIfNeeded(filesBeforeExpansion) {
const shouldExpandFiles = filesBeforeExpansion.some((file) =>
Expand Down Expand Up @@ -41,22 +45,28 @@ export async function runTransform(
}

if (!path) {
const res = await prompts({
type: 'text',
name: 'path',
message: 'On which files or directory should the codemods be applied?',
initial: '.',
})
const res = await prompts(
{
type: 'text',
name: 'path',
message: 'On which files or directory should the codemods be applied?',
initial: '.',
},
{ onCancel }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if we should create a cancellablePrompt wrapper? This seems fine for now

)

directory = res.path
}
if (!transform) {
const res = await prompts({
type: 'select',
name: 'transformer',
message: 'Which transform would you like to apply?',
choices: TRANSFORMER_INQUIRER_CHOICES,
})
const res = await prompts(
{
type: 'select',
name: 'transformer',
message: 'Which transform would you like to apply?',
choices: TRANSFORMER_INQUIRER_CHOICES,
},
{ onCancel }
)

transformer = res.transformer
}
Expand Down
28 changes: 12 additions & 16 deletions packages/next-codemod/bin/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chalk from 'chalk'
import { availableCodemods } from '../lib/codemods'
import { getPkgManager, installPackages } from '../lib/handle-package'
import { runTransform } from './transform'
import { onCancel } from '../lib/utils'

type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun'

Expand Down Expand Up @@ -162,11 +163,7 @@ async function suggestTurbopack(packageJson: any): Promise<void> {
message: 'Turbopack is now the stable default for dev mode. Enable it?',
initial: true,
},
{
onCancel: () => {
process.exit(0)
},
}
{ onCancel }
)

if (!responseTurbopack.enable) {
Expand All @@ -181,12 +178,15 @@ async function suggestTurbopack(packageJson: any): Promise<void> {
return
}

const responseCustomDevScript = await prompts({
type: 'text',
name: 'customDevScript',
message: 'Please add `--turbo` to your dev command:',
initial: devScript,
})
const responseCustomDevScript = await prompts(
{
type: 'text',
name: 'customDevScript',
message: 'Please add `--turbo` to your dev command:',
initial: devScript,
},
{ onCancel }
)

packageJson.scripts['dev'] =
responseCustomDevScript.customDevScript || devScript
Expand Down Expand Up @@ -233,11 +233,7 @@ async function suggestCodemods(
}
}),
},
{
onCancel: () => {
process.exit(0)
},
}
{ onCancel }
)

return codemods
Expand Down
4 changes: 4 additions & 0 deletions packages/next-codemod/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export function checkGitStatus(force) {
}
}

export function onCancel() {
process.exit(1)
}

export const TRANSFORMER_INQUIRER_CHOICES = [
{
title:
Expand Down
Loading