UNPKG

1.42 kBJavaScriptView Raw
1const { getGitBranch } = require('./git-api')
2const debug = require('debug')('commit-info')
3
4function firstFoundValue (keys, object = process.env) {
5 const found = keys.find(key => {
6 return key in object
7 })
8 return found ? object[found] : null
9}
10
11const getValue = name => o => {
12 if (name in o) {
13 return o[name]
14 }
15 return null
16}
17
18/**
19 * Uses "git" command to find the current branch
20 *
21 * @param {string} pathToRepo
22 * @returns {Promise<string|null>} Resolves with Git branch or null
23 */
24function getBranch (pathToRepo) {
25 pathToRepo = pathToRepo || process.cwd()
26 debug('using Git tool to find branch')
27 return getGitBranch(pathToRepo)
28}
29
30/**
31 * Looks up commit information from environment keys.
32 */
33function getCommitInfoFromEnvironment (env = process.env) {
34 return {
35 branch: getValue('COMMIT_INFO_BRANCH')(env),
36 message: getValue('COMMIT_INFO_MESSAGE')(env),
37 email: getValue('COMMIT_INFO_EMAIL')(env),
38 author: getValue('COMMIT_INFO_AUTHOR')(env),
39 sha: getValue('COMMIT_INFO_SHA')(env),
40 timestamp: getValue('COMMIT_INFO_TIMESTAMP')(env),
41 remote: getValue('COMMIT_INFO_REMOTE')(env)
42 }
43}
44
45/**
46 * Returns list of Git properties that this module searches for
47 */
48function getFields () {
49 return ['branch', 'message', 'email', 'author', 'sha', 'remote', 'timestamp']
50}
51
52module.exports = {
53 firstFoundValue,
54 getBranch,
55 getCommitInfoFromEnvironment,
56 getFields
57}