From 8cea197020dbd2325dd69e2b85c970ca606ec51a Mon Sep 17 00:00:00 2001 From: Soybean Date: Sun, 4 Jun 2023 21:55:51 +0800 Subject: [PATCH] feat(projects): changelog version support the latest verison in package.json --- src/changelog/git.ts | 4 +--- src/changelog/index.ts | 19 +++++++++++++++---- src/changelog/markdown.ts | 8 +++++--- src/types/index.ts | 2 ++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/changelog/git.ts b/src/changelog/git.ts index e367424..7843d61 100644 --- a/src/changelog/git.ts +++ b/src/changelog/git.ts @@ -182,9 +182,7 @@ async function getResolvedAuthorLogin(params: { } try { - const data = await ofetch(`https://api.github.com/search/users?q=${encodeURIComponent(email)}`, { - headers: getHeaders(githubToken) - }); + const data = await ofetch(`https://api.github.com/search/users?q=${encodeURIComponent(email)}`); login = data.items[0].login; } catch {} diff --git a/src/changelog/index.ts b/src/changelog/index.ts index 2703549..7602258 100644 --- a/src/changelog/index.ts +++ b/src/changelog/index.ts @@ -45,32 +45,43 @@ function createDefaultOptions() { breakingChanges: '🚨 Breaking Changes' }, output: 'CHANGELOG.md', - overrideChangelog: false + overrideChangelog: false, + newVersion: '' }; return options; } -async function getGithubTokenFromPkg(cwd: string) { +async function getOptionsFromPkg(cwd: string) { let githubToken = ''; + let newVersion = ''; try { const pkgJson = await readFile(`${cwd}/package.json`, 'utf-8'); const pkg = JSON.parse(pkgJson); githubToken = pkg?.['github-token'] || ''; + newVersion = pkg?.version || ''; } catch {} - return githubToken; + return { + githubToken, + newVersion + }; } export async function initOptions() { const options = createDefaultOptions(); + const { githubToken, newVersion } = await getOptionsFromPkg(options.cwd); + if (!options.githubToken) { - const githubToken = await getGithubTokenFromPkg(options.cwd); options.githubToken = githubToken; } + if (newVersion) { + options.newVersion = `v${newVersion}`; + } + if (!options.from) { options.from = await getLastGitTag(); } diff --git a/src/changelog/markdown.ts b/src/changelog/markdown.ts index fef333a..005bfb5 100644 --- a/src/changelog/markdown.ts +++ b/src/changelog/markdown.ts @@ -5,6 +5,8 @@ import { convert } from 'convert-gitmoji'; import { partition, groupBy, capitalize, join } from '../shared'; import type { Reference, GitCommit, ChangelogOption, AuthorInfo } from '../types'; +const VERSION_REG_OF_MARKDOWN = /## \[v\d+\.\d+\.\d+\]/g; + function formatReferences(references: Reference[], github: string, type: 'issues' | 'hash'): string { const refs = references .filter(i => { @@ -119,7 +121,9 @@ export function generateMarkdown(params: { if (showTitle) { const today = dayjs().format('YYYY-MM-DD'); - const title = `## [${options.to}](${url})(${today})`; + const version = VERSION_REG_OF_MARKDOWN.test(options.to) ? options.to : options.newVersion; + + const title = `## [${version}](${url})(${today})`; lines.push(title); } @@ -161,8 +165,6 @@ export function generateMarkdown(params: { export async function isVersionInMarkdown(version: string, mdPath: string) { let isIn = false; - const VERSION_REG_OF_MARKDOWN = /## \[v\d+\.\d+\.\d+\]/g; - const md = await readFile(mdPath, 'utf8'); if (md) { diff --git a/src/types/index.ts b/src/types/index.ts index eca90f0..d5026f8 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -58,4 +58,6 @@ export interface ChangelogOption { group?: boolean | 'multiple'; output: string; overrideChangelog: boolean; + /** version from package.json, with preffix "v" */ + newVersion: string; }