2024-06-13 13:09:27 +00:00
|
|
|
import { assert } from '@tldraw/utils'
|
2024-01-15 12:33:15 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import { Octokit } from 'octokit'
|
|
|
|
import path from 'path'
|
|
|
|
|
2024-03-18 15:59:29 +00:00
|
|
|
const octokit = new Octokit({})
|
2024-01-15 12:33:15 +00:00
|
|
|
|
|
|
|
const { log: nicelog } = console
|
|
|
|
|
2024-04-23 10:52:59 +00:00
|
|
|
const CHANGELOG_START_TAG = '{/* START AUTO-GENERATED CHANGELOG */}'
|
|
|
|
const CHANGELOG_END_TAG = '{/* END AUTO-GENERATED CHANGELOG */}'
|
|
|
|
|
2024-01-15 12:33:15 +00:00
|
|
|
export async function fetchReleases() {
|
|
|
|
try {
|
|
|
|
const RELEASES_DIRECTORY = path.join(process.cwd(), 'content', 'releases')
|
|
|
|
|
|
|
|
if (fs.existsSync(RELEASES_DIRECTORY)) {
|
|
|
|
fs.rmSync(RELEASES_DIRECTORY, { recursive: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.mkdirSync(RELEASES_DIRECTORY)
|
|
|
|
|
|
|
|
const res = await octokit.rest.repos.listReleases({
|
|
|
|
owner: 'tldraw',
|
|
|
|
repo: 'tldraw',
|
2024-04-23 10:52:59 +00:00
|
|
|
per_page: 100,
|
2024-01-15 12:33:15 +00:00
|
|
|
})
|
|
|
|
|
2024-04-23 10:52:59 +00:00
|
|
|
const changelogIndex: { tagName: string; body: string }[] = []
|
|
|
|
|
2024-01-15 12:33:15 +00:00
|
|
|
if (res.status === 200) {
|
|
|
|
nicelog(`• Writing releases...`)
|
|
|
|
res.data
|
2024-04-23 10:52:59 +00:00
|
|
|
.filter(
|
|
|
|
(release) =>
|
|
|
|
// no drafts
|
|
|
|
!release.draft &&
|
|
|
|
// no v1 releases
|
|
|
|
!release.tag_name.startsWith('v1') &&
|
|
|
|
// no pre-releases
|
|
|
|
!release.tag_name.includes('-')
|
|
|
|
)
|
2024-01-15 12:33:15 +00:00
|
|
|
.forEach((release, i) => {
|
|
|
|
const date = (
|
|
|
|
release.published_at ? new Date(release.published_at) : new Date()
|
|
|
|
).toLocaleDateString('en-US', {
|
|
|
|
year: 'numeric',
|
|
|
|
month: 'numeric',
|
|
|
|
day: 'numeric',
|
|
|
|
})
|
|
|
|
|
|
|
|
let m = ''
|
|
|
|
|
|
|
|
m += `---\n`
|
|
|
|
m += `title: ${release.tag_name}\n`
|
|
|
|
m += `description: Examples\n`
|
|
|
|
m += `author: tldraw\n`
|
|
|
|
m += `date: ${date}\n`
|
|
|
|
m += `order: ${i}\n`
|
|
|
|
m += `status: published\n`
|
|
|
|
m += `---\n\n`
|
|
|
|
m += `[View on GitHub](${release.html_url})\n\n`
|
|
|
|
|
|
|
|
const body = (release.body ?? '')
|
|
|
|
.replaceAll(/### Release Notes\n/g, '')
|
|
|
|
.replaceAll(/\[([^\]]+)\]$/g, '$1')
|
|
|
|
.replace(/<image (.*)">/g, '<image $1" />')
|
|
|
|
.replace(/<([^>]+)\/?>(?=\s|$)/g, '`<$1>`')
|
|
|
|
.replace(/`<image(.*) \/>`/g, '<image$1 />')
|
|
|
|
.replace(/`<img(.*) \/>`/g, '<img$1 />')
|
|
|
|
.replace(/\/\/>/g, '/>')
|
|
|
|
|
|
|
|
m += body
|
|
|
|
|
2024-04-23 10:52:59 +00:00
|
|
|
changelogIndex.push({
|
|
|
|
tagName: release.tag_name,
|
|
|
|
body,
|
|
|
|
})
|
|
|
|
|
2024-01-15 12:33:15 +00:00
|
|
|
const filePath = path.join(RELEASES_DIRECTORY, `${release.tag_name}.mdx`)
|
|
|
|
fs.writeFileSync(filePath, m)
|
|
|
|
})
|
2024-04-23 10:52:59 +00:00
|
|
|
nicelog(`• Writing changelog...`)
|
|
|
|
|
|
|
|
const releasesPagePath = path.join(
|
|
|
|
process.cwd(),
|
|
|
|
'content',
|
|
|
|
'getting-started',
|
|
|
|
'releases-versioning.mdx'
|
|
|
|
)
|
|
|
|
const releasesPageContent = fs.readFileSync(releasesPagePath, 'utf-8')
|
|
|
|
|
|
|
|
const startIndex = releasesPageContent.indexOf(CHANGELOG_START_TAG)
|
|
|
|
assert(startIndex !== -1, 'cannot find start tag')
|
|
|
|
|
|
|
|
const endIndex = releasesPageContent.indexOf(CHANGELOG_END_TAG)
|
|
|
|
assert(endIndex !== -1, 'cannot find end tag')
|
|
|
|
|
|
|
|
const contentBefore = releasesPageContent.slice(0, startIndex)
|
|
|
|
const contentAfter = releasesPageContent.slice(endIndex + CHANGELOG_END_TAG.length)
|
|
|
|
|
|
|
|
const changelogIndexMarkdown = changelogIndex
|
|
|
|
.sort((a, b) => b.tagName.localeCompare(a.tagName))
|
|
|
|
.map(({ body, tagName }, i) => {
|
|
|
|
if (i >= 10) {
|
|
|
|
return `### [${tagName}](/releases/${tagName})`
|
|
|
|
} else {
|
|
|
|
return [`### [${tagName}](/releases/${tagName})`, body].join('\n\n')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.join('\n\n')
|
|
|
|
|
|
|
|
const updatedContent = [
|
|
|
|
contentBefore,
|
|
|
|
CHANGELOG_START_TAG,
|
|
|
|
changelogIndexMarkdown,
|
|
|
|
CHANGELOG_END_TAG,
|
|
|
|
contentAfter,
|
|
|
|
].join('\n\n')
|
|
|
|
|
|
|
|
fs.writeFileSync(releasesPagePath, updatedContent, 'utf-8')
|
2024-01-15 12:33:15 +00:00
|
|
|
} else {
|
2024-02-29 16:06:19 +00:00
|
|
|
throw Error(`x Could not get releases for tldraw.`)
|
2024-01-15 12:33:15 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
nicelog(`x Could not generate release content.`)
|
|
|
|
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|