repositoryInfo.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getRepositoryInfo = void 0;
  4. const promise_1 = require("builder-util/out/promise");
  5. const fs_extra_1 = require("fs-extra");
  6. const hosted_git_info_1 = require("hosted-git-info");
  7. const path = require("path");
  8. function getRepositoryInfo(projectDir, metadata, devMetadata) {
  9. return _getInfo(projectDir, (devMetadata == null ? null : devMetadata.repository) || (metadata == null ? null : metadata.repository));
  10. }
  11. exports.getRepositoryInfo = getRepositoryInfo;
  12. async function getGitUrlFromGitConfig(projectDir) {
  13. const data = await (0, promise_1.orNullIfFileNotExist)((0, fs_extra_1.readFile)(path.join(projectDir, ".git", "config"), "utf8"));
  14. if (data == null) {
  15. return null;
  16. }
  17. const conf = data.split(/\r?\n/);
  18. const i = conf.indexOf('[remote "origin"]');
  19. if (i !== -1) {
  20. let u = conf[i + 1];
  21. if (!/^\s*url =/.exec(u)) {
  22. u = conf[i + 2];
  23. }
  24. if (/^\s*url =/.exec(u)) {
  25. return u.replace(/^\s*url = /, "");
  26. }
  27. }
  28. return null;
  29. }
  30. async function _getInfo(projectDir, repo) {
  31. if (repo != null) {
  32. return parseRepositoryUrl(typeof repo === "string" ? repo : repo.url);
  33. }
  34. const slug = process.env.TRAVIS_REPO_SLUG || process.env.APPVEYOR_REPO_NAME;
  35. if (slug != null) {
  36. const splitted = slug.split("/");
  37. return {
  38. user: splitted[0],
  39. project: splitted[1],
  40. };
  41. }
  42. const user = process.env.CIRCLE_PROJECT_USERNAME;
  43. const project = process.env.CIRCLE_PROJECT_REPONAME;
  44. if (user != null && project != null) {
  45. return {
  46. user,
  47. project,
  48. };
  49. }
  50. const url = await getGitUrlFromGitConfig(projectDir);
  51. return url == null ? null : parseRepositoryUrl(url);
  52. }
  53. function parseRepositoryUrl(url) {
  54. const info = (0, hosted_git_info_1.fromUrl)(url);
  55. if (info == null) {
  56. return null;
  57. }
  58. delete info.protocols;
  59. delete info.treepath;
  60. delete info.filetemplate;
  61. delete info.bugstemplate;
  62. delete info.gittemplate;
  63. delete info.tarballtemplate;
  64. delete info.sshtemplate;
  65. delete info.sshurltemplate;
  66. delete info.browsetemplate;
  67. delete info.docstemplate;
  68. delete info.httpstemplate;
  69. delete info.shortcuttemplate;
  70. delete info.pathtemplate;
  71. delete info.pathmatch;
  72. delete info.protocols_re;
  73. delete info.committish;
  74. delete info.default;
  75. delete info.opts;
  76. delete info.browsefiletemplate;
  77. delete info.auth;
  78. return info;
  79. }
  80. //# sourceMappingURL=repositoryInfo.js.map