Skip to content

Commit d0ba9af

Browse files
committed
chore: add release.js to auto update plugin version and release
1 parent cbf25a0 commit d0ba9af

File tree

5 files changed

+92
-24
lines changed

5 files changed

+92
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656

5757
> [!TIP]
5858
>
59-
> This template provided a script to auto tag. Change your plugin version in `plugin.json` and save the file, then run `pnpm commitAndTag` in the project root.
59+
> This template provided a script to auto create tag and release. You can use `pnpm release` to create a patch version.
6060
>
61-
> The script will help you to commit the `plugin.json` file as `update version`.
62-
>
63-
> Then use the `version` in `plugin.json` to create a `tag` and push to the Github.
61+
> You can add `--mode=manual|patch|minor|major` arg to set release mode, or run with arg like `pnpm release:manual`.
62+
>
63+
> All the scripts please see the `package.json` file.
6464
6565
The github action is included in this sample, you can use it to publish your new realse to marketplace automatically:
6666

README_zh_CN.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@
5858

5959
> [!TIP]
6060
>
61-
> 这个项目提供了一个根据 `plugin.json` 中的 `version` 自动创建 `tag` 的脚本。更改 `plugin.json` 中的 `version` 并保存文件,然后在项目根目录运行 `pnpm commitAndTag`
61+
> 这个项目提供了自动创建 `tag` 并发布新版本的脚本,你可以通过运行 `pnpm release` 创建一个修正版本
6262
>
63-
> 脚本会用 `update version` 提交你的 `plugin.json` 文件。然后使用 `version` 创建 `tag` 并推送至 Github。
63+
> 你可以通过使用参数 `--mode=manual|patch|minor|major` 设置版本号的调整模式,或者通过 `pnpm release:manual` 的方式直接以特定参数进行发布。
64+
>
65+
> 完整的命令列表请查看 `package.json` 文件。
6466
6567

6668
样例中自带了 github action,可以自动打包发布,请遵循以下操作:

autoTag.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
"repository": "",
1010
"scripts": {
1111
"dev": "vite build --watch",
12-
"commitAndTag": "git add ./plugin.json && git commit -m 'update version' && git push && node ./autoTag.js",
12+
"release": "node ./release.js",
13+
"release:manual": "node ./release.js --mode=manual",
14+
"release:patch": "node ./release.js --mode=patch",
15+
"release:minor": "node ./release.js --mode=minor",
16+
"release:major": "node ./release.js --mode=major",
1317
"build": "vite build"
1418
},
1519
"dependencies": {

release.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { exec } from 'node:child_process'
2+
import {
3+
readFileSync,
4+
writeFileSync,
5+
} from 'node:fs'
6+
import { createRequire } from 'node:module'
7+
import process from 'node:process'
8+
9+
const require = createRequire(import.meta.url)
10+
const PluginInfo = require('./plugin.json')
11+
12+
const {
13+
version,
14+
} = PluginInfo
15+
let [major, minor, patch] = version.split('.').map(Number)
16+
17+
const args = process.argv.slice(2)
18+
const mode = args.find((arg) => arg.startsWith('--mode='))?.split('=')[1] || 'patch'
19+
const isManual = mode === 'manual'
20+
21+
console.log(`Update mode: ${mode}`)
22+
switch (mode) {
23+
case 'manual':
24+
break
25+
case 'major':
26+
major++
27+
minor = 0
28+
patch = 0
29+
break
30+
case 'minor':
31+
minor++
32+
patch = 0
33+
break
34+
case 'patch':
35+
patch++
36+
break
37+
default:
38+
console.log()
39+
console.error('\x1B[31m%s\x1B[0m', `Invalid [mode] arg. Use major, minor, or patch`)
40+
console.log()
41+
process.exit(1)
42+
}
43+
44+
const newVersion = `${major}.${minor}.${patch}`
45+
PluginInfo.version = newVersion
46+
47+
if (isManual) {
48+
console.log(`Ready to release => v${newVersion}`)
49+
} else {
50+
console.log(`Ready to release: v${version} => v${newVersion}`)
51+
52+
const content = readFileSync('./plugin.json', 'utf8')
53+
const updated = content.replace(
54+
/"version"\s*:\s*"[^"]+"/,
55+
`"version": "${newVersion}"`,
56+
)
57+
writeFileSync('./plugin.json', updated, 'utf8')
58+
}
59+
60+
61+
62+
exec(
63+
`git add ./plugin.json && git commit -m "chore: update version to ${newVersion}" && git push && git tag v${newVersion}`,
64+
(err, stdout) => {
65+
if (err) {
66+
console.error('\x1B[31m%s\x1B[0m', 'Error:', err)
67+
process.exit(1)
68+
}
69+
70+
exec(`git push origin v${newVersion}`, (err) => {
71+
if (err) {
72+
console.error('\x1B[31m%s\x1B[0m', 'Error pushing tag:', err)
73+
process.exit(1)
74+
}
75+
console.log('\x1B[32m%s\x1B[0m', `✨ Version ${newVersion} released successfully`)
76+
})
77+
},
78+
)
79+

0 commit comments

Comments
 (0)