Skip to content

Commit

Permalink
fix: use validated package.json version in manifest (#147)
Browse files Browse the repository at this point in the history
Closes #139
  • Loading branch information
mcous authored Sep 13, 2023
1 parent 9f1e5f4 commit 402d679
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
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

0 comments on commit 402d679

Please sign in to comment.