This repository was archived by the owner on Nov 4, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (48 loc) · 2.03 KB
/
index.js
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
'use strict';
const { URL } = require('url');
const URL_PATTERNS = new RegExp(/^\/?:?([/\w-.]+)\/([\w-.]+)\/?$/);
const GITHUB_API = new RegExp(/^\/repos\/([\w-.]+)\/([\w-.]+)\/(?:tarball|zipball)(?:\/.+)?$/);
const GITHUB_CODELOAD = new RegExp(/^\/([\w-.]+)\/([\w-.]+)\/(?:legacy\.(?:zip|tar\.gz))(?:\/.+)?$/);
module.exports = url => {
const modifiedURL = url
// Prepend `https` to the URL so that `url.URL` will see the value of `url` as an actual `url`, and therefore, correctly parse it.
.replace(/^git@/, 'https://git@')
// Replace `:` with `/` before path segments so that `url.URL` will see the value of `url` as an actual `url`, and therefore, correctly parse it.
.replace(/git@([.\w]+):(?!\d)/, 'git@$1/')
// Remove `.git` from any URL before applying regular expressions to the string. Removing `.git` through a non capture group is kind of difficult.
.replace(/\.git$/, '');
const parsedURL = new URL(modifiedURL, 'https://example.com/');
const format = matches => {
return { browse: createBrowseURL(parsedURL, matches), domain: parsedURL.host, project: matches[2] || null, type: getType(parsedURL), user: matches[1] || null };
};
if (parsedURL.host) {
if (parsedURL.host.includes('api.github.com')) {
const matches = GITHUB_API.exec(parsedURL.pathname) || [];
return format(matches);
}
if (parsedURL.host.includes('codeload.github.com')) {
const matches = GITHUB_CODELOAD.exec(parsedURL.pathname) || [];
return format(matches);
}
}
return format(URL_PATTERNS.exec(parsedURL.pathname) || []);
};
function getType ({ host }) {
if (typeof host !== 'string') {
return null;
}
if (host.indexOf('github') !== -1) {
return 'github';
}
if (host.indexOf('gitlab') !== -1) {
return 'gitlab';
}
return null;
}
function createBrowseURL (parsedURL, matches) {
const protocol = parsedURL.protocol === 'http:' ? 'http:' : 'https:';
const browseURL = `${protocol}//${parsedURL.host}/${matches[1]}/${matches[2]}`;
return () => {
return browseURL;
};
}