Skip to content

Commit bfbd8ec

Browse files
committed
add safeParse for configuration node-api
1 parent e40dc0d commit bfbd8ec

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

packages/host/src/node/path-utils.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,50 @@ describe("findNodeApiModulePathsByDependency", () => {
541541
});
542542
});
543543

544+
it("should find Node-API paths by dependency in root package.json with bad configuration (excluding certain packages)", async (context) => {
545+
const packagesNames = ["lib-a", "lib-b", "lib-c", "lib-d", "lib-e"];
546+
const tempDir = setupTempDirectory(context, {
547+
"app/package.json": JSON.stringify({
548+
name: "app",
549+
dependencies: Object.fromEntries(
550+
packagesNames
551+
.slice(0, 2)
552+
.map((packageName) => [packageName, "^1.0.0"]),
553+
),
554+
reactNativeNodeApi: {
555+
scan: "lib-e",
556+
},
557+
}),
558+
...Object.fromEntries(
559+
packagesNames.map((packageName) => [
560+
`app/node_modules/${packageName}`,
561+
{
562+
"package.json": JSON.stringify({
563+
name: packageName,
564+
main: "index.js",
565+
}),
566+
"index.js": "",
567+
"addon.apple.node/react-native-node-api-module": "",
568+
},
569+
]),
570+
),
571+
});
572+
573+
// shouldn't drop error
574+
const result = await findNodeApiModulePathsByDependency({
575+
fromPath: path.join(tempDir, "app"),
576+
platform: "apple",
577+
includeSelf: false,
578+
excludePackages: ["lib-a"],
579+
});
580+
assert.deepEqual(result, {
581+
"lib-b": {
582+
path: path.join(tempDir, "app/node_modules/lib-b"),
583+
modulePaths: ["addon.apple.node"],
584+
},
585+
});
586+
});
587+
544588
it("should find Node-API paths by dependency in root package.json configuration with incorrect dependency configuration (excluding certain packages)", async (context) => {
545589
const packagesNames = ["lib-a", "lib-b", "lib-c", "lib-d"];
546590
const tempDir = setupTempDirectory(context, {
@@ -706,7 +750,7 @@ describe("findNodeApiModulePathsByDependency", () => {
706750
packagesNames.slice(1).map((packageName, i) => {
707751
// if even then i-1
708752
// if not even then i+1
709-
const dependencyIndex = i + ((i % 2) * 2 - 1)
753+
const dependencyIndex = i + ((i % 2) * 2 - 1);
710754

711755
return [
712756
`app/node_modules/${packageName}`,

packages/host/src/node/path-utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,21 @@ export type PackageJsonDependencies = zod.infer<
321321
type PackageJsonWithNodeApi = PackageJsonDependencies &
322322
ReactNativeNodeAPIConfiguration;
323323

324+
export function parseReactNativeNodeAPIConfigurationSchema(
325+
packageJson: PackageJsonWithNodeApi,
326+
): ReactNativeNodeAPIConfiguration {
327+
const parsed = ReactNativeNodeAPIConfigurationSchema.safeParse(packageJson);
328+
329+
if (!parsed.success) {
330+
console.warn("Invalid reactNativeNodeApi configuration");
331+
console.warn(JSON.stringify(packageJson));
332+
console.warn(parsed.error.message);
333+
return {};
334+
}
335+
336+
return parsed.data;
337+
}
338+
324339
export function findPackageConfigurationByPath(
325340
fromPath: string,
326341
): ReactNativeNodeAPIConfiguration {
@@ -331,7 +346,7 @@ export function findPackageConfigurationByPath(
331346
cwd: packageRoot,
332347
});
333348

334-
return ReactNativeNodeAPIConfigurationSchema.parse(packageJson);
349+
return parseReactNativeNodeAPIConfigurationSchema(packageJson);
335350
}
336351

337352
/**
@@ -356,7 +371,7 @@ export function findPackageDependencyPaths(
356371
const { dependencies = {} } =
357372
PackageJsonDependenciesSchema.parse(packageJson);
358373
const { reactNativeNodeApi } =
359-
ReactNativeNodeAPIConfigurationSchema.parse(packageJson);
374+
parseReactNativeNodeAPIConfigurationSchema(packageJson);
360375

361376
const initialDeps = Object.keys(dependencies).concat(
362377
reactNativeNodeApi?.scan?.dependencies ?? [],

0 commit comments

Comments
 (0)