mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-15 17:16:25 +00:00
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import semver from 'semver';
|
|
import { NewValueConfig } from '../common';
|
|
|
|
const fromParam = /^\s*from\s*:\s*"([^"]+)"\s*$/;
|
|
const fromRange = /^\s*"([^"]+)"\s*\.\.\.\s*$/;
|
|
const binaryRange = /^\s*"([^"]+)"\s*(\.\.[.<])\s*"([^"]+)"\s*$/;
|
|
const toRange = /^\s*(\.\.[.<])\s*"([^"]+)"\s*$/;
|
|
|
|
function toSemverRange(range: string): string {
|
|
if (fromParam.test(range)) {
|
|
const [, version] = range.match(fromParam);
|
|
if (semver.valid(version)) {
|
|
const nextMajor = `${semver.major(version) + 1}.0.0`;
|
|
return `>=${version} <${nextMajor}`;
|
|
}
|
|
} else if (fromRange.test(range)) {
|
|
const [, version] = range.match(fromRange);
|
|
if (semver.valid(version)) {
|
|
return `>=${version}`;
|
|
}
|
|
} else if (binaryRange.test(range)) {
|
|
const [, fromVersion, op, toVersion] = range.match(binaryRange);
|
|
if (semver.valid(fromVersion) && semver.valid(toVersion)) {
|
|
return op === '..<'
|
|
? `>=${fromVersion} <${toVersion}`
|
|
: `>=${fromVersion} <=${toVersion}`;
|
|
}
|
|
} else if (toRange.test(range)) {
|
|
const [, op, toVersion] = range.match(toRange);
|
|
if (semver.valid(toVersion)) {
|
|
return op === '..<' ? `<${toVersion}` : `<=${toVersion}`;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function getNewValue({ currentValue, toVersion }: NewValueConfig): string {
|
|
if (fromParam.test(currentValue)) {
|
|
return toVersion;
|
|
}
|
|
if (fromRange.test(currentValue)) {
|
|
const [, version] = currentValue.match(fromRange);
|
|
return currentValue.replace(version, toVersion);
|
|
}
|
|
if (binaryRange.test(currentValue)) {
|
|
const [, , , version] = currentValue.match(binaryRange);
|
|
return currentValue.replace(version, toVersion);
|
|
}
|
|
if (toRange.test(currentValue)) {
|
|
const [, , version] = currentValue.match(toRange);
|
|
return currentValue.replace(version, toVersion);
|
|
}
|
|
return currentValue;
|
|
}
|
|
|
|
export { toSemverRange, getNewValue };
|