Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use validated package.json version in manifest #147

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/main.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/__tests__/read-manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ describe("readManifest", () => {
});
});

it("should normalize the version field", async () => {
await fs.writeFile(
path.join(directory, "package.json"),
JSON.stringify({ name: "cool-name", version: "v1.2.3" }),
"utf8"
);

const result = await subject.readManifest(
path.join(directory, "package.json")
);

expect(result).toMatchObject({ version: "1.2.3" });
});

it("should read a package.json file in a directory", async () => {
await fs.writeFile(
path.join(directory, "package.json"),
Expand Down
8 changes: 4 additions & 4 deletions src/read-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const isTarball = (file: unknown): file is string => {
return typeof file === "string" && path.extname(file) === TARBALL_EXTNAME;
};

const isVersion = (version: unknown): version is string => {
return semverValid(version as string) !== null;
const validateVersion = (version: unknown): string | undefined => {
return semverValid(version as string) ?? undefined;
};

const readPackageJson = async (...pathSegments: string[]): Promise<string> => {
Expand Down Expand Up @@ -110,7 +110,7 @@ export async function readManifest(
try {
manifestJson = JSON.parse(manifestContents) as Record<string, unknown>;
name = manifestJson["name"];
version = manifestJson["version"];
version = validateVersion(manifestJson["version"]);
publishConfig = manifestJson["publishConfig"] ?? {};
} catch (error) {
throw new errors.PackageJsonParseError(packageSpec, error);
Expand All @@ -120,7 +120,7 @@ export async function readManifest(
throw new errors.InvalidPackageNameError(name);
}

if (!isVersion(version)) {
if (typeof version !== "string") {
throw new errors.InvalidPackageVersionError(version);
}

Expand Down