Skip to content

Commit

Permalink
fix(update-deps): properly resolve next pre-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
KillianHmyd authored and antongolub committed Jan 22, 2021
1 parent 702a540 commit 62b348e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const execa = require("execa");
*
* @param {String} branch The branch for which to retrieve the tags.
* @param {Object} [execaOptions] Options to pass to `execa`.
* @param {Array<String>} filters List of prefixes/sufixes to be checked inside tags.
* @param {Array<String>} filters List of string to be checked inside tags.
*
* @return {Array<String>} List of git tags.
* @throws {Error} If the `git` command fails.
Expand All @@ -20,7 +20,7 @@ function getTags(branch, execaOptions, filters) {

if (!filters || !filters.length) return tags;

const validateSubstr = (t, f) => !!f.find((v) => t.includes(v));
const validateSubstr = (t, f) => f.every((v) => t.includes(v));

return tags.filter((tag) => validateSubstr(tag, filters));
}
Expand Down
29 changes: 23 additions & 6 deletions lib/updateDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ const getNextVersion = (pkg) => {
: lastVersion || "1.0.0";
};

/**
* Resolve the package version from a tag
*
* @param {Package} pkg Package object.
* @param {string} tag The tag containing the version to resolve
* @returns {string} The version of the package
* @internal
*/
const getVersionFromTag = (pkg, tag) => (pkg.name ? tag.replace(`${pkg.name}@`, "") : tag);

/**
* Resolve next package version on prereleases.
*
Expand All @@ -38,20 +48,27 @@ const getNextPreVersion = (pkg, tags) => {
// Extract tags:
// 1. Set filter to extract only package tags
// 2. Get tags from a branch considering the filters established
// 3. Resolve the versions from the tags
// TODO: replace {cwd: '.'} with multiContext.cwd
if (pkg.name) tagFilters.push(pkg.name);
if (!tags || !tags.length) {
tags = getTags(pkg._branch, { cwd: "." }, tagFilters).map((tag) =>
pkg.name ? tag.replace(`${pkg.name}@`, "") : tag
);
tags = getTags(pkg._branch, { cwd: "." }, tagFilters);
}

const lastPreRelTag = getPreReleaseTag(lastVersion);
const isNewPreRelTag = lastPreRelTag && lastPreRelTag !== pkg._preRelease;

return isNewPreRelTag || !lastVersion
? `1.0.0-${pkg._preRelease}.1`
: _nextPreVersionCases(tags, lastVersion, pkg._nextType, pkg._preRelease);
const versionToSet =
isNewPreRelTag || !lastVersion
? `1.0.0-${pkg._preRelease}.1`
: _nextPreVersionCases(
tags.map((tag) => getVersionFromTag(pkg, tag)),
lastVersion,
pkg._nextType,
pkg._preRelease
);

return versionToSet;
};

/**
Expand Down
7 changes: 4 additions & 3 deletions test/lib/updateDeps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ describe("getNextPreVersion()", () => {
[null, "patch", "rc", [], "1.0.0-rc.1"],
["1.0.0-rc.0", "minor", "dev", [], "1.0.0-dev.1"],
["1.0.0-dev.0", "major", "dev", [], "1.0.0-dev.1"],
["1.0.0-dev.0", "major", "dev", ["1.0.0-dev.1"], "1.0.0-dev.2"],
["1.0.0-dev.0", "major", "dev", ["1.0.0-dev.1", "1.0.1-dev.0"], "1.0.1-dev.1"],
["1.0.0-dev.0", "major", "dev", ["testing-package@1.0.0-dev.1"], "1.0.0-dev.2"],
["1.0.0-dev.0", "major", "dev", ["testing-package@1.0.0-dev.1", "1.0.1-dev.0"], "1.0.1-dev.1"],
["11.0.0", "major", "beta", [], "12.0.0-beta.1"],
["1.0.0", "minor", "beta", [], "1.1.0-beta.1"],
["1.0.0", "patch", "beta", [], "1.0.1-beta.1"],
Expand All @@ -196,7 +196,8 @@ describe("getNextPreVersion()", () => {
_nextType: releaseType,
_lastRelease: {version: lastVersion},
_preRelease: preRelease,
_branch: "master",
_branch: "master",
name: "testing-package"
},
lastTags
)).toBe(nextVersion);
Expand Down

0 comments on commit 62b348e

Please sign in to comment.