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

fix: catch pnpm error and give user choose fix or exit #123

Merged
merged 1 commit into from
Dec 23, 2024
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
15 changes: 9 additions & 6 deletions src/actions/upgrade-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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, '')}`;
}, '')}`
)
);
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/helpers/actions/upgrade/catch-pnpm-exec.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>) {
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();
}
}
}
3 changes: 2 additions & 1 deletion src/helpers/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -12,7 +13,7 @@ export async function debugExecAddAction(cmd: string, components: string[] = [])
Logger.warn(`Debug: ${component}`);
}
} else {
await exec(cmd);
await catchPnpmExec(() => exec(cmd));
}
}

Expand Down
Loading