Skip to content

Commit 190903e

Browse files
committed
feat: refactor code & add changlog
1 parent c2ede89 commit 190903e

28 files changed

+7055
-6857
lines changed

README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ const { contributors } = useAddonGitLog()
6767

6868
Regarding the full `contributors` parameter:
6969

70-
| Name | Type | Description |
71-
| ---- | ---- | ---- |
72-
| name | `string` | Contributor's name |
73-
| email | `string` | Contributor's email |
70+
| Name | Type | Description |
71+
| ------ | -------- | ------------------------------------------------------------------ |
72+
| name | `string` | Contributor's name |
73+
| email | `string` | Contributor's email |
7474
| avatar | `string` | Contributor's avatar URL, obtained through gravatar based on email |
75-
| count | `number` | Number of contributions |
75+
| count | `number` | Number of contributions |
7676

7777
## Configuration / Options
7878

@@ -82,7 +82,6 @@ In your project (wether theme or addon), you can write this in `valaxy.config.ts
8282
export default defineValaxyConfig<ThemeConfig>({
8383
addons: [
8484
addonGitLog({
85-
debug: false,
8685
contributor: {
8786
mode: 'log',
8887
// logArgs: '--first-parent --follow',
@@ -92,12 +91,11 @@ export default defineValaxyConfig<ThemeConfig>({
9291
})
9392
```
9493

95-
| Name | Type | Default | Description |
96-
| ---- | ---- | ---- | ---- |
97-
| repositoryUrl | `string` | `undefined` | The URL of the repository. |
98-
| contributor.mode | `'api'` \| `'log'` \| `'shortLog'` | `'api'` | The method to generate Git information. |
99-
| contributor.logArgs | `string` | `''` | Additional arguments for `git log` command. |
100-
| debug | `boolean` | `undefined` | Enable debug mode. |
94+
| Name | Type | Default | Description |
95+
| ------------------- | ---------------------------------- | ----------- | ------------------------------------------- |
96+
| repositoryUrl | `string` | `undefined` | The URL of the repository. |
97+
| contributor.mode | `'api'` \| `'log'` \| `'shortLog'` | `'api'` | The method to generate Git information. |
98+
| contributor.logArgs | `string` | `''` | Additional arguments for `git log` command. |
10199

102100
Besides the `api` method, the `mode` option also includes `log` and `shortLog` methods. These methods allow you to generate Git information during build time, with the `git log` command by default adding the `--no-merges` parameter.
103101

client/composable.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

client/composables/changelog.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { MaybeRefOrGetter, Ref } from 'vue'
2+
import type { ChangeLog } from '../../types'
3+
import { computedAsync } from '@vueuse/core'
4+
import { toRef } from 'vue'
5+
import { useAddonGitLogConfig } from '../options'
6+
import { useGitLog } from './gitlog'
7+
8+
export function useChangeLog(_path?: MaybeRefOrGetter<string>): Ref<ChangeLog[]> {
9+
const gitLog = useGitLog()
10+
const gitLogOptions = useAddonGitLogConfig()
11+
12+
if (gitLogOptions.value.contributor?.mode !== 'api')
13+
return toRef([])
14+
15+
const contributors = computedAsync<ChangeLog[]>(
16+
async () => {
17+
// TODO: Complete the API-based method
18+
return []
19+
},
20+
gitLog.value.changeLog,
21+
{ lazy: true },
22+
)
23+
24+
return contributors
25+
}

client/composables/contributor.ts

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,28 @@
1-
import type { MaybeRefOrGetter } from 'vue'
1+
import type { MaybeRefOrGetter, Ref } from 'vue'
22
import type { Contributor } from '../../types'
3-
import { Octokit } from '@octokit/rest'
4-
import { computedAsync, isClient } from '@vueuse/core'
5-
import gravatar from 'gravatar'
6-
import { useFrontmatter } from 'valaxy'
7-
import { computed, toValue } from 'vue'
8-
import { useAddonGitLogConfig } from '..'
3+
import { computedAsync } from '@vueuse/core'
4+
import { toRef, toValue } from 'vue'
95
import { parseGithubUrl } from '../../utils'
6+
import { useAddonGitLogConfig } from '../options'
7+
import { fetchContributors } from '../services'
8+
import { useGitLog } from './gitlog'
109

