Skip to content

Commit

Permalink
fix(projects): fix version date
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jun 4, 2023
1 parent a3cf896 commit b50d804
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 116 deletions.
202 changes: 94 additions & 108 deletions CHANGELOG.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"@types/node": "20.2.5",
"eslint": "8.41.0",
"eslint-config-soybeanjs": "0.4.7",
"githublogen": "link:../githublogen",
"simple-git-hooks": "2.8.1",
"tsx": "3.12.7",
"typescript": "5.0.4",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/changelog/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ofetch } from 'ofetch';
import dayjs from 'dayjs';
import { execCommand, notNullish } from '../shared';
import type { RawGitCommit, GitCommit, GitCommitAuthor, Reference, AuthorInfo } from '../types';

Expand All @@ -10,6 +11,35 @@ export async function getTotalGitTags() {
return tags;
}

export async function getTagDateMap() {
const tagDateStr = await execCommand('git', [
'--no-pager',
'log',
'--tags',
'--simplify-by-decoration',
'--pretty=format:%ci %d'
]);

const TAG_MARK = '(tag: ';

const map = new Map<string, string>();

const dates = tagDateStr.split('\n').filter(item => item.includes(TAG_MARK));
dates.forEach(item => {
const [dateStr, tagStr] = item.split(TAG_MARK);

const date = dayjs(dateStr.trim()).format('YYYY-MM-DD');

const VERSION_REG = /v\d+\.\d+\.\d+/;
const tag = tagStr.match(VERSION_REG)?.[0];
if (tag && date) {
map.set(tag.trim(), date);
}
});

return map;
}

export function getFromToTags(tags: string[]) {
const result: { from: string; to: string }[] = [];

Expand Down
4 changes: 4 additions & 0 deletions src/changelog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cliProgress from 'cli-progress';
import { readFile } from 'fs/promises';
import {
getTotalGitTags,
getTagDateMap,
getLastGitTag,
getFromToTags,
getCurrentGitBranch,
Expand Down Expand Up @@ -39,6 +40,7 @@ function createDefaultOptions() {
from: '',
to: '',
tags: [],
tagDateMap: new Map(),
prerelease: false,
capitalize: true,
emoji: true,
Expand Down Expand Up @@ -98,6 +100,8 @@ export async function initOptions() {

options.tags = await getTotalGitTags();

options.tagDateMap = await getTagDateMap();

options.github = await getGitHubRepo();

options.prerelease = isPrerelease(options.to);
Expand Down
11 changes: 7 additions & 4 deletions src/changelog/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { existsSync } from 'fs';
import { readFile, writeFile } from 'fs/promises';
import dayjs from 'dayjs';
import { convert } from 'convert-gitmoji';
import { partition, groupBy, capitalize, join } from '../shared';
import type { Reference, GitCommit, ChangelogOption, AuthorInfo } from '../types';
Expand Down Expand Up @@ -133,11 +132,15 @@ export function generateMarkdown(params: {
const url = `https://github.com/${options.github}/compare/${options.from}...${options.to}`;

if (showTitle) {
const today = dayjs().format('YYYY-MM-DD');

const version = VERSION_REG.test(options.to) ? options.to : options.newVersion;

const title = `## [${version}](${url})(${today})`;
const date = options.tagDateMap.get(options.to);

let title = `## [${version}](${url})`;

if (date) {
title += ` (${date})`;
}

lines.push(title);
}
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface ChangelogOption {
from: string;
to: string;
tags: string[];
tagDateMap: Map<string, string>;
prerelease: boolean;
capitalize: boolean;
/**
Expand Down

0 comments on commit b50d804

Please sign in to comment.