-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathcommon.ts
More file actions
74 lines (63 loc) · 2.08 KB
/
common.ts
File metadata and controls
74 lines (63 loc) · 2.08 KB
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
67
68
69
70
71
72
73
74
import { logger } from '../../../logger/index.ts';
import { regEx } from '../../../util/regex.ts';
import { parseUrl } from '../../../util/url.ts';
import { api as versioning } from '../../versioning/nuget/index.ts';
import type { ParsedRegistryUrl } from './types.ts';
const buildMetaRe = regEx(/\+.+$/g);
export function removeBuildMeta(version: string): string {
return version.replace(buildMetaRe, '');
}
const urlWhitespaceRe = regEx(/\s/g);
export function massageUrl(url: string | null | undefined): string | null {
if (url === null || url === undefined) {
return null;
}
let resultUrl = url;
// During `dotnet pack` certain URLs are being URL decoded which may introduce whitespace
// and causes Markdown link generation problems.
resultUrl = resultUrl.replace(urlWhitespaceRe, '%20');
return resultUrl;
}
const protocolVersionRegExp = regEx(/#protocolVersion=(?<protocol>2|3)/);
export function parseRegistryUrl(registryUrl: string): ParsedRegistryUrl {
const parsedUrl = parseUrl(registryUrl);
if (!parsedUrl) {
logger.debug(
{ urL: registryUrl },
`nuget registry failure: can't parse ${registryUrl}`,
);
return { feedUrl: registryUrl, protocolVersion: null };
}
let protocolVersion = 2;
const protocolVersionMatch = protocolVersionRegExp.exec(
parsedUrl.hash,
)?.groups;
if (protocolVersionMatch) {
const { protocol } = protocolVersionMatch;
parsedUrl.hash = '';
protocolVersion = Number.parseInt(protocol, 10);
} else if (parsedUrl.pathname.endsWith('.json')) {
protocolVersion = 3;
}
const feedUrl = parsedUrl.href;
return { feedUrl, protocolVersion };
}
/**
* Compare two versions. Return:
* - `1` if `a > b` or `b` is invalid
* - `-1` if `a < b` or `a` is invalid
* - `0` if `a == b` or both `a` and `b` are invalid
*/
export function sortNugetVersions(a: string, b: string): number {
if (versioning.isValid(a)) {
if (versioning.isValid(b)) {
return versioning.sortVersions(a, b);
} else {
return 1;
}
} else if (versioning.isValid(b)) {
return -1;
} else {
return 0;
}
}