11-
const octokit = new Octokit()
12-
export function useAddonGitLogContributor(userPath?: MaybeRefOrGetter<string>) {
13-
if (!isClient)
14-
return
15-
16-
const frontmatter = useFrontmatter()
10+
export function useContributor(path?: MaybeRefOrGetter<string>): Ref<Contributor[]> {
11+
const gitLog = useGitLog()
1712
const gitLogOptions = useAddonGitLogConfig()
1813

19-
const gitLog = computed(() => frontmatter.value.git_log || {
20-
contributors: [],
21-
})
22-
2314
if (gitLogOptions.value.contributor?.mode !== 'api')
24-
return
25-
26-
const { owner, repo } = parseGithubUrl(gitLogOptions.value.repositoryUrl!)
27-
28-
const autoPath = gitLog.value.path
15+
return toRef([])
2916

3017
const contributors = computedAsync<Contributor[]>(
3118
async () => {
32-
const path = toValue(userPath || autoPath)
33-
return await fetchCommits(owner, repo, path)
19+
const { owner, repo } = parseGithubUrl(gitLogOptions.value.repositoryUrl!)
20+
const _path = toValue(path || gitLog.value.path)
21+
return await fetchContributors(owner, repo, _path)
3422
},
3523
gitLog.value.contributors,
3624
{ lazy: true },
3725
)
3826

3927
return contributors
4028
}
41-
42-
async function fetchCommits(owner: string, repo: string, path: string): Promise<Contributor[]> {
43-
let contributors: Contributor[] = []
44-
45-
try {
46-
const { data } = await octokit.repos.listCommits({ owner, repo, path })
47-
const contributorMap: { [key: string]: Contributor } = {}
48-
49-
data.forEach(({ author, commit }) => {
50-
const name = author?.name || author?.login || commit.author?.name || 'Unknown Contributor'
51-
const email = author?.email || commit.author?.email
52-
53-
if (!email)
54-
return
55-
56-
const github = author?.login ? `https://github.com/${author?.login}` : null
57-
const avatar = author?.avatar_url || gravatar.url(email, { d: 'wavatar' })
58-
59-
if (contributorMap[name])
60-
contributorMap[name].count += 1
61-
else
62-
contributorMap[name] = { count: 1, name, email, avatar, github }
63-
})
64-
65-
contributors = Object.values(contributorMap)
66-
67-
// sort by commit count
68-
contributors.sort((a: any, b: any) => b.count - a.count)
69-
}
70-
catch (error) {
71-
console.error(`valaxy-addon-git-log: ${error}`)
72-
}
73-
74-
return contributors
75-
}

client/composables/gitlog.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Ref } from 'vue'
2+
import type { GitLog } from '../../types'
3+
import { isClient } from '@vueuse/core'
4+
import { useFrontmatter } from 'valaxy'
5+
import { computed, toRef } from 'vue'
6+
7+
export function useGitLog(): Ref<GitLog> {
8+
const initGitLog: GitLog = {
9+
contributors: [],
10+
changeLog: [],
11+
path: '',
12+
}
13+
14+
if (!isClient)
15+
return toRef(initGitLog)
16+
17+
const frontmatter = useFrontmatter()
18+
19+
const gitLog = computed<GitLog>(() => frontmatter.value.git_log || initGitLog)
20+
21+
return gitLog
22+
}

client/composables/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export * from './changelog'
12
export * from './contributor'
3+
export * from './gitlog'

client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './composable'
21
export * from './composables'
32
export * from './options'
3+
export * from './services'

client/options.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import { computed } from 'vue'
21
import type { ValaxyAddon } from 'valaxy'
3-
import { useRuntimeConfig } from 'valaxy'
42
import type { GitLogOptions } from '../types'
3+
import { useRuntimeConfig } from 'valaxy'
4+
import { computed } from 'vue'
55

66
export function useAddonGitLogConfig() {
77
const runtimeConfig = useRuntimeConfig()
88
return computed<GitLogOptions>(() => {
99
const options = (runtimeConfig.value.addons['valaxy-addon-git-log'] as ValaxyAddon<GitLogOptions>).options
1010

11-
return {
12-
...options,
13-
contributor: {
14-
mode: 'api',
15-
},
16-
}
11+
return { ...options }
1712
})
1813
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { Contributor } from '../../types'
2+
import { Octokit } from '@octokit/rest'
3+
import gravatar from 'gravatar'
4+
import md5 from 'md5'
5+
6+
const octokit = new Octokit()
7+
8+
export async function fetchContributors(owner: string, repo: string, path: string): Promise<Contributor[]> {
9+
let contributors: Contributor[] = []
10+
11+
try {
12+
const { data } = await octokit.repos.listCommits({ owner, repo, path })
13+
const contributorMap: { [key: string]: Contributor } = {}
14+
15+
data.forEach(({ author, commit }) => {
16+
const name = author?.name || author?.login || commit.author?.name || 'Unknown Contributor'
17+
const email = author?.email || commit.author?.email
18+
19+
if (!email)
20+
return
21+
22+
const github = author?.login ? `https://github.com/${author?.login}` : null
23+
const avatar = author?.avatar_url || gravatar.url(email, { d: 'wavatar' })
24+
const hash = md5(email)
25+
26+
if (contributorMap[name])
27+
contributorMap[name].count += 1
28+
else
29+
contributorMap[name] = { count: 1, name, email, avatar, hash, github }
30+
})
31+
32+
contributors = Object.values(contributorMap)
33+
34+
// sort by commit count
35+
contributors.sort((a, b) => b.count - a.count)
36+
}
37+
catch (error) {
38+
console.error(`valaxy-addon-git-log: ${error}`)
39+
}
40+
41+
return contributors
42+
}

client/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './fetchContributors'

0 commit comments

Comments
 (0)