Skip to content

Commit 333c592

Browse files
Merge pull request #6 from AbhyudayaSharma/tsdoc
Documentation for Typescript types
2 parents 006e043 + 847038e commit 333c592

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

.editorconfig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
root = true
2+
13
[*]
2-
indent_style = space
4+
tab_width = 2
5+
charset = utf-8
36
indent_size = 2
7+
end_of_line = lf
8+
indent_style = space
9+
max_line_length = 120
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
node_modules/
2+
3+
# IntelliJ IDEA files
4+
.idea/

macro.d.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1+
/**
2+
* A Babel plugin macro giving access to git information at compile time.
3+
*/
14
declare module "react-git-info/macro" {
5+
/**
6+
* Information about a git commit.
7+
*/
28
export interface GitCommitInformation {
9+
/**
10+
* Date of the commit as a string in strict ISO 8601 format.
11+
*/
312
readonly date: string;
13+
/**
14+
* Full hash of the latest commit on the active branch.
15+
*/
416
readonly hash: string;
17+
/**
18+
* Raw commit message.
19+
*/
520
readonly message: string;
21+
/**
22+
* Abbreviated commit hash with length defined as `core.abbrev` in
23+
* {@link https://git-scm.com/docs/git-config | git-config}.
24+
*/
625
readonly shortHash: string;
726
}
827

928
export interface GitInformation {
29+
/**
30+
* Tags pointing to the current commit.
31+
*/
1032
readonly tags: string[];
11-
readonly branch: string;
33+
/**
34+
* The current git branch. `undefined` if the repository is in a detached HEAD state.
35+
*/
36+
readonly branch?: string;
37+
/**
38+
* Information about the commit pointed to by `HEAD`.
39+
*/
1240
readonly commit: GitCommitInformation;
1341
}
1442

43+
/**
44+
* Returns information about the current Git state.
45+
*/
1546
export default function GitInfo(): GitInformation;
1647
}

src/GitInfo.macro.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
const { createMacro } = require('babel-plugin-macros');
22
const { execSync } = require('child_process');
33

4-
const parseGitLog = (() => {
5-
let message = '';
6-
let refs = '';
4+
const parsedGitLog = (() => {
5+
let message;
6+
let refs;
77
const commit = {};
88
// only the commit message can have multiple lines. Make sure to always add at the end:
9+
// The format is specified in https://git-scm.com/docs/git-log#_pretty_formats
910
const logResult = execSync('git log --format="%D%n%h%n%H%n%cI%n%B" -n 1 HEAD')
1011
.toString()
1112
.trim()
1213
.split(/\r?\n/);
1314
[refs, commit.shortHash, commit.hash, commit.date, ...message] = logResult;
14-
commit.message = message.join("\n");
15+
commit.message = message.join('\n');
1516
return {refs, commit};
1617
})();
1718

1819
const parseRefs = (refs) => {
19-
let branch;
20+
let branch = undefined;
2021
const tags = [];
21-
refs.split(", ").map((item) => {
22-
const isBranch = item.match(/HEAD -> (.*)/);
23-
const isTag = item.match(/tag: (.*)/);
22+
refs.split(', ').map((item) => {
23+
// if HEAD is not detached, the branch is printed out as `HEAD -> branch_name`.
24+
// if HEAD is detached, the output becomes `HEAD`.
25+
const isBranch = item.match(/^HEAD -> (.*)$/);
26+
const isTag = item.match(/^tag: (.*)$/);
2427

2528
if (isTag && isTag.length > 1) {
2629
tags.push(isTag[1]);
2730
} else {
2831
branch = isBranch ? isBranch[1] : branch;
2932
}
3033
});
34+
3135
return [branch, tags];
3236
};
3337

3438
const gitInfo = (() => {
3539
const ret = {};
3640
try {
37-
const logResult = parseGitLog;
41+
const logResult = parsedGitLog;
3842
[ret.branch, ret.tags] = parseRefs(logResult.refs);
3943
ret.commit = logResult.commit;
4044
} catch (e) {

0 commit comments

Comments
 (0)