tldraw/apps/docs/scripts/functions/fetchReleases.ts
David Sheldrick 9f90fa230b
fix docs build (#3201)
- always refresh docs content when building on CI
- use local api.json files now since we don't want to use SOURCE_SHA
- @steveruizok it feels kinda problematic that we check in a bunch of
derived files that the docs build requires. Things can get out of sync
easily, and whose responsibility is it to update them? In the future I
reckon we should explore ways to remove these files from the git index
as much as possible.

closes #3200 

### Change Type

<!--  Please select a 'Scope' label ️ -->

- [ ] `sdk` — Changes the tldraw SDK
- [ ] `dotcom` — Changes the tldraw.com web app
- [x] `docs` — Changes to the documentation, examples, or templates.
- [ ] `vs code` — Changes to the vscode plugin
- [ ] `internal` — Does not affect user-facing stuff

<!--  Please select a 'Type' label ️ -->

- [x] `bugfix` — Bug fix
- [ ] `feature` — New feature
- [ ] `improvement` — Improving existing features
- [ ] `chore` — Updating dependencies, other boring stuff
- [ ] `galaxy brain` — Architectural changes
- [ ] `tests` — Changes to any test code
- [ ] `tools` — Changes to infrastructure, CI, internal scripts,
debugging tools, etc.
- [ ] `dunno` — I don't know


### Test Plan

1. Add a step-by-step description of how to test your PR here.
2.

- [ ] Unit Tests
- [ ] End to end tests

### Release Notes

- Add a brief release note for your PR here.
2024-03-18 15:59:29 +00:00

71 lines
1.8 KiB
TypeScript

import fs from 'fs'
import { Octokit } from 'octokit'
import path from 'path'
const octokit = new Octokit({})
const { log: nicelog } = console
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',
})
if (res.status === 200) {
nicelog(`• Writing releases...`)
res.data
.filter((release) => !release.draft && release.tag_name.startsWith('v2.0.0'))
.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
const filePath = path.join(RELEASES_DIRECTORY, `${release.tag_name}.mdx`)
fs.writeFileSync(filePath, m)
})
} else {
throw Error(`x Could not get releases for tldraw.`)
}
} catch (error) {
nicelog(`x Could not generate release content.`)
throw error
}
}