-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
current-version.ts
66 lines (63 loc) · 2.02 KB
/
current-version.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { logger } from '../../logger';
import * as allVersioning from '../../modules/versioning';
import { configRegexPredicate } from '../regex';
import { Matcher } from './base';
export class CurrentVersionMatcher extends Matcher {
override matches(
{
versioning,
lockedVersion,
currentValue,
currentVersion,
}: PackageRuleInputConfig,
{ matchCurrentVersion }: PackageRule
): boolean | null {
if (is.undefined(matchCurrentVersion)) {
return null;
}
const isUnconstrainedValue =
!!lockedVersion && is.nullOrUndefined(currentValue);
const version = allVersioning.get(versioning);
const matchCurrentVersionStr = matchCurrentVersion.toString();
const matchCurrentVersionPred = configRegexPredicate(
matchCurrentVersionStr
);
if (matchCurrentVersionPred) {
const compareVersion = lockedVersion ?? currentVersion ?? currentValue;
return (
!is.nullOrUndefined(compareVersion) &&
matchCurrentVersionPred(compareVersion)
);
}
if (version.isVersion(matchCurrentVersionStr)) {
try {
return (
isUnconstrainedValue ||
!!(
currentValue &&
version.isValid(currentValue) &&
version.matches(matchCurrentVersionStr, currentValue)
)
);
} catch (err) {
return false;
}
}
const compareVersion = version.isVersion(currentValue)
? currentValue // it's a version so we can match against it
: lockedVersion ?? currentVersion; // need to match against this currentVersion, if available
if (is.nullOrUndefined(compareVersion)) {
return false;
}
if (version.isVersion(compareVersion)) {
return version.matches(compareVersion, matchCurrentVersion);
}
logger.debug(
{ matchCurrentVersionStr, currentValue },
'Could not find a version to compare'
);
return false;
}
}