UNPKG

2.39 kBJavaScriptView Raw
1"use strict";
2var __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++) if (e.indexOf(p[i]) < 0)
8 t[p[i]] = s[p[i]];
9 return t;
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const path = require("path");
13const fs = require("fs");
14const ci = require("env-ci");
15const git_parse_1 = require("git-parse");
16const git = require("git-rev-sync");
17const lodash_1 = require("lodash");
18const findGitRoot = (start) => {
19 start = start || process.cwd();
20 if (typeof start === "string") {
21 if (start[start.length - 1] !== path.sep)
22 start += path.sep;
23 start = start.split(path.sep);
24 }
25 if (!start.length)
26 return;
27 start.pop();
28 const dir = start.join(path.sep);
29 if (fs.existsSync(path.join(dir, ".git"))) {
30 return path.normalize(dir);
31 }
32 else {
33 return findGitRoot(start);
34 }
35};
36exports.gitInfo = async () => {
37 const { isCi, commit, branch: ciBranch, slug, root, prBranch } = ci();
38 const gitLoc = root ? root : findGitRoot();
39 if (!commit)
40 return;
41 let committer;
42 let branch = ciBranch || prBranch;
43 let remoteUrl = slug || process.env.BUILD_REPOSITORY_ID;
44 let message;
45 if (gitLoc) {
46 const _a = await git_parse_1.gitToJs(gitLoc)
47 .then((commits) => commits && commits.length > 0
48 ? commits[0]
49 : { authorName: null, authorEmail: null, message: null })
50 .catch(() => ({ authorEmail: null, authorName: null, message: null })), { authorName, authorEmail } = _a, commit = __rest(_a, ["authorName", "authorEmail"]);
51 committer = `${authorName || ""} ${authorEmail ? `<${authorEmail}>` : ""}`.trim();
52 message = commit.message;
53 if (!isCi) {
54 try {
55 remoteUrl = git.remoteUrl();
56 }
57 catch (e) { }
58 }
59 if (!branch) {
60 branch = git.branch();
61 }
62 }
63 return lodash_1.pickBy({
64 committer,
65 commit,
66 remoteUrl,
67 message,
68 branch
69 }, lodash_1.identity);
70};
71//# sourceMappingURL=git.js.map
\No newline at end of file