Skip to content

Commit 6d8081a

Browse files
authored
ci(workflows): added automatic upgrade function for external test packages (#2804)
* fix: fix alpha publish error * ci(workflows): added automatic upgrade function for external test packages * ci(workflows): added automatic upgrade function for external test packages
1 parent 9a71be6 commit 6d8081a

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

.github/workflows/dispatch-renderless-theme-publish-alpha.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ jobs:
3030
throw new Error('请输入正确的包名称')
3131
}
3232
33-
if (!branchName.includes('release-3.')) {
34-
throw new Error('请使用release-3.xx.xx分支发布正式包')
35-
}
36-
3733
- name: CheckOut Code
3834
uses: actions/checkout@master
3935
with:
@@ -85,7 +81,7 @@ jobs:
8581
run: pnpm build:runtime
8682

8783
- name: Run Release alpha
88-
run: pnpm release:alpha
84+
run: pnpm release:alpha -u
8985

9086
- name: Publish
9187
run: |

.github/workflows/dispatch-ui-publish-alpha.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ jobs:
1818
build:
1919
runs-on: windows-latest
2020
steps:
21-
- name: Parse Components
22-
id: parseComponents
23-
uses: actions/github-script@v6
24-
with:
25-
script: |
26-
const branchName = `${{ github.ref_name }}`
27-
28-
if (!branchName.includes('release-3.')) {
29-
throw new Error('请使用release-3.xx.xx分支发布正式包')
30-
}
31-
3221
- name: CheckOut Code
3322
uses: actions/checkout@master
3423
with:
@@ -63,7 +52,7 @@ jobs:
6352
run: pnpm build:ui ${{ inputs.components }}
6453

6554
- name: Run Release alpha
66-
run: pnpm release:alpha
55+
run: pnpm release:alpha -u
6756

6857
- name: Publish
6958
run: |
Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
11
import { pathFromPackages } from '../build/build-ui'
22
import path from 'node:path'
33
import fs from 'fs-extra'
4+
import semver from 'semver'
5+
import { execSync } from 'node:child_process'
46

57
const excludeFiles = ['.png', '.gif', '.jpeg', '.jpg', '.ttf', 'node_modules']
68

9+
/**
10+
* @param {string} packageName 包名
11+
* @param {string} version 原始版本号
12+
* @returns {string} 自动升级patch版本后的版本号
13+
*/
14+
const getPatchVersion = (packageName: string, version: string): string => {
15+
try {
16+
// 防止测试仓库没有发布过该包,导致获取不到版本号
17+
const npmVersion = execSync(`npm v ${packageName} version`).toString('utf-8').replace(/\n/, '')
18+
const updateVersion = version.startsWith('2.') ? `2${npmVersion.slice(1)}` : npmVersion
19+
return semver.inc(updateVersion, 'patch')
20+
} catch (error) {
21+
return version
22+
}
23+
}
24+
725
// 递归遍历所有的组件,然后依次修改文件内容
8-
const findAllpage = (packagesPath) => {
26+
const findAllpage = (packagesPath, updateVersion) => {
927
if (excludeFiles.some((item) => packagesPath.includes(item)) || !fs.existsSync(packagesPath)) {
1028
return
1129
}
1230

1331
if (fs.statSync(packagesPath).isDirectory()) {
1432
// 循环递归查找子文件夹
1533
fs.readdirSync(packagesPath).forEach((childPatch) => {
16-
findAllpage(path.join(packagesPath, childPatch))
34+
findAllpage(path.join(packagesPath, childPatch), updateVersion)
1735
})
1836
} else {
1937
const content = fs.readFileSync(packagesPath).toString('UTF-8' as BufferEncoding)
20-
let result = content.replace(/@opentiny\/vue/g, '@opentinyvue/vue')
38+
const result = content.replace(/@opentiny\/vue/g, '@opentinyvue/vue')
2139

22-
fs.writeFileSync(packagesPath, result)
40+
if (packagesPath.endsWith('package.json') && updateVersion) {
41+
const packageJSON = JSON.parse(result)
42+
packageJSON.version = getPatchVersion(packageJSON.name, packageJSON.version)
43+
fs.writeFileSync(packagesPath, JSON.stringify(packageJSON, null, 2) + '\n')
44+
} else {
45+
fs.writeFileSync(packagesPath, result)
46+
}
2347
}
2448
}
2549

26-
export const releaseAlpha = () => {
50+
export const releaseAlpha = ({ updateVersion }) => {
2751
const distLists = ['dist3/', 'dist2/', 'renderless/dist', 'theme/dist', 'theme-mobile/dist', 'theme-saas/dist']
2852
distLists.forEach((item) => {
29-
findAllpage(pathFromPackages(item))
53+
findAllpage(pathFromPackages(item), updateVersion)
3054
})
3155
}

internals/cli/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const program = new Command()
99

1010
program.command('release:aurora').description('转换为aurora的包').action(releaseAurora)
1111

12-
program.command('release:alpha').description('转换为组织名为@opentinyvue的包').action(releaseAlpha)
12+
program
13+
.command('release:alpha')
14+
.description('转换为组织名为@opentinyvue的包')
15+
.option('-u, --updateVersion', '是否自动升级patch版本号', false)
16+
.action(releaseAlpha)
1317

1418
program.command('create:icon-saas').description('同步生成 icon-saas').action(createIconSaas)
1519

0 commit comments

Comments
 (0)