|
1 | 1 | import { pathFromPackages } from '../build/build-ui' |
2 | 2 | import path from 'node:path' |
3 | 3 | import fs from 'fs-extra' |
| 4 | +import semver from 'semver' |
| 5 | +import { execSync } from 'node:child_process' |
4 | 6 |
|
5 | 7 | const excludeFiles = ['.png', '.gif', '.jpeg', '.jpg', '.ttf', 'node_modules'] |
6 | 8 |
|
| 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 | + |
7 | 25 | // 递归遍历所有的组件,然后依次修改文件内容 |
8 | | -const findAllpage = (packagesPath) => { |
| 26 | +const findAllpage = (packagesPath, updateVersion) => { |
9 | 27 | if (excludeFiles.some((item) => packagesPath.includes(item)) || !fs.existsSync(packagesPath)) { |
10 | 28 | return |
11 | 29 | } |
12 | 30 |
|
13 | 31 | if (fs.statSync(packagesPath).isDirectory()) { |
14 | 32 | // 循环递归查找子文件夹 |
15 | 33 | fs.readdirSync(packagesPath).forEach((childPatch) => { |
16 | | - findAllpage(path.join(packagesPath, childPatch)) |
| 34 | + findAllpage(path.join(packagesPath, childPatch), updateVersion) |
17 | 35 | }) |
18 | 36 | } else { |
19 | 37 | 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') |
21 | 39 |
|
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 | + } |
23 | 47 | } |
24 | 48 | } |
25 | 49 |
|
26 | | -export const releaseAlpha = () => { |
| 50 | +export const releaseAlpha = ({ updateVersion }) => { |
27 | 51 | const distLists = ['dist3/', 'dist2/', 'renderless/dist', 'theme/dist', 'theme-mobile/dist', 'theme-saas/dist'] |
28 | 52 | distLists.forEach((item) => { |
29 | | - findAllpage(pathFromPackages(item)) |
| 53 | + findAllpage(pathFromPackages(item), updateVersion) |
30 | 54 | }) |
31 | 55 | } |
0 commit comments