Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(git-changelog): include extensions option #175

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/vitepress-plugin-git-changelog/src/vite/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { posix, sep, win32 } from 'node:path'
import { exec } from 'node:child_process'
import { promisify } from 'node:util'

Expand All @@ -14,6 +13,7 @@ import {
type CommitToStringsHandler,
type RewritePathsBy,
digestStringAsSHA256,
generateCommitPathsRegExp,
normalizeGitLogPath,
parseGitLogRefsAsTags,
returnOrResolvePromise,
Expand Down Expand Up @@ -49,6 +49,7 @@ async function aggregateCommit(
getReleaseTagsURL: CommitToStringsHandler,
log: Commit,
includeDirs: string[] = [],
includeExtensions: `.${string}`[] = [],
optsRewritePaths?: Record<string, string>,
kwaa marked this conversation as resolved.
Show resolved Hide resolved
optsRewritePathsByPatterns?: RewritePathsBy,
) {
Expand Down Expand Up @@ -77,11 +78,7 @@ async function aggregateCommit(
new Set(
files
.filter((i) => {
// include is not set, it is /^.+\md$/
// include is set, it is /^(${include.join('|')})\/.+\md$/
// in another word, /^(includeItem1|includeItem2|includeItem3)\/.+\md$/
const regexp = new RegExp(`^${includeDirs.length > 0 ? `(${includeDirs.join('|')})${sep === win32.sep ? win32.sep : `\\${posix.sep}`}` : ''}.+\\.md$`)
return !!i[1]?.match(regexp)?.[0]
return !!i[1]?.match(generateCommitPathsRegExp(includeDirs, includeExtensions))?.[0]
}),
),
)
Expand Down Expand Up @@ -110,6 +107,7 @@ async function aggregateCommits(
getReleaseTagsURL: CommitToStringsHandler,
commits: SimpleGitCommit,
includeDirs: string[] = [],
includeExtensions: `.${string}`[] = [],
rewritePaths?: Record<string, string>,
rewritePathsBy?: RewritePathsBy,
) {
Expand Down Expand Up @@ -144,7 +142,7 @@ async function aggregateCommits(
const processedCommits = await Promise.all(
transformedCommits.map(
async (commit) => {
return aggregateCommit(getReleaseTagURL, getReleaseTagsURL, commit, includeDirs, rewritePaths, rewritePathsBy)
return aggregateCommit(getReleaseTagURL, getReleaseTagsURL, commit, includeDirs, includeExtensions, rewritePaths, rewritePathsBy)
},
),
)
Expand All @@ -157,6 +155,10 @@ export interface GitChangelogOptions {
* When fetching git logs, what directories should be included?
*/
includeDirs?: string[]
/**
* When fetching git logs, what extensions should be included?
*/
includeExtensions?: `.${string}`[]
/**
* Your repository URL.
* Yes, you can dynamically generate it.
Expand Down Expand Up @@ -248,6 +250,7 @@ export function GitChangelog(options: GitChangelogOptions = {}): Plugin {
maxGitLogCount,
maxConcurrentProcesses,
includeDirs = [],
includeExtensions = [],
repoURL = 'https://github.com/example/example',
getReleaseTagURL = defaultReleaseTagURLHandler,
getReleaseTagsURL = defaultReleaseTagsURLHandler,
Expand Down Expand Up @@ -313,6 +316,7 @@ export function GitChangelog(options: GitChangelogOptions = {}): Plugin {
getReleaseTagsURL,
gitLogsRaw.all,
includeDirs,
includeExtensions,
options.rewritePaths,
options.rewritePathsBy,
)
Expand Down
17 changes: 16 additions & 1 deletion packages/vitepress-plugin-git-changelog/src/vite/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest'

import type { Commit } from '../types'
import { parseGitLogRefsAsTags, rewritePathsByPatterns, rewritePathsByRewritingExtension } from './helpers'
import { generateCommitPathsRegExp, parseGitLogRefsAsTags, rewritePathsByPatterns, rewritePathsByRewritingExtension } from './helpers'

describe('rewritePathsByRewritingExtension', () => {
it('should rewrite paths', () => {
Expand Down Expand Up @@ -74,3 +74,18 @@ describe('parseGitLogRefsAsTags', () => {
expect(parseGitLogRefsAsTags('tag: v1.0.0, tag: v1.0.1')).toEqual(['v1.0.0', 'v1.0.1'])
})
})

describe('generateCommitPathsRegExp', () => {
it('default', () => {
expect(generateCommitPathsRegExp([], [])).toEqual(/^.+.md$/)
})
it('includeDirs', () => {
expect(generateCommitPathsRegExp(['docs', 'packages'], [])).toEqual(/^(docs|packages)\/.+.md$/)
})
it('includeExtensions', () => {
expect(generateCommitPathsRegExp([], ['.md', '.ts'])).toEqual(/^.+(.md|.ts)$/)
})
it('includeDirs and includeExtensions', () => {
expect(generateCommitPathsRegExp(['docs', 'packages'], ['.md', '.ts'])).toEqual(/^(docs|packages)\/.+(.md|.ts)$/)
})
})
15 changes: 14 additions & 1 deletion packages/vitepress-plugin-git-changelog/src/vite/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extname, relative } from 'node:path'
import { extname, posix, relative, sep, win32 } from 'node:path'
import { subtle } from 'uncrypto'
import { normalizePath } from 'vite'
import type { Commit } from '../types'
Expand Down Expand Up @@ -207,3 +207,16 @@ export function parseGitLogRefsAsTags(refs?: string): string[] {

return tags.map(tag => tag.replace('tag: ', '').trim())
}

/**
* Generate RegExp for filtering out paths of commits.
*
* It follows the rules that:
* - includes is not set, it is /^.+.md$/
* - includeDirs is set, it is /^(${includeDirs.join('|')})\/.+.md$/
* - includeExtensions is set, it is /^.+(${includeExtensions.join('|')})$/
* - in another word, /^(includeDir1|includeDir2)\/.+(includeExtension1|includeExtensions2)$/
*/
export function generateCommitPathsRegExp(includeDirs: string[], includeExtensions: `.${string}`[]): RegExp {
return new RegExp(`^${includeDirs.length > 0 ? `(${includeDirs.join('|')})${sep === win32.sep ? win32.sep : `\\${posix.sep}`}` : ''}.+${includeExtensions.length > 0 ? `(${includeExtensions.join('|')})` : '.md'}$`)
nekomeowww marked this conversation as resolved.
Show resolved Hide resolved
}
nekomeowww marked this conversation as resolved.
Show resolved Hide resolved