r-util-js/scripts/publish.ps1

129 lines
4.2 KiB
PowerShell
Raw Normal View History

2026-03-17 10:24:02 +08:00
# 发布所有子包到私有 npm 仓库
2026-03-17 14:42:03 +08:00
# 使用方法: powershell scripts/publish.ps1 [版本类型]
# 版本类型: major | minor | patch (默认 patch)
# 注意: 发布仓库地址使用各子包 package.json 中的 publishConfig.registry 配置
2026-03-17 10:24:02 +08:00
$ErrorActionPreference = "Stop"
2026-03-17 14:42:03 +08:00
# 获取版本类型参数,默认为 patch
$VERSION_TYPE = if ($args.Count -gt 0) { $args[0] } else { "patch" }
2026-03-17 10:24:02 +08:00
2026-03-17 14:42:03 +08:00
# 验证版本类型
if ($VERSION_TYPE -notin @("major", "minor", "patch")) {
Write-Host "❌ 无效的版本类型: $VERSION_TYPE" -ForegroundColor Red
Write-Host "使用方法: powershell scripts/publish.ps1 [major|minor|patch]" -ForegroundColor Yellow
exit 1
}
Write-Host "🚀 开始发布流程 (版本类型: $VERSION_TYPE)" -ForegroundColor Green
2026-03-17 10:24:02 +08:00
Write-Host ""
# 子包列表
$PACKAGES = @(
"packages/common",
"packages/vue3",
"packages/uni-app",
"packages/vue2"
)
2026-03-17 14:42:03 +08:00
# 1. 检查工作区是否干净
Write-Host "🔍 检查 Git 工作区状态..." -ForegroundColor Cyan
$gitStatus = git status -s
if ($gitStatus) {
Write-Host "⚠️ 工作区有未提交的更改,请先提交或暂存" -ForegroundColor Yellow
git status -s
$continue = Read-Host "是否继续? (y/N)"
if ($continue -ne "y" -and $continue -ne "Y") {
exit 1
}
}
Write-Host ""
# 2. 运行测试
Write-Host "🧪 运行测试..." -ForegroundColor Cyan
pnpm test
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ 测试失败,停止发布" -ForegroundColor Red
exit 1
}
Write-Host ""
# 3. 更新版本号
Write-Host "📝 更新版本号 ($VERSION_TYPE)..." -ForegroundColor Cyan
foreach ($package in $PACKAGES) {
if (Test-Path $package) {
Write-Host " 更新 $package 版本号..." -ForegroundColor Gray
Push-Location $package
npm version $VERSION_TYPE --no-git-tag-version
Pop-Location
}
}
Write-Host ""
# 4. 构建所有包
2026-03-17 10:24:02 +08:00
Write-Host "📦 构建所有包..." -ForegroundColor Cyan
pnpm build
2026-03-17 14:42:03 +08:00
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ 构建失败,停止发布" -ForegroundColor Red
exit 1
}
Write-Host ""
# 5. 更新 README 中的版本信息
Write-Host "📄 更新 README..." -ForegroundColor Cyan
$COMMON_VERSION = node -p "require('./packages/common/package.json').version"
Write-Host " 当前版本: $COMMON_VERSION" -ForegroundColor Gray
Write-Host ""
# 6. 提交版本更新
Write-Host "💾 提交版本更新..." -ForegroundColor Cyan
git add packages/*/package.json
git commit -m "chore(release): publish v$COMMON_VERSION"
if ($LASTEXITCODE -ne 0) {
Write-Host " 没有需要提交的更改" -ForegroundColor Gray
}
2026-03-17 10:24:02 +08:00
Write-Host ""
2026-03-17 14:42:03 +08:00
# 7. 发布每个包
Write-Host "📤 开始发布包到 npm..." -ForegroundColor Cyan
2026-03-17 10:24:02 +08:00
foreach ($package in $PACKAGES) {
if (Test-Path $package) {
Push-Location $package
2026-03-17 14:42:03 +08:00
$PACKAGE_NAME = node -p "require('./package.json').name"
$PACKAGE_VERSION = node -p "require('./package.json').version"
Write-Host "📤 发布 $PACKAGE_NAME@$PACKAGE_VERSION ..." -ForegroundColor Cyan
pnpm publish --no-git-checks
if ($LASTEXITCODE -ne 0) {
Pop-Location
Write-Host "$package 发布失败,停止后续发布" -ForegroundColor Red
exit 1
}
2026-03-17 10:24:02 +08:00
Pop-Location
2026-03-17 14:42:03 +08:00
Write-Host "$PACKAGE_NAME@$PACKAGE_VERSION 发布成功" -ForegroundColor Green
2026-03-17 10:24:02 +08:00
Write-Host ""
} else {
Write-Host "⚠️ 跳过不存在的包: $package" -ForegroundColor Yellow
}
}
2026-03-17 14:42:03 +08:00
# 8. 创建 Git 标签
Write-Host "🏷️ 创建 Git 标签..." -ForegroundColor Cyan
git tag "v$COMMON_VERSION"
Write-Host ""
# 9. 推送到远程仓库
Write-Host "🔼 推送到远程仓库..." -ForegroundColor Cyan
$push = Read-Host "是否推送到远程仓库? (y/N)"
if ($push -eq "y" -or $push -eq "Y") {
git push origin master
git push origin "v$COMMON_VERSION"
Write-Host "✅ 已推送到远程仓库" -ForegroundColor Green
} else {
Write-Host "⚠️ 跳过推送,请手动执行:" -ForegroundColor Yellow
Write-Host " git push origin master" -ForegroundColor Gray
Write-Host " git push origin v$COMMON_VERSION" -ForegroundColor Gray
}
Write-Host ""
Write-Host "🎉 所有包发布完成!版本: v$COMMON_VERSION" -ForegroundColor Green