From 1718824b1d96afa990de68413879ad0fdc186034 Mon Sep 17 00:00:00 2001 From: silverwind Date: Sat, 1 Jun 2024 18:10:10 +0200 Subject: [PATCH] retain precision of range specifier when updating --- fixtures/npm-test/package.json | 2 +- index.ts | 22 ++++++++++++++---- snapshots/index.test.ts.snap | 42 ++++++++++++---------------------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/fixtures/npm-test/package.json b/fixtures/npm-test/package.json index f4ea1e3..c6b39a3 100644 --- a/fixtures/npm-test/package.json +++ b/fixtures/npm-test/package.json @@ -18,7 +18,7 @@ "peerDependencies": { "@babel/preset-env": "~6.0.0", "svgstore": "^1.0.0 || ^2.0.0", - "typescript": "^5" + "typescript": "^4" }, "resolutions": { "versions/updates": "^1.0.0" diff --git a/index.ts b/index.ts index 96d4430..656d2b5 100755 --- a/index.ts +++ b/index.ts @@ -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 { @@ -411,8 +411,19 @@ 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); + + // 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(".")}`; + } + } + + return newRange; } function isVersionPrerelease(version: string) { @@ -943,12 +954,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 = updateRange(oldRange, newVersion, oldOriginal); - if (!newVersion || oldRange === newRange) { + if (!newVersion || oldOriginal === newRange) { delete deps[mode][key]; } else { deps[mode][key].new = newRange; diff --git a/snapshots/index.test.ts.snap b/snapshots/index.test.ts.snap index 8abbb47..21dd5a3 100644 --- a/snapshots/index.test.ts.snap +++ b/snapshots/index.test.ts.snap @@ -79,11 +79,6 @@ exports[`dual 1`] = ` }, }, "tool.poetry.group.dev.dependencies": { - "PyYAML": { - "info": "https://github.com/yaml/pyyaml", - "new": "6.0", - "old": "6.0", - }, "djlint": { "info": "https://github.com/Riverside-Healthcare/djlint", "new": "1.31.0", @@ -121,8 +116,8 @@ exports[`exclude 1`] = ` "peerDependencies": { "typescript": { "info": "https://github.com/Microsoft/TypeScript", - "new": "^5.4.5", - "old": "^5", + "new": "^5", + "old": "^4", }, }, }, @@ -229,8 +224,8 @@ exports[`greatest 1`] = ` }, "typescript": { "info": "https://github.com/Microsoft/TypeScript", - "new": "^5.4.5", - "old": "^5", + "new": "^5", + "old": "^4", }, }, "resolutions": { @@ -349,8 +344,8 @@ exports[`latest 1`] = ` }, "typescript": { "info": "https://github.com/Microsoft/TypeScript", - "new": "^5.4.5", - "old": "^5", + "new": "^5", + "old": "^4", }, }, "resolutions": { @@ -383,6 +378,11 @@ exports[`patch 1`] = ` "new": "3.1.4", "old": "3.1.0", }, + "react": { + "info": "https://github.com/facebook/react/tree/HEAD/packages/react", + "new": "18.0.0", + "old": "18.0", + }, "svgstore": { "info": "https://github.com/svgstore/svgstore", "new": "^3.0.0-2", @@ -394,13 +394,6 @@ exports[`patch 1`] = ` "old": "6941e05", }, }, - "peerDependencies": { - "typescript": { - "info": "https://github.com/Microsoft/TypeScript", - "new": "^5.0.4", - "old": "^5", - }, - }, "resolutions": { "versions/updates": { "info": "https://github.com/silverwind/updates", @@ -475,8 +468,8 @@ exports[`prerelease 1`] = ` }, "typescript": { "info": "https://github.com/Microsoft/TypeScript", - "new": "^5.5.0-dev.20240601", - "old": "^5", + "new": "^5", + "old": "^4", }, }, "resolutions": { @@ -494,11 +487,6 @@ exports[`pypi 1`] = ` { "pypi": { "tool.poetry.group.dev.dependencies": { - "PyYAML": { - "info": "https://github.com/yaml/pyyaml", - "new": "6.0", - "old": "6.0", - }, "djlint": { "info": "https://github.com/Riverside-Healthcare/djlint", "new": "1.31.0", @@ -572,8 +560,8 @@ exports[`release 1`] = ` }, "typescript": { "info": "https://github.com/Microsoft/TypeScript", - "new": "^5.4.5", - "old": "^5", + "new": "^5", + "old": "^4", }, }, "resolutions": {