-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
30 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
packages/create-react-native-library/src/utils/promiseWithFallback.ts
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
packages/create-react-native-library/src/utils/resolveNpmPackageVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |