Skip to content

Commit abb407d

Browse files
committed
chore: add CI, tests, tsconfig and improve code quality
- Add GitHub Actions CI workflow (lint, test, typecheck) - Add unit tests for gitLog, github utils, and render utils - Add tsconfig.json, env.d.ts, vitest.config.ts, eslint.config.js - Extract regex constants for performance (RE_*) across utils and node modules - Fix committer_date field mapping in gitLog and contributor parsing - Make repositoryUrl configurable instead of hardcoding vueuse - Add null check for repo in renderCommitMessage - Fix type documentation format in README
1 parent 9e063ec commit abb407d

File tree

19 files changed

+5440
-2221
lines changed

19 files changed

+5440
-2221
lines changed

.github/workflows/ci.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
9+
pull_request:
10+
branches:
11+
- main
12+
- master
13+
14+
jobs:
15+
lint:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- uses: pnpm/action-setup@v4
21+
22+
- uses: actions/setup-node@v4
23+
with:
24+
node-version: lts/*
25+
cache: pnpm
26+
27+
- run: pnpm install --frozen-lockfile
28+
- run: pnpm test
29+
- run: pnpm lint
30+
- run: pnpm typecheck

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cSpell.words": [
3+
"valaxy"
4+
]
5+
}

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ export interface Contributor {
161161
count: number
162162
github: string | null
163163
hash: string
164-
}[]
164+
}
165+
166+
// type: Contributor[]
165167
```
166168

167169
| Name | Type | Description |
@@ -194,7 +196,9 @@ export interface Changelog {
194196
body?: string
195197
author_name: string
196198
author_email: string
197-
}[]
199+
}
200+
201+
// type: Changelog[]
198202
```
199203

200204
| Name | Type | Description |

components/GitLogChangelog.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
import { functions } from '@vueuse/metadata'
33
import changelog from 'virtual:git-log/changelog'
44
import { computed } from 'vue'
5+
import { useAddonGitLogConfig } from '../client'
56
import { renderCommitMessage } from '../utils'
67
78
const props = defineProps<{ fn: string }>()
9+
const gitLogOptions = useAddonGitLogConfig()
10+
const repositoryUrl = computed(() => gitLogOptions.value.repositoryUrl || '')
11+
812
const info = computed(() => functions.find(i => i.name === props.fn))
913
1014
const names = computed(() => [props.fn, ...info.value?.alias || []])
@@ -42,7 +46,7 @@ const commits = computed(() => {
4246
</div>
4347
<div>
4448
<a
45-
:href="`https://github.com/vueuse/vueuse/releases/tag/${commit.version}`"
49+
:href="`${repositoryUrl}/releases/tag/${commit.version}`"
4650
target="_blank"
4751
>
4852
<code class="!text-primary font-bold">{{ commit.version }}</code>
@@ -53,12 +57,12 @@ const commits = computed(() => {
5357
<template v-else>
5458
<octicon-git-commit-16 class="m-auto rotate-90 transform opacity-30" />
5559
<div>
56-
<a :href="`https://github.com/vueuse/vueuse/commit/${commit.hash}`" target="_blank">
60+
<a :href="`${repositoryUrl}/commit/${commit.hash}`" target="_blank">
5761
<code class="!hover:text-primary !text-xs !text-$vp-c-text-2">{{ commit.hash.slice(0, 5) }}</code>
5862
</a>
5963
<span text="sm">
6064
-
61-
<span v-html="renderCommitMessage(commit.message.replace(`(${fn})`, ''))" />
65+
<span v-html="renderCommitMessage(commit.message.replace(`(${fn})`, ''), repositoryUrl)" />
6266
</span>
6367
</div>
6468
</template>

env.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export {}
2+
3+
declare module 'valaxy' {
4+
import type { ComputedRef } from 'vue'
5+
6+
interface ValaxyAddon<AddonOptions = Record<string, any>> {
7+
name: string
8+
global?: boolean
9+
props?: Record<string, any>
10+
options?: AddonOptions
11+
}
12+
13+
interface RuntimeConfig {
14+
addons: Record<string, ValaxyAddon>
15+
}
16+
17+
export function useFrontmatter<T extends Record<string, any> = Record<string, any>>(): ComputedRef<T>
18+
export function useRuntimeConfig(): ComputedRef<RuntimeConfig>
19+
}

eslint.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @ts-check
2+
import antfu from '@antfu/eslint-config'
3+
4+
export default antfu()

node/changeLog.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { git } from '.'
44

55
let cache: Changelog[] | undefined
66

7+
const RE_BACKSLASH = /\\/g
8+
const RE_PACKAGE_PATH = /^packages\/\w+\/(\w+)\/\w+\.ts$/
9+
710
/**
811
* Separator used to split combined git log output into per-commit blocks.
912
*/
@@ -63,10 +66,10 @@ export async function getChangelog(maxCount = 200, path?: string) {
6366
}
6467
else {
6568
// Changed files are on the remaining non-empty lines
66-
const files = lines.slice(1).filter(Boolean).map(f => f.replace(/\\/g, '/'))
69+
const files = lines.slice(1).filter(Boolean).map(f => f.replace(RE_BACKSLASH, '/'))
6770
log.functions = uniq(
6871
files
69-
.map(i => i.match(/^packages\/\w+\/(\w+)\/\w+\.ts$/)?.[1])
72+
.map(i => i.match(RE_PACKAGE_PATH)?.[1])
7073
.filter(Boolean),
7174
)
7275
}

node/contributor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Contributor, GitLogOptions } from '../types'
2-
import { execSync } from 'node:child_process'
2+
import { execFileSync } from 'node:child_process'
33
import gravatar from 'gravatar'
44
import md5 from 'md5'
55
import { git } from '.'
@@ -9,7 +9,7 @@ export async function getContributors(filePath?: string, options?: GitLogOptions
99
const { contributor } = options || {}
1010

1111
try {
12-
const gitArgs: string[] = ['log', '--no-merges', '--pretty=format:"%an|%ae"']
12+
const gitArgs: string[] = ['log', '--no-merges', '--pretty=format:%an|%ae']
1313

1414
const additionalArgs: string[] = [
1515
filePath && `--`,
@@ -21,7 +21,7 @@ export async function getContributors(filePath?: string, options?: GitLogOptions
2121

2222
const contributorsMap = gitLog
2323
.split('\n')
24-
.map(line => line.slice(1, -1).split('|') as [string, string])
24+
.map(line => line.split('|') as [string, string])
2525
.filter(([_, email]) => email)
2626
.reduce((acc, [name, email]) => {
2727
if (!acc[email]) {
@@ -50,6 +50,6 @@ export async function getContributors(filePath?: string, options?: GitLogOptions
5050
}
5151

5252
export function getLastUpdated(filePath: string) {
53-
const lastUpdated = execSync(`git log -1 --format=%ct ${filePath}`, { encoding: 'utf-8' })
53+
const lastUpdated = execFileSync('git', ['log', '-1', '--format=%ct', '--', filePath], { encoding: 'utf-8' })
5454
return Number.parseInt(lastUpdated, 10) * 1000
5555
}

node/gitLog.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import type { EditableTreeNode } from 'unplugin-vue-router'
1+
import type { EditableTreeNode } from 'vue-router/unplugin'
22
import type { Changelog, Contributor, GitLogFileEntry, GitLogOptions } from '../types'
33
import path from 'node:path'
44
import process from 'node:process'
55
import consola from 'consola'
6+
import fs from 'fs-extra'
67
import gravatar from 'gravatar'
78
import md5 from 'md5'
8-
import fs from 'fs-extra'
99
import { git } from '.'
1010
import { guessGitHubUsername } from '../utils'
1111

12+
const RE_WHITESPACE = /\s+/
13+
14+
interface FrontmatterWithGitLog {
15+
git_log: Record<string, any>
16+
}
17+
18+
function getFrontmatter(route: EditableTreeNode): FrontmatterWithGitLog {
19+
return route.meta.frontmatter as FrontmatterWithGitLog
20+
}
21+
1222
export const destDir = path.resolve(process.cwd(), './public')
1323
// Only allow files from the user's working directory 'pages' folder
1424
export const currentWorkingDirectory = path.join(process.cwd(), 'pages')
@@ -73,8 +83,9 @@ export async function handleGitLogInfo(options: GitLogOptions, route: EditableTr
7383
if (!filePath)
7484
return
7585

76-
if (!route.meta.frontmatter.git_log)
77-
route.meta.frontmatter.git_log = {}
86+
const frontmatter = getFrontmatter(route)
87+
if (!frontmatter.git_log)
88+
frontmatter.git_log = {}
7889

7990
// Ensure basePath is available before computing relative path
8091
let resolvedBase: string
@@ -87,7 +98,7 @@ export async function handleGitLogInfo(options: GitLogOptions, route: EditableTr
8798
}
8899

89100
const gitRelativePath = path.relative(resolvedBase, filePath).split(path.sep).join('/')
90-
route.meta.frontmatter.git_log.path = gitRelativePath
101+
frontmatter.git_log.path = gitRelativePath
91102

92103
if (!isPrebuilt && !isBuildTime)
93104
return
@@ -115,7 +126,7 @@ async function batchGetContributors(resolvedBase: string, filePaths: string[], o
115126
'--no-merges',
116127
'--pretty=format:---COMMIT_SEP---%an|%ae',
117128
'--name-only',
118-
...(contributor?.logArgs ? contributor.logArgs.trim().split(/\s+/) : []),
129+
...(contributor?.logArgs?.trim() ? contributor.logArgs.trim().split(RE_WHITESPACE) : []),
119130
'--',
120131
...filePaths,
121132
]
@@ -301,8 +312,9 @@ export async function flushGitLogBatch(options: GitLogOptions) {
301312
const changeLog = changelogMap.get(filePath) || []
302313

303314
if (isBuildTime) {
304-
route.meta.frontmatter.git_log.contributors = contributors
305-
route.meta.frontmatter.git_log.changeLog = changeLog
315+
const frontmatter = getFrontmatter(route)
316+
frontmatter.git_log.contributors = contributors
317+
frontmatter.git_log.changeLog = changeLog
306318
}
307319

308320
if (isPrebuilt) {

package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "valaxy-addon-git-log",
3+
"type": "module",
34
"version": "0.3.4",
4-
"packageManager": "pnpm@9.15.0",
5+
"packageManager": "pnpm@10.33.0",
56
"description": "Integrates git logs into your page of Valaxy site.",
67
"repository": "https://github.com/valaxyjs/valaxy-addon-git-log",
78
"keywords": [
@@ -13,23 +14,32 @@
1314
"main": "index.ts",
1415
"types": "index.d.ts",
1516
"scripts": {
17+
"lint": "eslint .",
18+
"lint:fix": "eslint . --fix",
19+
"test": "vitest run",
20+
"typecheck": "vue-tsc --noEmit",
1621
"release": "bumpp"
1722
},
1823
"peerDependencies": {
1924
"valaxy": ">=0.22"
2025
},
2126
"dependencies": {
22-
"@octokit/rest": "^21.0.2",
23-
"fs-extra": "^11.3.0",
27+
"@octokit/rest": "^22.0.1",
28+
"fs-extra": "^11.3.4",
2429
"gravatar": "^1.8.2",
2530
"md5": "^2.3.0",
26-
"simple-git": "^3.27.0"
31+
"simple-git": "^3.33.0"
2732
},
2833
"devDependencies": {
34+
"@antfu/eslint-config": "^7.7.3",
2935
"@types/fs-extra": "^11.0.4",
3036
"@types/gravatar": "^1.8.6",
31-
"@types/md5": "^2.3.5",
32-
"bumpp": "^10.1.0",
33-
"valaxy": "^0.23.0"
37+
"@types/md5": "^2.3.6",
38+
"bumpp": "^11.0.1",
39+
"eslint": "^10.1.0",
40+
"typescript": "^5.9.3",
41+
"valaxy": "^0.28.1",
42+
"vitest": "^4.1.2",
43+
"vue-tsc": "^3.2.6"
3444
}
3545
}

0 commit comments

Comments
 (0)