Skip to content

Commit 0afc8e7

Browse files
committed
✨ Packager plugin improvements
1 parent c3fc829 commit 0afc8e7

File tree

5 files changed

+392
-38
lines changed

5 files changed

+392
-38
lines changed

tools/plugins/packagerPlugin.ts

Lines changed: 115 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Plugin } from 'esbuild'
22
import * as fs from 'fs'
3+
import { readFileSync, writeFileSync } from 'fs'
4+
import { Octokit } from 'octokit'
35
import * as pathjs from 'path'
6+
import * as prettier from 'prettier'
47
import * as c from 'svelte/compiler'
5-
import { readFileSync, writeFileSync } from 'fs'
68
import * as svelteInternal from 'svelte/internal'
7-
import * as prettier from 'prettier'
9+
10+
const octokit = new Octokit({})
811

912
const PACKAGE = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
1013
const PLUGIN_PACKAGE_PATH = './src/pluginPackage/'
@@ -16,16 +19,30 @@ const PLUGIN_REPO_PATH = 'D:/github-repos/snavesutit/blockbench-plugins/plugins/
1619
const PLUGIN_MANIFEST_PATH = 'D:/github-repos/snavesutit/blockbench-plugins/plugins.json'
1720
const CHANGELOG_PATH = './src/pluginPackage/changelog.json'
1821
const RELEASE_NOTES_TEMPLATES = './tools/plugins/releaseNoteTemplates/'
22+
const URL_REGEX =
23+
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gm
1924

2025
function replaceTemplateVars(str: string, items: Record<string, string>) {
21-
return str.replace(/\{(.+?)\}/g, str => items[str.replace(/[\{\}]/g, '')] || str)
26+
return str.replace(/\{(.+?)\}/g, str => items[str.replace(/[\{\}]/g, '')] ?? str)
27+
}
28+
29+
const VERSION_REGEX = /(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9]+))?/
30+
31+
function getVersionNumbers(version: string) {
32+
const match = VERSION_REGEX.exec(version)
33+
if (!match) return null
34+
const major = parseInt(match[1])
35+
const minor = parseInt(match[2])
36+
const patch = parseInt(match[3])
37+
const preRelease = match[4] ?? null
38+
return { major, minor, patch, preRelease }
2239
}
2340

