2023-04-20 09:40:07 +00:00
|
|
|
import { execSync } from 'node:child_process'
|
|
|
|
import { $fetch } from 'ofetch'
|
|
|
|
import { inc } from 'semver'
|
2023-09-04 08:44:23 +00:00
|
|
|
import { generateMarkDown, getCurrentGitBranch, loadChangelogConfig } from 'changelogen'
|
2023-09-19 21:26:15 +00:00
|
|
|
import { consola } from 'consola'
|
2024-01-30 14:18:37 +00:00
|
|
|
import { determineBumpType, getContributors, getLatestCommits, loadWorkspace } from './_utils'
|
2023-04-20 09:40:07 +00:00
|
|
|
|
|
|
|
async function main () {
|
2023-09-04 08:44:23 +00:00
|
|
|
const releaseBranch = await getCurrentGitBranch()
|
2023-04-20 09:40:07 +00:00
|
|
|
const workspace = await loadWorkspace(process.cwd())
|
2023-09-04 08:44:23 +00:00
|
|
|
const config = await loadChangelogConfig(process.cwd(), {})
|
2023-04-20 09:40:07 +00:00
|
|
|
|
2023-04-20 10:16:57 +00:00
|
|
|
const commits = await getLatestCommits().then(commits => commits.filter(
|
2024-06-19 13:16:42 +00:00
|
|
|
c => config.types[c.type] && !(c.type === 'chore' && c.scope === 'deps'),
|
2023-04-20 10:16:57 +00:00
|
|
|
))
|
2024-06-13 12:54:49 +00:00
|
|
|
const bumpType = await determineBumpType() || 'patch'
|
2023-04-20 09:40:07 +00:00
|
|
|
|
2024-06-13 12:54:49 +00:00
|
|
|
const newVersion = inc(workspace.find('nuxt').data.version, bumpType)
|
2023-04-20 09:40:07 +00:00
|
|
|
const changelog = await generateMarkDown(commits, config)
|
|
|
|
|
|
|
|
// Create and push a branch with bumped versions if it has not already been created
|
|
|
|
const branchExists = execSync(`git ls-remote --heads origin v${newVersion}`).toString().trim().length > 0
|
|
|
|
if (!branchExists) {
|
2023-04-20 09:42:20 +00:00
|
|
|
execSync('git config --global user.email "daniel@roe.dev"')
|
|
|
|
execSync('git config --global user.name "Daniel Roe"')
|
2023-04-20 09:40:07 +00:00
|
|
|
execSync(`git checkout -b v${newVersion}`)
|
|
|
|
|
|
|
|
for (const pkg of workspace.packages.filter(p => !p.data.private)) {
|
|
|
|
workspace.setVersion(pkg.data.name, newVersion!)
|
|
|
|
}
|
|
|
|
await workspace.save()
|
|
|
|
|
|
|
|
execSync(`git commit -am v${newVersion}`)
|
|
|
|
execSync(`git push -u origin v${newVersion}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current PR for this release, if it exists
|
|
|
|
const [currentPR] = await $fetch(`https://api.github.com/repos/nuxt/nuxt/pulls?head=nuxt:v${newVersion}`)
|
2024-01-30 14:18:37 +00:00
|
|
|
const contributors = await getContributors()
|
2023-04-20 09:40:07 +00:00
|
|
|
|
|
|
|
const releaseNotes = [
|
|
|
|
currentPR?.body.replace(/## 👉 Changelog[\s\S]*$/, '') || `> ${newVersion} is the next ${bumpType} release.\n>\n> **Timetable**: to be announced.`,
|
|
|
|
'## 👉 Changelog',
|
2024-01-30 14:18:37 +00:00
|
|
|
changelog
|
2024-05-14 17:54:37 +00:00
|
|
|
.replace(/^## v.*\n/, '')
|
2024-01-30 14:18:37 +00:00
|
|
|
.replace(`...${releaseBranch}`, `...v${newVersion}`)
|
2024-06-13 12:54:49 +00:00
|
|
|
.replace(/### ❤️ Contributors[\s\S]*$/, '')
|
|
|
|
.replace(/[\n\r]+/g, '\n'),
|
2024-03-09 06:48:15 +00:00
|
|
|
'### ❤️ Contributors',
|
2024-04-05 18:08:32 +00:00
|
|
|
contributors.map(c => `- ${c.name} (@${c.username})`).join('\n'),
|
2023-04-20 09:40:07 +00:00
|
|
|
].join('\n')
|
|
|
|
|
|
|
|
// Create a PR with release notes if none exists
|
|
|
|
if (!currentPR) {
|
|
|
|
return await $fetch('https://api.github.com/repos/nuxt/nuxt/pulls', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2024-04-05 18:08:32 +00:00
|
|
|
Authorization: `token ${process.env.GITHUB_TOKEN}`,
|
2023-04-20 09:40:07 +00:00
|
|
|
},
|
|
|
|
body: {
|
|
|
|
title: `v${newVersion}`,
|
|
|
|
head: `v${newVersion}`,
|
2023-09-04 08:16:51 +00:00
|
|
|
base: releaseBranch,
|
2023-04-20 09:40:07 +00:00
|
|
|
body: releaseNotes,
|
2024-04-05 18:08:32 +00:00
|
|
|
draft: true,
|
|
|
|
},
|
2023-04-20 09:40:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update release notes if the pull request does exist
|
|
|
|
await $fetch(`https://api.github.com/repos/nuxt/nuxt/pulls/${currentPR.number}`, {
|
|
|
|
method: 'PATCH',
|
|
|
|
headers: {
|
2024-04-05 18:08:32 +00:00
|
|
|
Authorization: `token ${process.env.GITHUB_TOKEN}`,
|
2023-04-20 09:40:07 +00:00
|
|
|
},
|
|
|
|
body: {
|
2024-04-05 18:08:32 +00:00
|
|
|
body: releaseNotes,
|
|
|
|
},
|
2023-04-20 09:40:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((err) => {
|
2023-09-19 21:26:15 +00:00
|
|
|
consola.error(err)
|
2023-04-20 09:40:07 +00:00
|
|
|
process.exit(1)
|
|
|
|
})
|