diff --git a/src/actions/upgrade-action.ts b/src/actions/upgrade-action.ts index 92e9da0..4330432 100644 --- a/src/actions/upgrade-action.ts +++ b/src/actions/upgrade-action.ts @@ -2,6 +2,7 @@ import type {AppendKeyValue} from '@helpers/type'; import fs from 'node:fs'; +import {catchPnpmExec} from '@helpers/actions/upgrade/catch-pnpm-exec'; import {getBetaVersion} from '@helpers/beta'; import {checkIllegalComponents} from '@helpers/check'; import {detect} from '@helpers/detect'; @@ -223,12 +224,14 @@ export async function upgradeAction(components: string[], options: UpgradeAction Logger.success('✅ Upgrade version written to package.json'); process.exit(0); } else { - await exec( - `${packageManager} ${install} ${result.reduce((acc, component, index) => { - return `${acc}${index === 0 ? '' : ' '}${ - component.package - }@${component.latestVersion.replace(colorMatchRegex, '')}`; - }, '')}` + await catchPnpmExec(() => + exec( + `${packageManager} ${install} ${result.reduce((acc, component, index) => { + return `${acc}${index === 0 ? '' : ' '}${ + component.package + }@${component.latestVersion.replace(colorMatchRegex, '')}`; + }, '')}` + ) ); } } diff --git a/src/helpers/actions/upgrade/catch-pnpm-exec.ts b/src/helpers/actions/upgrade/catch-pnpm-exec.ts new file mode 100644 index 0000000..8ba32d3 --- /dev/null +++ b/src/helpers/actions/upgrade/catch-pnpm-exec.ts @@ -0,0 +1,28 @@ +import chalk from 'chalk'; + +import {exec} from '@helpers/exec'; +import {selectClack} from 'src/prompts/clack'; + +export async function catchPnpmExec(execFn: () => Promise) { + try { + await execFn(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } catch (error: any) { + if (error) { + const reRunPnpm = await selectClack({ + message: `${chalk.red('Error: ')}a unexpected error occurred, run "pnpm install" maybe can fix it or report it as a bug`, + options: [ + {label: 'Re-run pnpm install', value: 're-run-pnpm-install'}, + {label: 'Exit', value: 'exit'} + ] + }); + + if (reRunPnpm === 'exit') { + process.exit(1); + } + + await exec('pnpm install --force'); + await execFn(); + } + } +} diff --git a/src/helpers/debug.ts b/src/helpers/debug.ts index 0dba1b9..5941abf 100644 --- a/src/helpers/debug.ts +++ b/src/helpers/debug.ts @@ -2,6 +2,7 @@ import {writeFileSync} from 'node:fs'; import {getStoreSync, store} from 'src/constants/store'; +import {catchPnpmExec} from './actions/upgrade/catch-pnpm-exec'; import {exec} from './exec'; import {Logger} from './logger'; import {getPackageInfo} from './package'; @@ -12,7 +13,7 @@ export async function debugExecAddAction(cmd: string, components: string[] = []) Logger.warn(`Debug: ${component}`); } } else { - await exec(cmd); + await catchPnpmExec(() => exec(cmd)); } }