rafinskipg/git-changelog

View on GitHub
tasks/lib/get-git-repo-info.js

Summary

Maintainability
A
35 mins
Test Coverage
A
100%
function getGitRepoInfo(initialUrl) {
  const [
    initial, 
    protocol, 
    service, 
    delimiter, 
    repo,
    paths,
  ] = /(git@|https?:\/\/|https?:\/\/|ssh?:\/\/)([a-zA-Z0-9\.\-_]+)(\/|:)([a-zA-Z0-9\-]+)(.*)(\.git)/g.exec(initialUrl) || [];
  const isHttp = /http(s)?/.test(protocol);
  const isPort = /[0-9]/g.test(repo);
  const portChunk = isPort ? `:${repo}` : '';
  const repoChunk = isPort ? '' : `/${repo}`;
  
  const fullUrl = isHttp ? 
    initial.replace(/\.git+/g, '') : 
    `https://${service}${portChunk}${repoChunk}${paths}`;

  const result = { 
    initialUrl, 
    protocol, 
    service,
    port: isPort ? repo : '', 
    repo: isPort ? '' : repo, 
    repoUrl: service ? fullUrl : initialUrl 
  };

  return result;
}

module.exports = getGitRepoInfo;