Skip to content

Commit 197a43c

Browse files
committed
🚧 Packager plugin improvements
1 parent 76f4b3c commit 197a43c

File tree

3 files changed

+93
-28
lines changed

3 files changed

+93
-28
lines changed

tools/plugins/packagerPlugin.ts

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@ import { Plugin } from 'esbuild'
22
import * as fs from 'fs'
33
import * as pathjs from 'path'
44
import * as c from 'svelte/compiler'
5-
import { mkdirSync, readFileSync, writeFileSync } from 'fs'
5+
import { readFileSync, writeFileSync } from 'fs'
66
import * as svelteInternal from 'svelte/internal'
77
import * as prettier from 'prettier'
88

99
const PACKAGE = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
1010
const PLUGIN_PACKAGE_PATH = './src/pluginPackage/'
1111
const SVELTE_FILE = './src/pluginPackage/about.svelte'
1212
const README_DIST_PATH = './dist/pluginPackage/about.md'
13-
const DIST_PATH = './dist/pluginPackage/'
13+
const DIST_PATH = './dist/'
14+
const DIST_PACKAGE_PATH = './dist/pluginPackage/'
1415
const PLUGIN_REPO_PATH = 'D:/github-repos/snavesutit/blockbench-plugins/plugins/animated_java'
1516
const PLUGIN_MANIFEST_PATH = 'D:/github-repos/snavesutit/blockbench-plugins/plugins.json'
17+
const CHANGELOG_PATH = './src/pluginPackage/changelog.json'
18+
const RELEASE_NOTES_TEMPLATES = './tools/plugins/releaseNoteTemplates/'
19+
20+
function replaceTemplateVars(str: string, items: Record<string, string>) {
21+
return str.replace(/\{(.+?)\}/g, str => items[str.replace(/[\{\}]/g, '')] || str)
22+
}
1623

1724
function plugin(): Plugin {
1825
return {
1926
name: 'packagerPlugin',
2027
setup(build) {
2128
build.onEnd(() => {
22-
fs.rmSync(DIST_PATH, { recursive: true, force: true })
23-
fs.cpSync(PLUGIN_PACKAGE_PATH, DIST_PATH, { recursive: true })
29+
console.log('📦 Packaging...')
30+
fs.rmSync(DIST_PACKAGE_PATH, { recursive: true, force: true })
31+
fs.cpSync(PLUGIN_PACKAGE_PATH, DIST_PACKAGE_PATH, { recursive: true })
2432
fs.copyFileSync(
2533
`./dist/${PACKAGE.name}.js`,
26-
pathjs.join(DIST_PATH, PACKAGE.name + '.js')
34+
pathjs.join(DIST_PACKAGE_PATH, PACKAGE.name + '.js')
2735
)
2836
const svelteResult = c.compile(readFileSync(SVELTE_FILE, 'utf-8'), {
2937
generate: 'ssr',
@@ -41,31 +49,54 @@ function plugin(): Plugin {
4149
const result = component(svelteInternal).render()
4250
const html = `${result.html}\n<style>${result.css.code}</style>`
4351
writeFileSync(README_DIST_PATH, html)
44-
if (fs.existsSync(pathjs.join(DIST_PATH, 'about.svelte')))
45-
fs.unlinkSync(pathjs.join(DIST_PATH, 'about.svelte'))
46-
47-
if (process.env.NODE_ENV === 'production' && fs.existsSync(PLUGIN_REPO_PATH)) {
48-
fs.rmSync(PLUGIN_REPO_PATH, { recursive: true, force: true })
49-
fs.cpSync(DIST_PATH, PLUGIN_REPO_PATH, { recursive: true })
50-
const manifest = JSON.parse(fs.readFileSync(PLUGIN_MANIFEST_PATH, 'utf-8'))
51-
manifest.animated_java.title = PACKAGE.title
52-
manifest.animated_java.author = PACKAGE.author.name
53-
manifest.animated_java.icon = PACKAGE.icon
54-
manifest.animated_java.description = PACKAGE.description
55-
manifest.animated_java.version = PACKAGE.version
56-
manifest.animated_java.min_version = PACKAGE.min_blockbench_version
57-
manifest.animated_java.variant = PACKAGE.variant
58-
manifest.animated_java.tags = PACKAGE.tags
59-
manifest.animated_java.has_changelog = true
52+
if (fs.existsSync(pathjs.join(DIST_PACKAGE_PATH, 'about.svelte')))
53+
fs.unlinkSync(pathjs.join(DIST_PACKAGE_PATH, 'about.svelte'))
6054

61-
fs.writeFileSync(
62-
PLUGIN_MANIFEST_PATH,
63-
prettier.format(JSON.stringify(manifest, null, '\t'), {
64-
useTabs: true,
65-
parser: 'json',
55+
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'),
6673
})
67-
)
68-
console.log('📋 Copied to Plugin Repo!')
74+
fs.writeFileSync(pathjs.join(DIST_PATH, file), content)
75+
}
76+
77+
if (fs.existsSync(PLUGIN_REPO_PATH)) {
78+
fs.rmSync(PLUGIN_REPO_PATH, { recursive: true, force: true })
79+
fs.cpSync(DIST_PACKAGE_PATH, PLUGIN_REPO_PATH, { recursive: true })
80+
const manifest = JSON.parse(fs.readFileSync(PLUGIN_MANIFEST_PATH, 'utf-8'))
81+
manifest.animated_java.title = PACKAGE.title
82+
manifest.animated_java.author = PACKAGE.author.name
83+
manifest.animated_java.icon = PACKAGE.icon
84+
manifest.animated_java.description = PACKAGE.description
85+
manifest.animated_java.version = PACKAGE.version
86+
manifest.animated_java.min_version = PACKAGE.min_blockbench_version
87+
manifest.animated_java.variant = PACKAGE.variant
88+
manifest.animated_java.tags = PACKAGE.tags
89+
manifest.animated_java.has_changelog = true
90+
91+
fs.writeFileSync(
92+
PLUGIN_MANIFEST_PATH,
93+
prettier.format(JSON.stringify(manifest, null, '\t'), {
94+
useTabs: true,
95+
parser: 'json',
96+
})
97+
)
98+
console.log('📋 Copied to Plugin Repo!')
99+
}
69100
}
70101
})
71102
},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# :AnimatedJava: Animated Java Release {version}
2+
3+
### Changes
4+
5+
{changes}
6+
7+
### Fixes
8+
9+
{fixes}
10+
11+
## How to Install
12+
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.
17+
18+
{pings}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Animated Java Release {version}
2+
3+
### Changes
4+
5+
{changes}
6+
7+
### Fixes
8+
9+
{fixes}
10+
11+
## How to Install
12+
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.

0 commit comments

Comments
 (0)