Skip to content

Commit aa78e0b

Browse files
SnaveSutitgitbutler-client
authored andcommitted
🎨 Simplify plugin packager
1 parent 1dfe134 commit aa78e0b

1 file changed

Lines changed: 55 additions & 119 deletions

File tree

.scripts/plugins/packagerPlugin.ts

Lines changed: 55 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import type { Plugin } from 'esbuild'
22
import * as fs from 'fs'
33
import { readFileSync, writeFileSync } from 'fs'
44
import { basename, join } from 'node:path'
5-
import { Octokit } from 'octokit'
6-
import * as prettier from 'prettier'
75
import { sveltePreprocess } from 'svelte-preprocess'
86
// @ts-expect-error - Types are broken in nodenext for this package, but it works fine.
97
import { typescript } from 'svelte-preprocess-esbuild'
@@ -13,16 +11,12 @@ import { render } from 'svelte/server'
1311
// @ts-expect-error - Svelte's internal server-side rendering API is not typed, but we need it to render the about.svelte file at build time.
1412
import * as svelteInternalServer from 'svelte/internal/server'
1513

16-
const OCTO_KIT = new Octokit({})
17-
1814
const PACKAGE = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
1915
const PLUGIN_PACKAGE_PATH = './src/pluginPackage/'
2016
const SVELTE_FILE = './src/pluginPackage/about.svelte'
2117
const README_DIST_PATH = './dist/pluginPackage/about.md'
2218
const DIST_PATH = './dist/'
2319
const DIST_PACKAGE_PATH = './dist/pluginPackage/'
24-
const PLUGIN_REPO_PATH = 'D:/github-repos/snavesutit/blockbench-plugins/plugins/animated_java'
25-
const PLUGIN_MANIFEST_PATH = 'D:/github-repos/snavesutit/blockbench-plugins/plugins.json'
2620
const CHANGELOG_PATH = './src/pluginPackage/changelog.json'
2721
const RELEASE_NOTES_TEMPLATES = './.scripts/plugins/releaseNoteTemplates/'
2822
const URL_REGEX =
@@ -32,18 +26,6 @@ function replaceTemplateVars(str: string, items: Record<string, string>) {
3226
return str.replace(/\{(.+?)\}/g, str => items[str.replace(/[\{\}]/g, '')] ?? str)
3327
}
3428

