52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
/* ---------------------------------------------- *
|
|
* ------------------ classes ------------------- *
|
|
* ---------------------------------------------- */
|
|
export class GitRepo {
|
|
host: string;
|
|
user: string;
|
|
repo: string;
|
|
accessToken?: string;
|
|
sshUrl: string;
|
|
httpsUrl: string;
|
|
constructor(stringArg: string, tokenArg?: string) {
|
|
const regexMatches = gitRegex(stringArg);
|
|
if (!regexMatches) {
|
|
throw new Error(`Invalid git repository URL: ${stringArg}`);
|
|
}
|
|
this.host = regexMatches[1];
|
|
this.user = regexMatches[2];
|
|
this.repo = regexMatches[3];
|
|
this.accessToken = tokenArg;
|
|
this.sshUrl = gitLink(this.host, this.user, this.repo, this.accessToken, 'ssh');
|
|
this.httpsUrl = gitLink(this.host, this.user, this.repo, this.accessToken, 'https');
|
|
}
|
|
}
|
|
|
|
/* ---------------------------------------------- *
|
|
* ------------------ helpers ------------------- *
|
|
* ---------------------------------------------- */
|
|
const gitRegex = function (stringArg: string): RegExpExecArray | null {
|
|
const regexString =
|
|
/([a-zA-Z0-9\-_\.]*)(?:\/|\:)([a-zA-Z0-9\-_\.]*)(?:\/)([a-zA-Z0-9\-_\.]*)(?:\.git)/;
|
|
const regexMatches = regexString.exec(stringArg);
|
|
return regexMatches;
|
|
};
|
|
|
|
const gitLink = function (
|
|
hostArg: string,
|
|
userArg: string,
|
|
repoArg: string,
|
|
tokenArg = '',
|
|
linkTypeArg: 'https' | 'ssh'
|
|
): string {
|
|
if (tokenArg !== '') {
|
|
tokenArg = tokenArg + '@';
|
|
}
|
|
switch (linkTypeArg) {
|
|
case 'https':
|
|
return 'https://' + tokenArg + hostArg + '/' + userArg + '/' + repoArg + '.git';
|
|
case 'ssh':
|
|
return 'git@' + hostArg + ':' + userArg + '/' + repoArg + '.git';
|
|
}
|
|
};
|