Skip to content

Commit 7676e1d

Browse files
committed
add zod schema for package.json
1 parent 001884f commit 7676e1d

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

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

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import assert from "node:assert/strict";
22
import path from "node:path";
33
import fs from "node:fs";
44
import { packageDirectorySync } from "pkg-dir";
5-
import { NormalizedPackageJson, readPackageSync } from "read-pkg";
5+
import { readPackageSync } from "read-pkg";
66
import { createRequire } from "node:module";
7+
import * as zod from "zod";
78

89
import { chalk, prettyPath } from "@react-native-node-api/cli-utils";
910

@@ -294,32 +295,45 @@ export function visualizeLibraryMap(libraryMap: LibraryMap) {
294295
return result.join("\n");
295296
}
296297

298+
export const ReactNativeNodeAPIConfigurationSchema = zod.object({
299+
reactNativeNodeApi: zod
300+
.object({
301+
scan: zod
302+
.object({
303+
dependencies: zod.array(zod.string()).optional(),
304+
})
305+
.optional(),
306+
})
307+
.optional(),
308+
});
309+
310+
export const PackageJsonDependenciesSchema = zod.object({
311+
dependencies: zod.record(zod.string(), zod.string()).optional(),
312+
});
313+
314+
export type ReactNativeNodeAPIConfiguration = zod.infer<
315+
typeof ReactNativeNodeAPIConfigurationSchema
316+
>;
317+
export type PackageJsonDependencies = zod.infer<
318+
typeof PackageJsonDependenciesSchema
319+
>;
320+
321+
type PackageJsonWithNodeApi = PackageJsonDependencies &
322+
ReactNativeNodeAPIConfiguration;
323+
297324
export function findPackageConfigurationByPath(
298325
fromPath: string,
299326
): ReactNativeNodeAPIConfiguration {
300327
const packageRoot = packageDirectorySync({ cwd: fromPath });
301328
assert(packageRoot, `Could not find package root from ${fromPath}`);
302329

303-
const {
304-
reactNativeNodeApi,
305-
}: NormalizedPackageJson & ReactNativeNodeAPIConfiguration = readPackageSync({
330+
const packageJson = readPackageSync({
306331
cwd: packageRoot,
307332
});
308333

309-
return { reactNativeNodeApi };
334+
return ReactNativeNodeAPIConfigurationSchema.parse(packageJson);
310335
}
311336

312-
export interface ReactNativeNodeAPIConfiguration {
313-
reactNativeNodeApi?: {
314-
scan?: {
315-
dependencies?: string[];
316-
};
317-
};
318-
}
319-
320-
type PackageJsonWithNodeApi = NormalizedPackageJson &
321-
ReactNativeNodeAPIConfiguration;
322-
323337
/**
324338
* Search upwards from a directory to find a package.json and
325339
* return a record mapping from each dependency of that package to their path on disk.
@@ -333,10 +347,15 @@ export function findPackageDependencyPaths(
333347

334348
const requireFromRoot = createRequire(path.join(packageRoot, "noop.js"));
335349

336-
const { dependencies = {}, reactNativeNodeApi } = readPackageSync({
350+
const packageJson = readPackageSync({
337351
cwd: packageRoot,
338352
}) as PackageJsonWithNodeApi;
339353

354+
const { dependencies = {} } =
355+
PackageJsonDependenciesSchema.parse(packageJson);
356+
const { reactNativeNodeApi } =
357+
ReactNativeNodeAPIConfigurationSchema.parse(packageJson);
358+
340359
const initialDeps = Object.keys(dependencies).concat(
341360
reactNativeNodeApi?.scan?.dependencies ?? [],
342361
);

0 commit comments

Comments
 (0)