From a403f149d2c578b5dcf524951f29b2aa0baa9619 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Sun, 8 Dec 2024 19:41:17 +0100 Subject: [PATCH] fix: don't crash if resolving version fails when creating a new library, it fails if resolving the version failed for some reason. this is because the promise is created earlier, but the code with try catch is run way later. this moves the promise into try/catch so it's properly handled. --- .../create-react-native-library/src/index.ts | 9 ++++-- .../src/utils/promiseWithFallback.ts | 32 ------------------- .../src/utils/resolveNpmPackageVersion.ts | 24 ++++++++++++++ 3 files changed, 30 insertions(+), 35 deletions(-) delete mode 100644 packages/create-react-native-library/src/utils/promiseWithFallback.ts create mode 100644 packages/create-react-native-library/src/utils/resolveNpmPackageVersion.ts diff --git a/packages/create-react-native-library/src/index.ts b/packages/create-react-native-library/src/index.ts index 47d508660..dbf87992f 100644 --- a/packages/create-react-native-library/src/index.ts +++ b/packages/create-react-native-library/src/index.ts @@ -8,7 +8,7 @@ import generateExampleApp from './exampleApp/generateExampleApp'; import { addCodegenBuildScript } from './exampleApp/addCodegenBuildScript'; import { createInitialGitCommit } from './utils/initialCommit'; import { assertUserInput, assertNpxExists } from './utils/assert'; -import { resolveBobVersionWithFallback } from './utils/promiseWithFallback'; +import { resolveNpmPackageVersion } from './utils/resolveNpmPackageVersion'; import { applyTemplates, generateTemplateConfiguration } from './template'; import { createQuestions, @@ -44,7 +44,10 @@ async function create(_argv: yargs.Arguments) { const { _, $0, ...argv } = _argv; // Prefetch bob version in background while asking questions - const resolveBobVersion = resolveBobVersionWithFallback(FALLBACK_BOB_VERSION); + const bobVersionPromise = resolveNpmPackageVersion( + 'react-native-builder-bob', + FALLBACK_BOB_VERSION + ); const local = await promptLocalLibrary(argv); const folder = await promptPath(argv, local); @@ -72,7 +75,7 @@ async function create(_argv: yargs.Arguments) { assertUserInput(questions, answers); - const bobVersion = await resolveBobVersion(); + const bobVersion = await bobVersionPromise; const config = generateTemplateConfiguration({ bobVersion, diff --git a/packages/create-react-native-library/src/utils/promiseWithFallback.ts b/packages/create-react-native-library/src/utils/promiseWithFallback.ts deleted file mode 100644 index 305135395..000000000 --- a/packages/create-react-native-library/src/utils/promiseWithFallback.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { spawn } from './spawn'; - -export function promiseWithFallback( - promise: Promise, - fallback: T, - timeout: number -): () => Promise { - async function forceResolve() { - let result: T; - - try { - result = await Promise.race([ - new Promise((resolve) => { - setTimeout(() => resolve(fallback), timeout); - }), - promise, - ]); - } catch (e) { - result = fallback; - } - return result; - } - - return forceResolve; -} - -export const resolveBobVersionWithFallback = (fallback: string) => - promiseWithFallback( - spawn('npm', ['view', 'react-native-builder-bob', 'dist-tags.latest']), - fallback, - 1000 - ); diff --git a/packages/create-react-native-library/src/utils/resolveNpmPackageVersion.ts b/packages/create-react-native-library/src/utils/resolveNpmPackageVersion.ts new file mode 100644 index 000000000..43251dc0b --- /dev/null +++ b/packages/create-react-native-library/src/utils/resolveNpmPackageVersion.ts @@ -0,0 +1,24 @@ +import { spawn } from './spawn'; + +export async function resolveNpmPackageVersion( + name: string, + fallback: string, + timeout: number = 1000 +): Promise { + let result: string; + + try { + const promise = spawn('npm', ['view', name, 'dist-tags.latest']); + + result = await Promise.race([ + new Promise((resolve) => { + setTimeout(() => resolve(fallback), timeout); + }), + promise, + ]); + } catch (e) { + result = fallback; + } + + return result; +}