Skip to content

Commit 1b0e033

Browse files
authored
feat(tools-react-native): add additional platform helpers (#3490)
* add additional platform helpers to tools-react-native * docs(changeset): Add additional platform helpers for react-native related to platform names and suffixes * update readme file for tools-react-native * add visionos as a valid platform in tools-react-native * remove package lookup table and supporting functions, will do this another way * update readme * rename parsePlatformValue to tryParsePlatform
1 parent 0727cf9 commit 1b0e033

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

.changeset/short-singers-doubt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rnx-kit/tools-react-native": patch
3+
---
4+
5+
Add additional platform helpers for react-native related to platform names and
6+
suffixes

packages/tools-react-native/README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@ import * as platformTools from "@rnx-kit/tools-react-native/platform";
2424
| -------- | ------------ | ----------------------------------------- |
2525
| platform | AllPlatforms | List of supported react-native platforms. |
2626

27-
| Category | Function | Description |
28-
| -------- | ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
29-
| context | `loadContext(projectRoot)` | Equivalent to calling `loadConfig()` from `@react-native-community/cli`, but the result is cached for faster subsequent accesses. |
30-
| context | `loadContextAsync(projectRoot)` | Equivalent to calling `loadConfigAsync()` (with fallback to `loadConfig()`) from `@react-native-community/cli`, but the result is cached for faster subsequent accesses. |
31-
| context | `resolveCommunityCLI(root, reactNativePath)` | Finds path to `@react-native-community/cli`. |
32-
| metro | `findMetroPath(projectRoot)` | Finds the installation path of Metro. |
33-
| metro | `getMetroVersion(projectRoot)` | Returns Metro version number. |
34-
| metro | `requireModuleFromMetro(moduleName, fromDir)` | Imports specified module starting from the installation directory of the currently used `metro` version. |
35-
| platform | `expandPlatformExtensions(platform, extensions)` | Returns a list of extensions that should be tried for the target platform in prioritized order. |
36-
| platform | `getAvailablePlatforms(startDir)` | Returns a map of available React Native platforms. The result is cached. |
37-
| platform | `getAvailablePlatformsUncached(startDir, platformMap)` | Returns a map of available React Native platforms. The result is NOT cached. |
38-
| platform | `parsePlatform(val)` | Parse a string to ensure it maps to a valid react-native platform. |
39-
| platform | `platformExtensions(platform)` | Returns file extensions that can be mapped to the target platform. |
27+
| Category | Function | Description |
28+
| -------- | ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
29+
| context | `loadContext(projectRoot)` | Equivalent to calling `loadConfig()` from `@react-native-community/cli`, but the result is cached for faster subsequent accesses. |
30+
| context | `loadContextAsync(projectRoot)` | Equivalent to calling `loadConfigAsync()` (with fallback to `loadConfig()`) from `@react-native-community/cli`, but the result is cached for faster subsequent accesses. |
31+
| context | `resolveCommunityCLI(root, reactNativePath)` | Finds path to `@react-native-community/cli`. |
32+
| metro | `findMetroPath(projectRoot)` | Finds the installation path of Metro. |
33+
| metro | `getMetroVersion(projectRoot)` | Returns Metro version number. |
34+
| metro | `requireModuleFromMetro(moduleName, fromDir)` | Imports specified module starting from the installation directory of the currently used `metro` version. |
35+
| platform | `expandPlatformExtensions(platform, extensions)` | Returns a list of extensions that should be tried for the target platform in prioritized order. |
36+
| platform | `getAvailablePlatforms(startDir)` | Returns a map of available React Native platforms. The result is cached. |
37+
| platform | `getAvailablePlatformsUncached(startDir, platformMap)` | Returns a map of available React Native platforms. The result is NOT cached. |
38+
| platform | `getModuleSuffixes(platform, appendEmpty)` | Get the module suffixes array for a given platform, suitable for use with TypeScript's moduleSuffixes setting in the form of ['.ios', '.native', ''] or ['.windows', '.win', '.native', ''] or similar |
39+
| platform | `parsePlatform(val)` | Parse a string to ensure it maps to a valid react-native platform. |
40+
| platform | `platformExtensions(platform)` | Returns file extensions that can be mapped to the target platform. |
41+
| platform | `platformValues()` | |
42+
| platform | `tryParsePlatform(val)` | |
4043

4144
<!-- @rnx-kit/api end -->

packages/tools-react-native/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export {
1313
expandPlatformExtensions,
1414
getAvailablePlatforms,
1515
getAvailablePlatformsUncached,
16+
getModuleSuffixes,
1617
parsePlatform,
1718
platformExtensions,
19+
platformValues,
20+
tryParsePlatform,
1821
} from "./platform";
1922
export type { AllPlatforms } from "./platform";

packages/tools-react-native/src/platform.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@ import { readReactNativeConfig } from "./context";
66
/**
77
* List of supported react-native platforms.
88
*/
9-
export type AllPlatforms = "android" | "ios" | "macos" | "win32" | "windows";
9+
export type AllPlatforms =
10+
| "android"
11+
| "ios"
12+
| "macos"
13+
| "win32"
14+
| "windows"
15+
| "visionos";
16+
17+
// Possible values for AllPlatforms
18+
const allValues: AllPlatforms[] = [
19+
"android",
20+
"ios",
21+
"macos",
22+
"win32",
23+
"windows",
24+
"visionos",
25+
];
1026

1127
/**
1228
* Returns a list of extensions that should be tried for the target platform in
@@ -30,6 +46,25 @@ export function expandPlatformExtensions(
3046
return expanded;
3147
}
3248

49+
/**
50+
* Get the module suffixes array for a given platform, suitable for use with TypeScript's moduleSuffixes setting
51+
* in the form of ['.ios', '.native', ''] or ['.windows', '.win', '.native', ''] or similar
52+
*
53+
* @param platform platform to get module suffixes for
54+
* @param appendEmpty finish the suffixes with an empty entry, required for typescript usage
55+
* @returns an array of suffixes to try to match a module to in order of priority
56+
*/
57+
export function getModuleSuffixes(
58+
platform: AllPlatforms,
59+
appendEmpty = true
60+
): string[] {
61+
const extensions = platformExtensions(platform).map((ext) => `.${ext}`);
62+
if (appendEmpty) {
63+
extensions.push("");
64+
}
65+
return extensions;
66+
}
67+
3368
/**
3469
* Returns a map of available React Native platforms. The result is NOT cached.
3570
* @param startDir The directory to look for react-native platforms from
@@ -134,22 +169,32 @@ export function platformExtensions(platform: string): string[] {
134169
}
135170
}
136171

172+
/**
173+
* @returns the given string as a platform value or undefined if it is not a valid platform.
174+
*/
175+
export function tryParsePlatform(val: string): AllPlatforms | undefined {
176+
return allValues.includes(val as AllPlatforms)
177+
? (val as AllPlatforms)
178+
: undefined;
179+
}
180+
137181
/**
138182
* Parse a string to ensure it maps to a valid react-native platform.
139183
*
140184
* @param val Input string
141185
* @returns React-native platform name. Throws `Error` on failure.
142186
*/
143187
export function parsePlatform(val: string): AllPlatforms {
144-
switch (val) {
145-
case "android":
146-
case "ios":
147-
case "macos":
148-
case "win32":
149-
case "windows":
150-
return val;
151-
152-
default:
153-
throw new Error("Invalid platform '" + val + "'");
188+
const platform = tryParsePlatform(val);
189+
if (!platform) {
190+
throw new Error(`Unknown platform '${val}'`);
154191
}
192+
return platform;
193+
}
194+
195+
/**
196+
* @returns List of all supported react-native platforms.
197+
*/
198+
export function platformValues(): readonly AllPlatforms[] {
199+
return allValues;
155200
}

0 commit comments

Comments
 (0)