-
Notifications
You must be signed in to change notification settings - Fork 218
/
github-url.ts
75 lines (54 loc) · 1.99 KB
/
github-url.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
67
68
69
70
71
72
73
74
75
import sanitize from 'sanitize-filename';
// https://regex101.com/r/fK2rV3/1
const githubReposIdPattern = new RegExp(/^\/([^/]+)\/([^/]+)$/);
// https://regex101.com/r/CQjSuz/1
const sanitizeSymbolsChars = new RegExp(/[^a-zA-Z0-9_.]+/g);
// https://regex101.com/r/ARgXvb/1
// GitHub will return a zip file with the v removed if the tag or branch name is "v + number"
const sanitizeVersionChars = new RegExp(/^v[\d]/gi);
export class GitHubUrl {
private _organizationName: string;
private _reposName: string;
private _branchName: string;
private _tagName: string;
get organizationName(): string {
return this._organizationName;
}
get reposName(): string {
return this._reposName;
}
get branchName(): string {
return this._branchName;
}
get tagName(): string {
return this._tagName;
}
get archiveUrl(): string {
const encodedBranchName = encodeURIComponent(this.branchName);
const encodedTagName = encodeURIComponent(this.tagName);
const zipUrl = encodedTagName !== '' ? `tags/${encodedTagName}` : `heads/${encodedBranchName}`;
const ghUrl = new URL(`/${this.organizationName}/${this.reposName}/archive/refs/${zipUrl}.zip`, 'https://github.com');
return ghUrl.toString();
}
get extractedArchiveDirName(): string {
const name = this._tagName !== '' ? this._tagName : this._branchName;
return name.replace(sanitizeVersionChars, m => m.substring(1)).replaceAll(sanitizeSymbolsChars, '-');
}
constructor(url: string, branchName = 'main', tagName = '') {
let matched;
try {
const ghUrl = new URL(url);
matched = ghUrl.pathname.match(githubReposIdPattern);
if (ghUrl.hostname !== 'github.com' || matched == null) {
throw new Error();
}
}
catch (err) {
throw new Error(`The specified URL is invalid. : url='${url}'`);
}
this._branchName = branchName;
this._tagName = tagName;
this._organizationName = sanitize(matched[1]);
this._reposName = sanitize(matched[2]);
}
}