-
Notifications
You must be signed in to change notification settings - Fork 639
/
address-parser.js
80 lines (70 loc) · 2.21 KB
/
address-parser.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
// USED BY FRONT END
// DO NOT GO ES6
const addressWindowsLocalRegexp = /[a-zA-Z]:\\([^\\]+\\?)*/;
const addressSshWithPortRegexp = /ssh:\/\/(.*):(\d*)\/(.*)/;
const addressSshWithoutPortRegexp = /ssh:\/\/([^/]*)\/(.*)/;
const addressGitWithoutPortWithUsernamePortRegexp = /([^@]*)@([^:]*):([^.]*)(\.git)?$/;
const addressGitWithoutPortWithoutUsernameRegexp = /([^:]*):([^.]*)(\.git)?$/;
const addressHttpsRegexp = /https:\/\/([^/]*)\/([^.]*)(\.git)?$/;
const addressUnixLocalRegexp = /.*\/([^/]+)/;
/**
* Show slashes in path parameter.
*
* @param {string} path
*/
exports.encodePath = (path) => encodeURIComponent(path).replace(/%2F/g, '/');
exports.parseAddress = (remote) => {
let match = addressWindowsLocalRegexp.exec(remote);
if (match) {
let project = match[1];
if (project[project.length - 1] == '\\') project = project.slice(0, project.length - 1);
return { address: remote, host: 'localhost', project: project, shortProject: project };
}
match = addressSshWithPortRegexp.exec(remote);
if (match)
return {
address: remote,
host: match[1],
port: match[2],
project: match[3],
shortProject: match[3].split('/').pop(),
};
match = addressSshWithoutPortRegexp.exec(remote);
if (match)
return {
address: remote,
host: match[1],
project: match[2],
shortProject: match[2].split('/').pop(),
};
match = addressGitWithoutPortWithUsernamePortRegexp.exec(remote);
if (match)
return {
address: remote,
username: match[1],
host: match[2],
project: match[3],
shortProject: match[3].split('/').pop(),
};
match = addressGitWithoutPortWithoutUsernameRegexp.exec(remote);
if (match)
return {
address: remote,
host: match[1],
project: match[2],
shortProject: match[2].split('/').pop(),
};
match = addressHttpsRegexp.exec(remote);
if (match)
return {
address: remote,
host: match[1],
project: match[2],
shortProject: match[2].split('/').pop(),
};
match = addressUnixLocalRegexp.exec(remote);
if (match)
return { address: remote, host: 'localhost', project: match[1], shortProject: match[1] };
return { address: remote };
};