فهرست منبع

feat(scripts): 测试日志生成

liujia 2 ماه پیش
والد
کامیت
375e86d8da
2فایلهای تغییر یافته به همراه52 افزوده شده و 63 حذف شده
  1. 3 2
      package.json
  2. 49 61
      scripts/release.js

+ 3 - 2
package.json

@@ -10,9 +10,10 @@
     "build:dev": "vite build --mode development",
     "build:test": "vite build --mode test",
     "build": "vite build",
-    "changelog": "npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
     "bump": "npm version patch -m \"new version published: v%s\"",
     "release": "node scripts/release.js",
+    "changelog": "npx conventional-changelog -p angular -i CHANGELOG.md -s",
+    "changelog:all": "npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
     "deploy:test": "npm-run-all build:test run:deploy",
     "run:deploy": "pwsh -ExecutionPolicy Bypass -File scripts/deploy.ps1",
     "preview": "vite preview",
@@ -65,4 +66,4 @@
     "vite-plugin-vue-devtools": "^7.7.7",
     "vue-tsc": "^2.2.10"
   }
-}
+}

+ 49 - 61
scripts/release.js

@@ -1,80 +1,68 @@
-#!/usr/bin/env node
-import { execSync } from 'node:child_process'
-import fs from 'node:fs'
+import inquirer from 'inquirer'
+import { execSync } from 'child_process'
+import fs from 'fs'
 
+// 运行命令工具
 const run = (cmd) => execSync(cmd, { stdio: 'inherit' })
-const out = (cmd) => execSync(cmd).toString().trim()
 
-console.log('\n🚀 发布流程开始...\n')
+// 检查 Git 工作区是否干净
+const isGitClean = () => {
+  const status = execSync('git status --porcelain').toString().trim()
+  return status === ''
+}
 
-// 1) 检查 Git 工作区是否干净
-if (out('git status --porcelain')) {
-  console.error('❌ Git 工作区有未提交的改动,请先提交后再发版!\n')
-  process.exit(1)
+// 检查是否有符合规范的提交(feat、fix 等)
+const hasConventionalCommits = () => {
+  const log = execSync('git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:%s')
+    .toString()
+    .trim()
+  return /^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?:/m.test(log)
 }
 
-// 2) 动态导入 inquirer
-const inquirer = (await import('inquirer')).default
+// 1. 检查 git 工作区
+if (!isGitClean()) {
+  console.error('❌ Git 工作区有未提交的改动,请先提交后再发版!')
+  process.exit(1)
+}
 
-// 3) 选择版本类型
-const { releaseType } = await inquirer.prompt([
-  {
-    type: 'list',
-    name: 'releaseType',
-    message: '请选择版本类型:',
-    choices: [
-      { name: '补丁版本 (patch)', value: 'patch' },
-      { name: '次版本 (minor)', value: 'minor' },
-      { name: '主版本 (major)', value: 'major' },
-    ],
-  },
-])
+// 2. 检查是否有符合规范的提交
+if (!hasConventionalCommits()) {
+  console.error('⚠️ 没有检测到符合 Angular 提交规范的提交,跳过 changelog 生成!')
+  process.exit(1)
+}
 
-// 4) 是否全量生成 changelog
-const { changelogMode } = await inquirer.prompt([
+// 3. 询问生成 changelog 的方式
+const { mode } = await inquirer.prompt([
   {
     type: 'list',
-    name: 'changelogMode',
-    message: '生成 CHANGELOG 模式:',
+    name: 'mode',
+    message: '请选择生成 changelog 的方式:',
     choices: [
-      { name: '只追加自上次发布以来的日志 (推荐)', value: 'append' },
-      { name: '重新生成完整历史日志 (谨慎)', value: 'full' },
+      { name: '只追加日志(推荐)', value: 'incremental' },
+      { name: '全量生成日志(会覆盖原文件)', value: 'all' },
     ],
-    default: 'append',
+    default: 'incremental',
   },
 ])
 
-// 5) 升级版本号
-run(`npm version ${releaseType} -m "new version published: v%s"`)
-
-// 6) 生成或更新 CHANGELOG
-try {
-  let cmd
-  if (changelogMode === 'full') {
-    cmd = 'npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0'
-  } else {
-    if (!fs.existsSync('CHANGELOG.md')) {
-      console.log('📄 未检测到 CHANGELOG.md,正在初始化...')
-      cmd = 'npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0'
-    } else {
-      cmd = 'npx conventional-changelog -p angular -i CHANGELOG.md -s'
-    }
-  }
-
-  run(cmd)
+// 4. 生成 changelog
+if (mode === 'all') {
+  run('npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0')
+} else {
+  run('npx conventional-changelog -p angular -i CHANGELOG.md -s')
+}
 
-  // 提交 CHANGELOG
-  if (/CHANGELOG\.md/.test(out('git status --porcelain'))) {
-    run('git add CHANGELOG.md')
-    run('git commit -m "docs: update changelog"')
-  }
-} catch {
-  console.warn(
-    '⚠ 生成 changelog 失败:请确认已安装 conventional-changelog-cli(devDependencies)。'
-  )
+// 5. 检查 changelog 是否真的更新
+const changelog = fs.readFileSync('CHANGELOG.md', 'utf8')
+if (!changelog.includes(new Date().getFullYear().toString())) {
+  console.error('⚠️ changelog 没有生成新的内容,请检查提交记录!')
+  process.exit(1)
 }
 
-// 7) 推送代码与标签
-run('git push --follow-tags')
+// 6. 更新版本号(补丁版本)
+run('npm version patch -m "new version published: v%s"')
+
+// 7. 推送代码和 tag
+run('git push && git push --tags')
 
-console.log('\n🎉 发布完成!\n')
+console.log('✅ 发布完成!')