35-
const VERSION_REGEX = /(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9]+))?/
36-
37-
function getVersionNumbers(version: string) {
38-
const match = VERSION_REGEX.exec(version)
39-
if (!match) return null
40-
const major = parseInt(match[1])
41-
const minor = parseInt(match[2])
42-
const patch = parseInt(match[3])
43-
const preRelease = match[4] ?? null
44-
return { major, minor, patch, preRelease }
45-
}
46-
4729
/**
4830
* Convert a warning or error emitted from the svelte compiler for esbuild.
4931
*/
@@ -160,114 +142,68 @@ function plugin(): Plugin {
160142
fs.unlinkSync(join(DIST_PACKAGE_PATH, 'about.svelte'))
161143

162144
if (process.env.NODE_ENV === 'production') {
163-
try {
164-
console.log('📝 Creating changelogs...')
165-
const rawChangelog = fs.readFileSync(CHANGELOG_PATH, 'utf-8')
166-
const changelog = JSON.parse(rawChangelog)
167-
for (const file of fs.readdirSync(RELEASE_NOTES_TEMPLATES)) {
168-
let content = fs.readFileSync(
169-
join(RELEASE_NOTES_TEMPLATES, file),
170-
'utf-8'
171-
)
172-
let pings = ''
173-
const version = getVersionNumbers(PACKAGE.version)
174-
if (!version) {
175-
throw new Error(
176-
`Version ${PACKAGE.version} in package.json is not valid semver!`
177-
)
178-
}
179-
const latestRelease = getVersionNumbers(
180-
(
181-
await OCTO_KIT.request('GET /repos/{owner}/{repo}/releases', {
182-
owner: 'animated-java',
183-
repo: 'animated-java',
184-
per_page: 1,
185-
headers: {
186-
accept: 'application/vnd.github+json',
187-
'X-GitHub-Api-Version': '2022-11-28',
188-
},
189-
})
190-
).data[0].tag_name
145+
console.log('📝 Creating changelogs...')
146+
const rawChangelog = fs.readFileSync(CHANGELOG_PATH, 'utf-8')
147+
const changelog = JSON.parse(rawChangelog)
148+
for (const file of fs.readdirSync(RELEASE_NOTES_TEMPLATES)) {
149+
let content = fs.readFileSync(join(RELEASE_NOTES_TEMPLATES, file), 'utf-8')
150+
151+
const versionChangelog = changelog[PACKAGE.version]
152+
if (!versionChangelog) {
153+
console.warn(
154+
`⚠️ No changelog found for version ${PACKAGE.version} in ${CHANGELOG_PATH}`
191155
)
192-
if (!latestRelease) {
193-
throw new Error('No latest release found on github!')
194-
}
195-
if (version.major > latestRelease.major) {
196-
pings += `@Major Release Ping`
197-
}
198-
if (version.minor > latestRelease.minor) {
199-
pings += ` @Minor Release Ping`
200-
}
201-
if (version.patch > latestRelease.patch) {
202-
pings += ` @Patch Release Ping`
203-
}
204-
if (latestRelease.preRelease) {
205-
pings += ` @Pre-Release Ping`
206-
}
207-
if (rawChangelog.includes('[BREAKING]')) {
208-
pings += ` @Breaking Changes Ping`
209-
}
210-
211-
const versionChangelog = changelog[PACKAGE.version]
212-
if (!versionChangelog) {
213-
throw new Error(
214-
`No changelog found for version ${PACKAGE.version} in ${CHANGELOG_PATH}`
215-
)
216-
}
217-
218-
let categories = ''
219-
for (const category of versionChangelog.categories) {
220-
categories +=
221-
`\n\n### ${category.title}\n\n` +
222-
category.list
223-
.map((v: string) => '- ' + v)
224-
.join('\n')
225-
.replaceAll('[BREAKING]', '⚠️ **BREAKING** —')
226-
}
156+
return
157+
}
227158

228-
content = replaceTemplateVars(content, {
229-
version: PACKAGE.version,
230-
categories: categories.trim(),
231-
pings: pings.trim(),
232-
})
159+
let categories = ''
160+
for (const category of versionChangelog.categories) {
161+
categories +=
162+
`\n\n### ${category.title}\n\n` +
163+
category.list
164+
.map((v: string) => '- ' + v)
165+
.join('\n')
166+
.replaceAll('[BREAKING]', '⚠️ **BREAKING** —')
167+
}
233168

234-
if (content.includes('[[ESCAPE_URLS]]')) {
235-
content = content
236-
.replace('[[ESCAPE_URLS]]', '')
237-
.replaceAll(URL_REGEX, (match: string) => '<' + match + '>')
238-
}
169+
content = replaceTemplateVars(content, {
170+
version: PACKAGE.version,
171+
categories: categories.trim(),
172+
})
239173

240-
fs.writeFileSync(join(DIST_PATH, file), content)
174+
if (content.includes('[[ESCAPE_URLS]]')) {
175+
content = content
176+
.replace('[[ESCAPE_URLS]]', '')
177+
.replaceAll(URL_REGEX, (match: string) => '<' + match + '>')
241178
}
242-
} catch (e) {
243-
console.error('Error creating changelogs:', e)
244-
throw e
245-
}
246-
247-
if (fs.existsSync(PLUGIN_REPO_PATH)) {
248-
fs.rmSync(PLUGIN_REPO_PATH, { recursive: true, force: true })
249-
fs.cpSync(DIST_PACKAGE_PATH, PLUGIN_REPO_PATH, { recursive: true })
250-
const manifest = JSON.parse(fs.readFileSync(PLUGIN_MANIFEST_PATH, 'utf-8'))
251-
manifest.animated_java.title = PACKAGE.title
252-
manifest.animated_java.author = PACKAGE.author.name
253-
manifest.animated_java.icon = PACKAGE.icon
254-
manifest.animated_java.description = PACKAGE.description
255-
manifest.animated_java.version = PACKAGE.version
256-
manifest.animated_java.min_version = PACKAGE.min_blockbench_version
257-
manifest.animated_java.max_version = PACKAGE.max_blockbench_version
258-
manifest.animated_java.variant = PACKAGE.variant
259-
manifest.animated_java.tags = PACKAGE.tags
260-
manifest.animated_java.has_changelog = true
261179

262-
fs.writeFileSync(
263-
PLUGIN_MANIFEST_PATH,
264-
await prettier.format(JSON.stringify(manifest, null, '\t'), {
265-
useTabs: true,
266-
parser: 'json',
267-
})
268-
)
269-
console.log('📋 Copied to Plugin Repo!')
180+
fs.writeFileSync(join(DIST_PATH, file), content)
270181
}
182+
183+
// if (fs.existsSync(PLUGIN_REPO_PATH)) {
184+
// fs.rmSync(PLUGIN_REPO_PATH, { recursive: true, force: true })
185+
// fs.cpSync(DIST_PACKAGE_PATH, PLUGIN_REPO_PATH, { recursive: true })
186+
// const manifest = JSON.parse(fs.readFileSync(PLUGIN_MANIFEST_PATH, 'utf-8'))
187+
// manifest.animated_java.title = PACKAGE.title
188+
// manifest.animated_java.author = PACKAGE.author.name
189+
// manifest.animated_java.icon = PACKAGE.icon
190+
// manifest.animated_java.description = PACKAGE.description
191+
// manifest.animated_java.version = PACKAGE.version
192+
// manifest.animated_java.min_version = PACKAGE.min_blockbench_version
193+
// manifest.animated_java.max_version = PACKAGE.max_blockbench_version
194+
// manifest.animated_java.variant = PACKAGE.variant
195+
// manifest.animated_java.tags = PACKAGE.tags
196+
// manifest.animated_java.has_changelog = true
197+
198+
// fs.writeFileSync(
199+
// PLUGIN_MANIFEST_PATH,
200+
// await prettier.format(JSON.stringify(manifest, null, '\t'), {
201+
// useTabs: true,
202+
// parser: 'json',
203+
// })
204+
// )
205+
// console.log('📋 Copied to Plugin Repo!')
206+
// }
271207
}
272208
})
273209
},

0 commit comments

Comments
 (0)