Skip to content

Commit b781591

Browse files
committed
feat: extend git return things
1 parent 57aacc3 commit b781591

File tree

6 files changed

+106
-24
lines changed

6 files changed

+106
-24
lines changed

examples/vite/env.d.ts

+47
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1+
declare module '~build/git' {
2+
/** Github repo url */
3+
export const github: string | null;
4+
5+
/** The current branch */
6+
export const branch: string;
7+
8+
/** SHA of the current commit */
9+
export const sha: string;
10+
11+
/** The first 10 chars of the current SHA */
12+
export const abbreviatedSha: string;
13+
14+
/** The tag for the current SHA (or `null` if no tag exists) */
15+
export const tag: string | null;
16+
17+
/** The tags for the current SHA*/
18+
export const tags: string[];
19+
20+
/** Tag for the closest tagged ancestor (or `null` if no ancestor is tagged) */
21+
export const lastTag: string | null;
22+
23+
/** The committer of the current SHA */
24+
export const committer: string;
25+
26+
/** The committer email of the current SHA */
27+
export const committerEmail: string;
28+
29+
/** The commit date of the current SHA */
30+
export const committerDate: string;
31+
32+
/** The author for the current SHA */
33+
export const author: string;
34+
35+
/** The author email for the current SHA */
36+
export const authorEmail: string;
37+
38+
/** The authored date for the current SHA */
39+
export const authorDate: string;
40+
41+
/** The commit message for the current SHA */
42+
export const commitMessage: string;
43+
44+
/** Gets whether this represents a clean working branch. */
45+
export const isClean: boolean;
46+
}
47+
148
declare module '~build/meta' {
249
export const message: string;
350
}

examples/vite/main.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
author,
1515
authorEmail,
1616
authorDate,
17-
commitMessage
17+
commitMessage,
18+
isClean
1819
} from '~build/git';
1920
import { message } from '~build/meta';
2021
import { name, version } from '~build/package';
@@ -59,6 +60,7 @@ append('Author: ', author);
5960
append('Author Email: ', authorEmail);
6061
append('Author Date: ', authorDate);
6162
append('Commit Message: ', commitMessage);
63+
append('Is Clean: ', isClean ? 'true' : 'false');
6264

6365
append('Message: ', message);
6466

examples/vite/vite.config.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,15 @@ import BuildInfo from '../../src/vite';
44

55
export default defineConfig({
66
base: './',
7-
plugins: [BuildInfo({ meta: { message: 'This is set from vite.config.ts' } })]
7+
plugins: [
8+
BuildInfo({
9+
git: {
10+
isClean: async (git) => {
11+
const status = await git.status();
12+
return status.isClean();
13+
}
14+
},
15+
meta: { message: 'This is set from vite.config.ts' }
16+
})
17+
]
818
});

src/core/git.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,35 @@ import { type SimpleGit, simpleGit } from 'simple-git';
22

33
import parseGitUrl from 'git-url-parse';
44

5-
export async function getRepoInfo(root: string) {
5+
import type { Options } from './types';
6+
7+
export async function getRepoInfo(root: string, extra: Options['git'] = {}) {
68
const git = simpleGit(root);
79

810
if (!(await git.checkIsRepo())) {
911
return undefined;
1012
}
1113

12-
const [branch, currentCommit, commiter, tags, github] = await Promise.all([
14+
const [branch, currentCommit, commiter, tags, github, result] = await Promise.all([
1315
getBranch(git),
1416
getCommit(git),
1517
getCommitter(git),
1618
getTags(git),
17-
getGitHubUrl(git)
19+
getGitHubUrl(git),
20+
Promise.all(
21+
Object.entries(extra).map(async ([key, fn]) => {
22+
return [key, await fn(git)] as const;
23+
})
24+
)
1825
] as const);
1926

2027
return {
2128
...branch,
2229
...currentCommit,
2330
...commiter,
2431
...tags,
25-
...github
32+
...github,
33+
...Object.fromEntries(result)
2634
};
2735
}
2836

src/core/index.ts

+26-18
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ export const UnpluginInfo = createUnplugin<Options | undefined>((option) => {
4747
if (id === ModuleName.BuildTime) {
4848
return `const time = new Date(${now.getTime()})\n` + 'export default time';
4949
} else if (id === ModuleName.BuildInfo || id === ModuleName.BuildGit) {
50-
const info = await getRepoInfo(root);
51-
const github = option?.github ?? info?.github;
50+
const info = await getRepoInfo(root, option?.git);
51+
if (info && option?.github) {
52+
info.github = option.github;
53+
}
5254

5355
if (id === ModuleName.BuildInfo) {
5456
this.warn(
@@ -59,28 +61,34 @@ export const UnpluginInfo = createUnplugin<Options | undefined>((option) => {
5961
this.warn('This may not be a git repo');
6062
}
6163

62-
const gen = (key: keyof NonNullable<typeof info>) => {
63-
return `export const ${key} = ${info ? JSON.stringify(info[key]) : 'undefined'}`;
64+
const keys = [
65+
...new Set([
66+
'github',
67+
'sha',
68+
'abbreviatedSha',
69+
'branch',
70+
'tag',
71+
'tags',
72+
'lastTag',
73+
'author',
74+
'authorEmail',
75+
'authorDate',
76+
'committer',
77+
'committerEmail',
78+
'committerDate',
79+
'commitMessage',
80+
...Object.keys(option?.git ?? {})
81+
])
82+
];
83+
const gen = (key: string) => {
84+
return `export const ${key} = ${info ? JSON.stringify((info as any)[key]) : 'null'}`;
6485
};
6586

6687
return [
6788
id === ModuleName.BuildInfo
6889
? `export const CI = ${ci.isCI ? `"${ci.name}"` : 'null'}`
6990
: ``,
70-
`export const github = ${JSON.stringify(github ?? null)}`,
71-
gen('sha'),
72-
gen('abbreviatedSha'),
73-
gen('branch'),
74-
gen('tag'),
75-
gen('tags'),
76-
gen('lastTag'),
77-
gen('author'),
78-
gen('authorEmail'),
79-
gen('authorDate'),
80-
gen('committer'),
81-
gen('committerEmail'),
82-
gen('committerDate'),
83-
gen('commitMessage')
91+
...keys.map((key) => gen(key))
8492
].join('\n');
8593
} else if (id === ModuleName.BuildCI) {
8694
return [

src/core/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { SimpleGit } from 'simple-git';
2+
13
export interface Options {
24
/**
35
* Git repo root path
@@ -9,6 +11,11 @@ export interface Options {
911
*/
1012
github?: string;
1113

14+
/**
15+
* Custom the exported fields in ~build/git
16+
*/
17+
git?: Record<string, (git: SimpleGit) => Promise<any> | any>;
18+
1219
/**
1320
* Custom virtual module prefix
1421
*

0 commit comments

Comments
 (0)