Skip to content

Commit 341a1eb

Browse files
committed
preserve ordering
1 parent e36f95a commit 341a1eb

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

workspaces/util/src/readPackageJson.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ import { z } from "zod";
55

66
const packageJsonZodType = z.object({
77
scripts: z.record(z.string(), z.string()).optional(),
8+
workspaces: z.array(z.string()).optional(),
89
});
910

1011
export type PackageJson = z.infer<typeof packageJsonZodType>;
1112

1213
export async function readPackageJson(
1314
directory: string = ".",
1415
): Promise<PackageJson> {
15-
const text = await readFile(path.join(directory, "package.json"), {
16-
encoding: "utf8",
17-
});
18-
return packageJsonZodType.passthrough().parse(JSON.parse(text));
16+
const data = JSON.parse(
17+
await readFile(path.join(directory, "package.json"), {
18+
encoding: "utf8",
19+
}),
20+
);
21+
22+
// Using Zod for validation only, not for returning, to preserve the ordering
23+
// of keys in the original data.
24+
packageJsonZodType.parse(data);
25+
return data;
1926
}

0 commit comments

Comments
 (0)