Skip to content

Commit

Permalink
retain precision of range specifier when updating
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Jun 1, 2024
1 parent 849ac26 commit 2a9cd77
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion fixtures/npm-test/package.json

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

24 changes: 19 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ function highlightDiff(a: string, b: string, colorFn: (str: string) => string) {
} else {
res += aParts[i].split("").map(char => {
return versionPartRe.test(char) ? colorFn(char) : char;
}).join("") + colorFn(`.${aParts.slice(i + 1).join(".")}`);
}).join("") + colorFn(`.${aParts.slice(i + 1).join(".")}`.replace(/\.$/, ""));
}
break;
} else {
Expand Down Expand Up @@ -411,8 +411,21 @@ function updateProjectToml(pkgStr: string, deps: Deps) {
return newPkgStr;
}

function updateRange(range: string, version: string) {
return range.replace(/[0-9]+\.[0-9]+\.[0-9]+(-.+)?/g, version);
function updateRange(oldRange: string, newVersion: string, oldOriginal?: string) {
let newRange = oldRange.replace(/[0-9]+\.[0-9]+\.[0-9]+(-.+)?/g, newVersion);
let rangeModified = false;

// if old version is a range like ^5 or ~5, retain number of version parts in new range
if (oldOriginal && oldOriginal !== oldRange && /^[\^~]/.test(newRange)) {
const oldParts = oldOriginal.substring(1).split(".");
const newParts = newRange.substring(1).split(".");
if (oldParts.length !== newParts.length) {
newRange = `${newRange[0]}${newParts.slice(0, oldParts.length).join(".")}`;
rangeModified = true;
}
}
return {newRange, rangeModified};
}
function isVersionPrerelease(version: string) {
Expand Down Expand Up @@ -943,12 +956,13 @@ async function main() {

const key = `${type}${sep}${name}`;
const oldRange = deps[mode][key].old;
const oldOriginal = deps[mode][key].oldOriginal;
const newVersion = findNewVersion(data, {
usePre, useRel, useGreatest, semvers, range: oldRange, mode,
});
const newRange = updateRange(oldRange, newVersion);
const {newRange, rangeModified} = updateRange(oldRange, newVersion, oldOriginal);

if (!newVersion || oldRange === newRange) {
if (!newVersion || oldRange === newRange || (rangeModified && oldOriginal !== newRange)) {
delete deps[mode][key];
} else {
deps[mode][key].new = newRange;
Expand Down
42 changes: 15 additions & 27 deletions snapshots/index.test.ts.snap

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

0 comments on commit 2a9cd77

Please sign in to comment.