From 3b30a1ef4f0aee1e6f3d8a20809f5a95c97014fb Mon Sep 17 00:00:00 2001 From: Shima Ryuhei <65934663+islandryu@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:16:20 +0900 Subject: [PATCH] fix: Correctly display errors in react-native init (#2394) * fix: Display correct error when config file does not exist * fix: move cocoaPods Error * fix: prettify output * fix: pass an error * fix: do cleanup before exiting process --------- Co-authored-by: szymonrybczak --- packages/cli/src/commands/init/init.ts | 56 +++++++++++++++----------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/packages/cli/src/commands/init/init.ts b/packages/cli/src/commands/init/init.ts index 824daf4c9..f1bfffd34 100644 --- a/packages/cli/src/commands/init/init.ts +++ b/packages/cli/src/commands/init/init.ts @@ -291,43 +291,53 @@ async function createFromTemplate({ if (process.platform === 'darwin') { const installPodsValue = String(installCocoaPods); - if (installPodsValue === 'true') { - didInstallPods = true; - await installPods(loader); - loader.succeed(); - setEmptyHashForCachedDependencies(projectName); - } else if (installPodsValue === 'undefined') { - const {installCocoapods} = await prompt({ - type: 'confirm', - name: 'installCocoapods', - message: `Do you want to install CocoaPods now? ${chalk.reset.dim( - 'Only needed if you run your project in Xcode directly', - )}`, - }); - didInstallPods = installCocoapods; - - if (installCocoapods) { + try { + if (installPodsValue === 'true') { + didInstallPods = true; await installPods(loader); loader.succeed(); setEmptyHashForCachedDependencies(projectName); + } else if (installPodsValue === 'undefined') { + const {installCocoapods} = await prompt({ + type: 'confirm', + name: 'installCocoapods', + message: `Do you want to install CocoaPods now? ${chalk.reset.dim( + 'Only needed if you run your project in Xcode directly', + )}`, + }); + didInstallPods = installCocoapods; + + if (installCocoapods) { + await installPods(loader); + loader.succeed(); + setEmptyHashForCachedDependencies(projectName); + } } + } catch (error) { + logger.error( + `Installing Cocoapods failed. This doesn't affect project initialization and you can safely proceed. However, you will need to install Cocoapods manually when running iOS, follow additional steps in "Run instructions for iOS" section.\n\nError: ${ + (error as Error).message as string + }\n`, + ); } } } else { didInstallPods = false; loader.succeed('Dependencies installation skipped'); } + + fs.removeSync(templateSourceDir); } catch (e) { - loader.fail(); - if (e instanceof Error) { - logger.error( - 'Installing pods failed. This doesn\'t affect project initialization and you can safely proceed. \nHowever, you will need to install pods manually when running iOS, follow additional steps in "Run instructions for iOS" section.\n', - ); - logger.debug(e as any); + logger.log('\n'); + if (e instanceof CLIError) { + logger.error(e.message); + } else if (e instanceof Error) { + logger.error(`An unexpected error occurred: ${e.message}.`); } didInstallPods = false; - } finally { + logger.debug(e as any); fs.removeSync(templateSourceDir); + process.exit(1); } if (process.platform === 'darwin') {