Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore]: upgrade compare-versions to 6.0.0 #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"dependencies": {
"arg": "^5.0.2",
"compare-versions": "^5.0.1",
"compare-versions": "^6.0.0",
"fancy-log": "^2.0.0",
"kleur": "^4.1.5",
"table": "^6.8.0"
Expand Down
25 changes: 1 addition & 24 deletions src/core/getPackageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,11 @@ function isCompatible(nodeVersion: string, depRange: string) {
if (['x', '*'].includes(depRange)) return true;

try {
return depRange
.split('||')
.map((range) => removeWhitespace(range))
.some((range) => safeSatisfies(nodeVersion, range));
return satisfies(nodeVersion, depRange);
} catch (error) {
if ((error as Error).message.match(/Invalid argument not valid semver/)) {
return 'invalid';
}
throw error;
}
}

// accounts for `AND` ranges -- ie, `'>=1.2.9 <2.0.0'`
function safeSatisfies(nodeVersion: string, range: string) {
return (
range
.split(' ')
// filter out any whitespace we may have missed with the RegEx -- ie, `'>4 <8'`
.filter((r) => !!r)
.every((r) => satisfies(nodeVersion, r))
);
}

// trims leading and trailing whitespace and whitespace
// between the comparator operators and the actual version number
// version number. ie, ' > = 12.0.0 ' becomes '>=12.0.0'
function removeWhitespace(range: string) {
const comparatorWhitespace = /((?<=(<|>))(\s+)(?=(=)))/g;
const comparatorAndVersionWhiteSpace = /(?<=(<|>|=|\^|~))(\s+)(?=\d)/g;
return range.trim().replace(comparatorWhitespace, '').replace(comparatorAndVersionWhiteSpace, '');
}
14 changes: 14 additions & 0 deletions tests/unit/core/getPackageData/get-package-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ describe('getPackageData', () => {
range: 'not-a-range',
});
});

it('correctly handles malformed range', async () => {
const output = getPackageData(
{
package: 'test-package',
range: '> = 8.0.0 < = 9.0.0',
},
'8.0.0'
);
expect(output).toStrictEqual({
compatible: true,
range: '> = 8.0.0 < = 9.0.0',
});
});
});