Skip to content

Commit 791eaa3

Browse files
committed
chore: Update changelog gen script
1 parent 3e41ed5 commit 791eaa3

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"rimraf": "^6.0.1",
4545
"semver": "^7.6.2",
4646
"sponsorkit": "^0.14.7",
47+
"tinyglobby": "^0.2.10",
4748
"typescript": "^5.5.3",
4849
"yorkie": "^2.0.0"
4950
},

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/_utils.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { execSync } from 'node:child_process'
21
import { promises as fsp } from 'node:fs'
32
import { $fetch } from 'ofetch'
43
import { resolve } from 'pathe'
5-
import { globby } from 'globby'
6-
import { execaSync } from 'execa'
4+
import { compare } from 'semver'
5+
import { glob } from 'tinyglobby'
6+
import { exec } from 'tinyexec'
77
import { determineSemverChange, getGitDiff, loadChangelogConfig, parseCommits } from 'changelogen'
88

99
export interface Dep {
@@ -27,7 +27,7 @@ export async function loadPackage (dir: string) {
2727
const dep: Dep = { name: e[0], range: e[1] as string, type }
2828
delete data[type][dep.name]
2929
const updated = reviver(dep) || dep
30-
data[updated.type] = data[updated.type] || {}
30+
data[updated.type] ||= {}
3131
data[updated.type][updated.name] = updated.range
3232
}
3333
}
@@ -43,7 +43,7 @@ export async function loadPackage (dir: string) {
4343

4444
export async function loadWorkspace (dir: string) {
4545
const workspacePkg = await loadPackage(dir)
46-
const pkgDirs = (await globby(['packages/*'], { onlyDirectories: true })).sort()
46+
const pkgDirs = (await glob(['packages/*'], { onlyDirectories: true })).sort()
4747

4848
const packages: Package[] = []
4949

@@ -103,32 +103,49 @@ export async function determineBumpType () {
103103
const config = await loadChangelogConfig(process.cwd())
104104
const commits = await getLatestCommits()
105105

106-
const bumpType = determineSemverChange(commits, config)
106+
return determineSemverChange(commits, config)
107+
}
108+
109+
export async function getLatestTag () {
110+
const { stdout: latestTag } = await exec('git', ['describe', '--tags', '--abbrev=0'])
111+
return latestTag.trim()
112+
}
107113

108-
return bumpType === 'major' ? 'minor' : bumpType
114+
export async function getLatestReleasedTag () {
115+
const latestReleasedTag = await exec('git', ['tag', '-l']).then(r => r.stdout.trim().split('\n').filter(t => /v2\.\d+\.\d+/.test(t)).sort(compare)).then(r => r.pop()!.trim())
116+
return latestReleasedTag
117+
}
118+
119+
export async function getPreviousReleasedCommits () {
120+
const config = await loadChangelogConfig(process.cwd())
121+
const latestTag = await getLatestTag()
122+
const latestReleasedTag = await getLatestReleasedTag()
123+
const commits = parseCommits(await getGitDiff(latestTag, latestReleasedTag), config)
124+
return commits
109125
}
110126

111127
export async function getLatestCommits () {
112128
const config = await loadChangelogConfig(process.cwd())
113-
const latestTag = execaSync('git', ['describe', '--tags', '--abbrev=0']).stdout
129+
const latestTag = await getLatestTag()
114130

115131
return parseCommits(await getGitDiff(latestTag), config)
116132
}
117133

118134
export async function getContributors () {
119135
const contributors = [] as Array<{ name: string, username: string }>
120136
const emails = new Set<string>()
121-
const latestTag = execSync('git describe --tags --abbrev=0').toString().trim()
137+
const latestTag = await getLatestTag()
122138
const rawCommits = await getGitDiff(latestTag)
123139
for (const commit of rawCommits) {
124140
if (emails.has(commit.author.email) || commit.author.name === 'renovate[bot]') { continue }
125-
const { author } = await $fetch<{ author: { login: string, email: string } }>(`https://api.github.com/repos/CodeDredd/pinia-orm/commits/${commit.shortHash}`, {
141+
const { author } = await $fetch<{ author: { login: string, email: string } }>(`https://api.github.com/repos/nuxt/nuxt/commits/${commit.shortHash}`, {
126142
headers: {
127-
'User-Agent': 'CodeDredd/pinia-orm',
143+
'User-Agent': 'nuxt/nuxt',
128144
'Accept': 'application/vnd.github.v3+json',
129145
'Authorization': `token ${process.env.GITHUB_TOKEN}`,
130146
},
131147
})
148+
if (!author) { continue }
132149
if (!contributors.some(c => c.username === author.login)) {
133150
contributors.push({ name: commit.author.name, username: author.login })
134151
}

scripts/update-changelog.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import { $fetch } from 'ofetch'
33
import { inc } from 'semver'
44
import { generateMarkDown, getCurrentGitBranch, loadChangelogConfig } from 'changelogen'
55
import { consola } from 'consola'
6-
import { determineBumpType, getContributors, getLatestCommits, loadWorkspace } from './_utils'
6+
import { determineBumpType, getContributors, getLatestCommits, getLatestReleasedTag, getLatestTag, getPreviousReleasedCommits, loadWorkspace } from './_utils'
77

88
async function main () {
99
const releaseBranch = await getCurrentGitBranch()
1010
const workspace = await loadWorkspace(process.cwd())
1111
const config = await loadChangelogConfig(process.cwd(), {})
1212

13+
const prevMessages = new Set(handleSeparateBranch ? await getPreviousReleasedCommits().then(r => r.map(c => c.message)) : [])
14+
1315
const commits = await getLatestCommits().then(commits => commits.filter(
1416
c => config.types[c.type] && !(c.type === 'chore' && c.scope === 'deps' && !c.isBreaking),
1517
))
16-
const bumpType = await determineBumpType()
18+
const bumpType = await determineBumpType() || 'patch'
1719

18-
const newVersion = inc(workspace.find('pinia-orm').data.version, bumpType || 'patch')
20+
const newVersion = inc(workspace.find('pinia-orm').data.version, bumpType)
1921
const changelog = await generateMarkDown(commits, config)
2022

2123
// Create and push a branch with bumped versions if it has not already been created
@@ -35,19 +37,24 @@ async function main () {
3537
}
3638

3739
// Get the current PR for this release, if it exists
38-
const currentPR = (await $fetch(`https://api.github.com/repos/CodeDredd/pinia-orm/pulls?head=v${newVersion}`)).find(pull => pull.title === `v${newVersion}`)
40+
const [currentPR] = await $fetch(`https://api.github.com/repos/CodeDredd/pinia-orm/pulls?head=v${newVersion}`)
3941
const contributors = await getContributors()
4042

43+
const latestTag = await getLatestTag()
44+
const previousReleasedTag = handleSeparateBranch ? await getLatestReleasedTag() : latestTag
45+
4146
console.log('CurrentPR', currentPR)
4247
console.info('New Version ', newVersion)
4348

4449
const releaseNotes = [
4550
currentPR?.body.replace(/## 👉 Changelog[\s\S]*$/, '') || `> ${newVersion} is the next ${bumpType} release.\n>\n> **Timetable**: to be announced.`,
4651
'## 👉 Changelog',
4752
changelog
48-
.replace(/^## v.*?\n/, '')
53+
.replace(/^## v.*\n/, '')
4954
.replace(`...${releaseBranch}`, `...v${newVersion}`)
50-
.replace(/### Contributors[\s\S]*$/, ''),
55+
.replace(/### Contributors[\s\S]*$/, '')
56+
.replace(/[\n\r]+/g, '\n')
57+
.replace(latestTag, previousReleasedTag),
5158
'### ❤️ Contributors',
5259
contributors.map(c => `- ${c.name} (@${c.username})`).join('\n'),
5360
].join('\n')

0 commit comments

Comments
 (0)