2441
function plugin(): Plugin {
2542
return {
2643
name: 'packagerPlugin',
2744
setup(build) {
28-
build.onEnd(() => {
45+
build.onEnd(async () => {
2946
console.log('📦 Packaging...')
3047
fs.rmSync(DIST_PACKAGE_PATH, { recursive: true, force: true })
3148
fs.cpSync(PLUGIN_PACKAGE_PATH, DIST_PACKAGE_PATH, { recursive: true })
@@ -53,25 +70,100 @@ function plugin(): Plugin {
5370
fs.unlinkSync(pathjs.join(DIST_PACKAGE_PATH, 'about.svelte'))
5471

5572
if (process.env.NODE_ENV === 'production') {
56-
console.log('📝 Creating changelogs...')
57-
const changelog = JSON.parse(fs.readFileSync(CHANGELOG_PATH, 'utf-8'))
58-
for (const file of fs.readdirSync(RELEASE_NOTES_TEMPLATES)) {
59-
let content = fs.readFileSync(
60-
pathjs.join(RELEASE_NOTES_TEMPLATES, file),
61-
'utf-8'
62-
)
63-
content = replaceTemplateVars(content, {
64-
version: PACKAGE.version,
65-
changes: changelog[PACKAGE.version].categories
66-
.find(c => c.title === 'Changes')
67-
?.list.map(v => '- ' + v)
68-
.join('\n'),
69-
fixes: changelog[PACKAGE.version].categories
70-
.find(c => c.title === 'Fixes')
71-
?.list.map(v => '- ' + v)
72-
.join('\n'),
73-
})
74-
fs.writeFileSync(pathjs.join(DIST_PATH, file), content)
73+
try {
74+
console.log('📝 Creating changelogs...')
75+
const rawChangelog = fs.readFileSync(CHANGELOG_PATH, 'utf-8')
76+
const changelog = JSON.parse(rawChangelog)
77+
for (const file of fs.readdirSync(RELEASE_NOTES_TEMPLATES)) {
78+
let content = fs.readFileSync(
79+
pathjs.join(RELEASE_NOTES_TEMPLATES, file),
80+
'utf-8'
81+
)
82+
let pings = ''
83+
const version = getVersionNumbers(PACKAGE.version)
84+
const latestRelease = getVersionNumbers(
85+
(
86+
await octokit.request('GET /repos/{owner}/{repo}/releases', {
87+
owner: 'animated-java',
88+
repo: 'animated-java',
89+
per_page: 1,
90+
headers: {
91+
accept: 'application/vnd.github+json',
92+
'X-GitHub-Api-Version': '2022-11-28',
93+
},
94+
})
95+
).data[0].tag_name
96+
)
97+
if (!latestRelease) {
98+
throw new Error('No latest release found on github!')
99+
}
100+
if (version.major > latestRelease.major) {
101+
pings += `@Major Release Ping`
102+
}
103+
if (version.minor > latestRelease.minor) {
104+
pings += ` @Minor Release Ping`
105+
}
106+
if (version.patch > latestRelease.patch) {
107+
pings += ` @Patch Release Ping`
108+
}
109+
if (latestRelease.preRelease) {
110+
pings += ` @Pre-Release Ping`
111+
}
112+
if (rawChangelog.includes('[BREAKING]')) {
113+
pings += ` @Breaking Changes Ping`
114+
}
115+
116+
const versionChangelog = changelog[PACKAGE.version]
117+
if (!versionChangelog) {
118+
throw new Error(
119+
`No changelog found for version ${PACKAGE.version} in ${CHANGELOG_PATH}`
120+
)
121+
}
122+
123+
const changeList = versionChangelog.categories.find(
124+
c => c.title === 'Changes'
125+
)
126+
let changes = ''
127+
if (changeList) {
128+
changes =
129+
'### Changes\n\n' +
130+
changeList.list
131+
.map(v => '- ' + v)
132+
.join('\n')
133+
.replace('[BREAKING]', '⚠️ **BREAKING CHANGE** — ')
134+
}
135+
const fixList = versionChangelog.categories.find(
136+
c => c.title === 'Fixes'
137+
)
138+
let fixes = ''
139+
if (fixList) {
140+
fixes =
141+
'### Fixes\n\n' +
142+
fixList.list
143+
.map(v => '- ' + v)
144+
.join('\n')
145+
.replace('[BREAKING]', '⚠️ **BREAKING CHANGE** — ')
146+
}
147+
148+
content = replaceTemplateVars(content, {
149+
version: PACKAGE.version,
150+
changes,
151+
fixes,
152+
pings: pings.trim(),
153+
})
154+
155+
if (content.includes('[[ESCAPE_URLS]]')) {
156+
content = content
157+
.replace('[[ESCAPE_URLS]]', '')
158+
// @ts-expect-error
159+
.replaceAll(URL_REGEX, (match: string) => '<' + match + '>')
160+
}
161+
162+
fs.writeFileSync(pathjs.join(DIST_PATH, file), content)
163+
}
164+
} catch (e) {
165+
console.error('Error creating changelogs:', e)
166+
throw e
75167
}
76168

77169
if (fs.existsSync(PLUGIN_REPO_PATH)) {
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
# :AnimatedJava: Animated Java Release {version}
1+
[[ESCAPE_URLS]]
22

3-
### Changes
3+
# :AnimatedJava: Animated Java Release v{version}
44

55
{changes}
66

7-
### Fixes
8-
97
{fixes}
108

119
## How to Install
1210

13-
For now, you can download and install the latest build of AJ [here](<https://builds.animated-java.dev/latest>) or install it via this link:
14-
<https://builds.animated-java.dev/latest/download/animated_java.js>
15-
Once [this PR](<https://github.com/JannisX11/blockbench-plugins/pull/{pr_number}>) has been merged, you can install the latest release of AJ from the Blockbench plugin list.
16-
[Follow this tutorial](<https://animated-java.github.io/docs/getting-started/installing-animated-java>) if you don't know how to install plugins in Blockbench.
11+
For now, you can download and install the latest build of AJ [here](https://builds.animated-java.dev/latest) or install it via this link:
12+
https://builds.animated-java.dev/latest/download/animated_java.js
13+
Once [this PR](https://github.com/JannisX11/blockbench-plugins/pull/{pr_number}) has been merged, you can install the latest release of AJ from the Blockbench plugin list.
14+
[Follow this tutorial](https://animated-java.github.io/docs/getting-started/installing-animated-java) if you don't know how to install plugins in Blockbench.
1715

18-
{pings}
16+
-# {pings}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
# Animated Java Release {version}
2-
3-
### Changes
1+
# Animated Java Release v{version}
42

53
{changes}
64

7-
### Fixes
8-
95
{fixes}
106

117
## How to Install
128

139
For now, you can download and install the latest build of AJ [here](https://builds.animated-java.dev/latest) or install it via this link:
14-
<https://builds.animated-java.dev/latest/download/animated_java.js>
10+
https://builds.animated-java.dev/latest/download/animated_java.js
1511
Once [this PR](https://github.com/JannisX11/blockbench-plugins/pull/{pr_number}) has been merged, you can install the latest release of AJ from the Blockbench plugin list.
1612
[Follow this tutorial](https://animated-java.github.io/docs/getting-started/installing-animated-java) if you don't know how to install plugins in Blockbench.

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"noImplicitAny": true,
66
"module": "ES2022",
77
"target": "ES2022",
8+
"lib": ["ES2024"],
89
"moduleResolution": "node",
910
"strict": true,
1011
"types": ["node", "./types/blockbench-types", "svelte"],

0 commit comments

Comments
 (0)