-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-remote-to-url.js
35 lines (27 loc) · 960 Bytes
/
git-remote-to-url.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
const gitRemoteToUrl = (remote) => {
if (typeof remote !== 'string' || remote === '') return '';
const hostRegex = /@[a-zA-Z0-9.]+:/;
const hostRepositoryRegex = /[a-zA-Z0-9]+@[a-zA-Z0-9.]+/;
const noPrefixRegex = /^[a-zA-Z]+:\/\//;
const http = 'http';
// Copy remote string
let gitRemote = remote.substr();
if (gitRemote.startsWith(http)) {
return gitRemote;
}
// Replace 'ssh' prefix with 'http'
gitRemote = gitRemote.replace(/^ssh/, http);
if (!gitRemote.match(noPrefixRegex)) {
gitRemote = `http://${gitRemote}`;
}
// Remove ':' after host and repository
if (gitRemote.match(hostRegex)) {
gitRemote = gitRemote.replace(hostRegex, p => `${p.substr(0, p.length - 1)}/`);
}
// Remove user from user@host format for ssh
if (gitRemote.match(hostRepositoryRegex)) {
gitRemote = gitRemote.replace(hostRepositoryRegex, p => p.split('@')[1]);
}
return gitRemote;
};
module.exports = gitRemoteToUrl;