Skip to content

Commit

Permalink
fix: don't crash if resolving version fails
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
satya164 committed Dec 8, 2024
1 parent 9c5638a commit a403f14
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
9 changes: 6 additions & 3 deletions packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -44,7 +44,10 @@ async function create(_argv: yargs.Arguments<Args>) {
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);
Expand Down Expand Up @@ -72,7 +75,7 @@ async function create(_argv: yargs.Arguments<Args>) {

assertUserInput(questions, answers);

const bobVersion = await resolveBobVersion();
const bobVersion = await bobVersionPromise;

const config = generateTemplateConfiguration({
bobVersion,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { spawn } from './spawn';

export async function resolveNpmPackageVersion(
name: string,
fallback: string,
timeout: number = 1000
): Promise<string> {
let result: string;

try {
const promise = spawn('npm', ['view', name, 'dist-tags.latest']);

result = await Promise.race([
new Promise<string>((resolve) => {
setTimeout(() => resolve(fallback), timeout);
}),
promise,
]);
} catch (e) {
result = fallback;
}

return result;
}

0 comments on commit a403f14

Please sign in to comment.