UNPKG

1.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const fs = require("fs");
5const ci = require("env-ci");
6const git_parse_1 = require("git-parse");
7const git = require("git-rev-sync");
8const lodash_1 = require("lodash");
9const findGitRoot = (start) => {
10 start = start || process.cwd();
11 if (typeof start === "string") {
12 if (start[start.length - 1] !== path.sep)
13 start += path.sep;
14 start = start.split(path.sep);
15 }
16 if (!start.length)
17 return;
18 start.pop();
19 const dir = start.join(path.sep);
20 if (fs.existsSync(path.join(dir, ".git"))) {
21 return path.normalize(dir);
22 }
23 else {
24 return findGitRoot(start);
25 }
26};
27exports.gitInfo = async () => {
28 const { isCi, commit, slug, root } = ci();
29 const gitLoc = root ? root : findGitRoot();
30 if (!commit)
31 return;
32 let committer;
33 let remoteUrl = slug;
34 if (gitLoc) {
35 const { authorName, authorEmail } = await git_parse_1.gitToJs(gitLoc)
36 .then((commits) => commits && commits.length > 0
37 ? commits[0]
38 : { authorName: null, authorEmail: null })
39 .catch(() => ({ authorEmail: null, authorName: null }));
40 committer = `${authorName || ""} ${authorEmail ? `<${authorEmail}>` : ""}`.trim();
41 if (!isCi) {
42 try {
43 remoteUrl = git.remoteUrl();
44 }
45 catch (e) { }
46 }
47 }
48 return lodash_1.pickBy({ committer, commit, remoteUrl }, lodash_1.identity);
49};
50//# sourceMappingURL=git.js.map
\No newline at end of file