Skip to content

Commit

Permalink
refactor: ensures prompts are cancelled when user exits
Browse files Browse the repository at this point in the history
  • Loading branch information
devjiwonchoi committed Oct 7, 2024
1 parent 1183c00 commit 80d5b0d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
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 }
)

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

0 comments on commit 80d5b0d

Please sign in to comment.