Browse Source

fix: 日志增量生成功能优化

liujia 2 months ago
parent
commit
e6893bd652
1 changed files with 28 additions and 34 deletions
  1. 28 34
      scripts/release.js

+ 28 - 34
scripts/release.js

@@ -11,18 +11,6 @@ function getVersion() {
   return JSON.parse(fs.readFileSync('package.json', 'utf8')).version
 }
 
-function hasPreviousTag() {
-  try {
-    const tags = execSync('git tag --sort=-creatordate', { encoding: 'utf8' })
-      .trim()
-      .split('\n')
-      .filter(Boolean)
-    return tags.length > 0
-  } catch {
-    return false
-  }
-}
-
 async function main() {
   console.log('🚀 开始发布流程...')
 
@@ -40,41 +28,47 @@ async function main() {
     },
   ])
 
-  // 2. 更新版本号(会生成新 tag)
-  run(`npm version ${versionType} -m "new version published: v%s"`)
-
-  // 读取新版本号
-  const version = getVersion()
+  // 2. 选择日志生成模式
+  const { changelogMode } = await inquirer.prompt([
+    {
+      type: 'list',
+      name: 'changelogMode',
+      message: '请选择 changelog 生成模式:',
+      choices: [
+        { name: '全量生成(会覆盖整个文件)', value: 'full' },
+        { name: '增量生成(只追加本次更新的内容)', value: 'incremental' },
+      ],
+    },
+  ])
 
-  // 3. 生成 changelog(增量优先)
-  if (hasPreviousTag()) {
-    console.log('📝 检测到已有 tag,执行增量 changelog 生成...')
-    run('npx conventional-changelog -p angular -i CHANGELOG.md -s')
-  } else {
-    console.log('📝 未检测到历史 tag,执行全量 changelog 生成...')
+  // 3. 根据模式生成 changelog
+  console.log('📝 正在生成 changelog...')
+  if (changelogMode === 'full') {
     run('npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0')
+  } else {
+    run('npx conventional-changelog -p angular -i CHANGELOG.md -s')
   }
 
-  // 4. 自动提交 changelog
+  // 4. 提交 changelog
   run('git add CHANGELOG.md')
-  run('git commit --amend --no-edit') // 合并到刚才的提交
+  run('git commit -m "chore: update changelog"')
+
+  // 5. 更新版本号并生成 tag(会合并 changelog 提交)
+  run(`npm version ${versionType} -m "new version published: v%s"`)
+
+  const version = getVersion()
 
-  // 5. 推送代码 & tag
-  run('git push --force-with-lease')
-  run('git push --tags')
+  // 6. 推送代码 & tag
+  run('git push --follow-tags')
 
-  // 6. 彩色提示
+  // 7. 彩色提示
   console.log('\x1b[36m=========================\x1b[0m')
   console.log('\x1b[32m🎉 发布成功!\x1b[0m')
   console.log(`\x1b[33m📦 版本号:v${version}\x1b[0m`)
   console.log('\x1b[35m⚙️  部署:\x1b[0m')
-  console.log('\x1b[34m  * 打包并部署到测试环境(单机版):npm run deploy:test\x1b[0m')
-  console.log('\x1b[35m⚙️  打包:\x1b[0m')
+  console.log('\x1b[34m  * 测试环境:npm run deploy:test\x1b[0m')
   console.log('\x1b[34m  * 开发环境:npm run build:dev\x1b[0m')
-  console.log('\x1b[34m  * 测试环境:npm run build:test\x1b[0m')
   console.log('\x1b[34m  * 生产环境:npm run build\x1b[0m')
-  console.log('\x1b[35m⚙️  同步代码:\x1b[0m')
-  console.log('\x1b[34m  * 同步dev分支代码到prod分支:bash scripts/merge_dev_to_pro.sh\x1b[0m')
   console.log('\x1b[36m=========================\x1b[0m')
 }