Skip to content

Commit

Permalink
Merge pull request #2035 from hydrosquall/cameron.yick/skip-canary-an…
Browse files Browse the repository at this point in the history
…d-next-branches-when-calculating-greatest-version

feat(plugins/npm): exclude prelease versions when calculating monorepo project version
  • Loading branch information
hipstersmoothie authored Jul 22, 2021
2 parents 909ae8b + bdc9179 commit 2d0dc88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 15 additions & 0 deletions plugins/npm/__tests__/npm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ const monorepoPackagesResult = [
{ path: "packages/d", name: "@packages/d", package: { version: "0.1.1" } },
];

const monorepoPackagesWithPrereleaseResult = [
{ path: "packages/a", name: "@packages/a", package: { version: "0.1.1" } },
{ path: "packages/b", name: "@packages/b", package: {} },
{ path: "packages/c", name: "@packages/c", package: { version: "0.1.2" } },
{ path: "packages/d", name: "@packages/d", package: { version: "0.1.1" } },
// This can happen if a new module is published with a breaking version
{ path: "packages/e", name: "@packages/e", package: { version: "1.0.0-next.0" } },
{ path: "packages/f", name: "@packages/f", package: { version: "1.0.0-next.0" } },
];

const packageTemplate = ({
path,
name,
Expand Down Expand Up @@ -143,6 +153,11 @@ describe("getMonorepoPackage", () => {
monorepoPackages.mockReturnValueOnce(monorepoPackagesResult);
expect(getMonorepoPackage()).toStrictEqual({ version: "0.1.2" });
});

test("should ignore versions from preleases", () => {
monorepoPackages.mockReturnValueOnce(monorepoPackagesWithPrereleaseResult);
expect(getMonorepoPackage()).toStrictEqual({ version: "0.1.2" });
});
});

const prefixRelease = (str: string) => str;
Expand Down
13 changes: 11 additions & 2 deletions plugins/npm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
DEFAULT_PRERELEASE_BRANCHES,
} from "@auto-it/core";
import getPackages from "get-monorepo-packages";
import { gt, gte, inc, ReleaseType } from "semver";
import { gt, gte, inc, prerelease, ReleaseType } from "semver";
import {
loadPackageJson,
getRepo,
Expand Down Expand Up @@ -146,6 +146,7 @@ export async function getChangedPackages({
return [...changed];
}


/** Get the package with the greatest version in a monorepo */
export function getMonorepoPackage() {
const packages = getPackages(process.cwd());
Expand All @@ -154,7 +155,15 @@ export function getMonorepoPackage() {
return {} as IPackageJSON;
}

const monorepoPackage = packages.reduce((greatest, subPackage) => {
// Remove pre-releases so that released package versions take precedence
let releasedPackages = packages.filter(subPackage => prerelease(subPackage.package?.version || '') === null);
// If doing this would remove all packages, this means were not any @latest releases yet
// In that case, restore the original list of packages.
if (releasedPackages.length === 0) {
releasedPackages = packages;
}

const monorepoPackage = releasedPackages.reduce((greatest, subPackage) => {
if (subPackage.package.version) {
if (!greatest.package.version) {
return subPackage;
Expand Down

0 comments on commit 2d0dc88

Please sign in to comment.