Browse Source

feat: 更新发布脚本

liujia 2 months ago
parent
commit
2010d42
1 changed files with 61 additions and 19 deletions
  1. 61 19
      scripts/release.js

+ 61 - 19
scripts/release.js

@@ -3,18 +3,64 @@ import { execSync } from 'child_process'
 import inquirer from 'inquirer'
 import inquirer from 'inquirer'
 import fs from 'fs'
 import fs from 'fs'
 
 
-function run(cmd) {
-  execSync(cmd, { stdio: 'inherit' })
+function run(cmd, options = {}) {
+  execSync(cmd, { stdio: 'inherit', ...options })
+}
+
+function runSilent(cmd) {
+  return execSync(cmd, { encoding: 'utf-8' }).trim()
 }
 }
 
 
 function getVersion() {
 function getVersion() {
   return JSON.parse(fs.readFileSync('package.json', 'utf8')).version
   return JSON.parse(fs.readFileSync('package.json', 'utf8')).version
 }
 }
 
 
+function getLastTag() {
+  try {
+    return runSilent('git describe --tags --abbrev=0')
+  } catch {
+    return null // 没有 tag
+  }
+}
+
+function getCommitsSince(tag) {
+  const range = tag ? `${tag}..HEAD` : ''
+  try {
+    return runSilent(`git log ${range} --pretty=format:"- %s (%h)"`)
+  } catch {
+    return ''
+  }
+}
+
+function updateChangelog(version, mode = 'incremental') {
+  const lastTag = getLastTag()
+  const commits = getCommitsSince(lastTag)
+
+  if (!commits) {
+    console.log('⚠️ 没有新提交,但依旧会记录版本变化。')
+  }
+
+  const date = new Date().toISOString().slice(0, 10)
+  const newEntry = `\n## ${version} (${date})\n${commits || '- No changes'}\n`
+
+  let changelog = ''
+  if (fs.existsSync('CHANGELOG.md')) {
+    changelog = fs.readFileSync('CHANGELOG.md', 'utf-8')
+  }
+
+  if (mode === 'full') {
+    fs.writeFileSync('CHANGELOG.md', newEntry, 'utf-8')
+  } else {
+    fs.writeFileSync('CHANGELOG.md', newEntry + changelog, 'utf-8')
+  }
+
+  console.log(`✅ changelog 已更新到版本 ${version}`)
+}
+
 async function main() {
 async function main() {
   console.log('🚀 开始发布流程...')
   console.log('🚀 开始发布流程...')
 
 
-  // 选择版本类型(默认 patch)
+  // 选择版本类型
   const { versionType } = await inquirer.prompt([
   const { versionType } = await inquirer.prompt([
     {
     {
       type: 'list',
       type: 'list',
@@ -29,12 +75,12 @@ async function main() {
     },
     },
   ])
   ])
 
 
-  // 更新版本
-  run(`npm version ${versionType} -m "feat: release new version: v%s"`)
+  // 更新版本号(不自动打 tag)
+  run(`npm version ${versionType} --no-git-tag-version`)
 
 
   const version = getVersion()
   const version = getVersion()
 
 
-  // 选择 changelog 生成模式(默认 incremental)
+  // 选择 changelog 生成模式
   const { changelogMode } = await inquirer.prompt([
   const { changelogMode } = await inquirer.prompt([
     {
     {
       type: 'list',
       type: 'list',
@@ -48,14 +94,10 @@ async function main() {
     },
     },
   ])
   ])
 
 
-  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')
-  }
+  // 生成 changelog
+  updateChangelog(`v${version}`, changelogMode)
 
 
-  // 安全 add(存在才加)
+  // 提交变更
   if (fs.existsSync('package-lock.json')) {
   if (fs.existsSync('package-lock.json')) {
     run('git add package.json package-lock.json')
     run('git add package.json package-lock.json')
   } else {
   } else {
@@ -63,15 +105,15 @@ async function main() {
   }
   }
   run('git add CHANGELOG.md')
   run('git add CHANGELOG.md')
 
 
-  // 仅在有变更时提交
   try {
   try {
-    run(`git diff --cached --quiet || git commit -m "chore: update changelog for v${version}"`)
-  } catch (err) {
-    console.log('⚠️ 没有需要提交的文件,跳过 commit', err)
+    run(`git diff --cached --quiet || git commit -m "chore: release v${version}"`)
+  } catch {
+    console.log('⚠️ 没有需要提交的文件,跳过 commit')
   }
   }
 
 
-  // 推送代码 & tag
-  run('git push --follow-tags')
+  // 打 tag & 推送
+  run(`git tag v${version}`)
+  run('git push && git push --tags')
 
 
   console.log('\x1b[36m=========================\x1b[0m')
   console.log('\x1b[36m=========================\x1b[0m')
   console.log('\x1b[32m🎉 发布成功!\x1b[0m')
   console.log('\x1b[32m🎉 发布成功!\x1b[0m')