From 80d5b0d4c1ac5dc2c87684e0bd06537eb4ff0b53 Mon Sep 17 00:00:00 2001 From: devjiwonchoi Date: Tue, 8 Oct 2024 04:22:28 +0900 Subject: [PATCH] refactor: ensures prompts are cancelled when user exits --- packages/next-codemod/bin/transform.ts | 36 ++++++++++++++++---------- packages/next-codemod/bin/upgrade.ts | 28 +++++++++----------- packages/next-codemod/lib/utils.ts | 4 +++ 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/packages/next-codemod/bin/transform.ts b/packages/next-codemod/bin/transform.ts index 07f96deb1d317..ff36f5fb1a0c1 100644 --- a/packages/next-codemod/bin/transform.ts +++ b/packages/next-codemod/bin/transform.ts @@ -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) => @@ -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 } diff --git a/packages/next-codemod/bin/upgrade.ts b/packages/next-codemod/bin/upgrade.ts index 3a83d57084f7c..a8e2238200507 100644 --- a/packages/next-codemod/bin/upgrade.ts +++ b/packages/next-codemod/bin/upgrade.ts @@ -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' @@ -162,11 +163,7 @@ async function suggestTurbopack(packageJson: any): Promise { message: 'Turbopack is now the stable default for dev mode. Enable it?', initial: true, }, - { - onCancel: () => { - process.exit(0) - }, - } + { onCancel } ) if (!responseTurbopack.enable) { @@ -181,12 +178,15 @@ async function suggestTurbopack(packageJson: any): Promise { 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 @@ -233,11 +233,7 @@ async function suggestCodemods( } }), }, - { - onCancel: () => { - process.exit(0) - }, - } + { onCancel } ) return codemods diff --git a/packages/next-codemod/lib/utils.ts b/packages/next-codemod/lib/utils.ts index 38f5bf4e86aa7..0e8aa3066fbe1 100644 --- a/packages/next-codemod/lib/utils.ts +++ b/packages/next-codemod/lib/utils.ts @@ -31,6 +31,10 @@ export function checkGitStatus(force) { } } +export function onCancel() { + process.exit(1) +} + export const TRANSFORMER_INQUIRER_CHOICES = [ { title: