Skip to content

Commit

Permalink
feat(projects): changelog version support the latest verison in packa…
Browse files Browse the repository at this point in the history
…ge.json
  • Loading branch information
honghuangdc committed Jun 4, 2023
1 parent 646a7b3 commit 8cea197
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/changelog/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
19 changes: 15 additions & 4 deletions src/changelog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 5 additions & 3 deletions src/changelog/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ export interface ChangelogOption {
group?: boolean | 'multiple';
output: string;
overrideChangelog: boolean;
/** version from package.json, with preffix "v" */
newVersion: string;
}

0 comments on commit 8cea197

Please sign in to comment.