1 | "use strict";
|
2 | var __rest = (this && this.__rest) || function (s, e) {
|
3 | var t = {};
|
4 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
5 | t[p] = s[p];
|
6 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
7 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
8 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
9 | t[p[i]] = s[p[i]];
|
10 | }
|
11 | return t;
|
12 | };
|
13 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
14 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
15 | };
|
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
17 | const path_1 = __importDefault(require("path"));
|
18 | const fs_1 = __importDefault(require("fs"));
|
19 | const env_ci_1 = __importDefault(require("env-ci"));
|
20 | const git_parse_1 = require("git-parse");
|
21 | const git_rev_sync_1 = __importDefault(require("git-rev-sync"));
|
22 | const lodash_pickby_1 = __importDefault(require("lodash.pickby"));
|
23 | const lodash_identity_1 = __importDefault(require("lodash.identity"));
|
24 | const findGitRoot = (start) => {
|
25 | start = start || process.cwd();
|
26 | if (typeof start === "string") {
|
27 | if (start[start.length - 1] !== path_1.default.sep)
|
28 | start += path_1.default.sep;
|
29 | start = start.split(path_1.default.sep);
|
30 | }
|
31 | if (!start.length)
|
32 | return;
|
33 | start.pop();
|
34 | const dir = start.join(path_1.default.sep);
|
35 | if (fs_1.default.existsSync(path_1.default.join(dir, ".git"))) {
|
36 | return path_1.default.normalize(dir);
|
37 | }
|
38 | else {
|
39 | return findGitRoot(start);
|
40 | }
|
41 | };
|
42 | exports.gitInfo = async (log) => {
|
43 | const { commit, branch: ciBranch, root, prBranch } = env_ci_1.default();
|
44 | const gitLoc = root ? root : findGitRoot();
|
45 | if (!commit)
|
46 | return;
|
47 | let committer, remoteUrl, message;
|
48 | let branch = ciBranch || prBranch;
|
49 | if (gitLoc) {
|
50 | const _a = await git_parse_1.gitToJs(gitLoc)
|
51 | .then((commits) => commits && commits.length > 0
|
52 | ? commits[0]
|
53 | : { authorName: null, authorEmail: null, message: null })
|
54 | .catch(() => ({ authorEmail: null, authorName: null, message: null })), { authorName, authorEmail } = _a, commit = __rest(_a, ["authorName", "authorEmail"]);
|
55 | committer = `${authorName || ""} ${authorEmail ? `<${authorEmail}>` : ""}`.trim();
|
56 | message = commit.message;
|
57 | try {
|
58 | remoteUrl = git_rev_sync_1.default.remoteUrl();
|
59 | }
|
60 | catch (e) {
|
61 | log(["Unable to retrieve remote url, failed with:", e].join("\n\n"));
|
62 | }
|
63 | if (!branch) {
|
64 | branch = git_rev_sync_1.default.branch([gitLoc]);
|
65 | }
|
66 | }
|
67 | return lodash_pickby_1.default({
|
68 | committer,
|
69 | commit,
|
70 | remoteUrl,
|
71 | message,
|
72 | branch
|
73 | }, lodash_identity_1.default);
|
74 | };
|
75 |
|
\ | No newline at end of file |