소스 검색

feat:更新部署脚本

liujia 2 달 전
부모
커밋
7cd5409e01
1개의 변경된 파일34개의 추가작업 그리고 71개의 파일을 삭제
  1. 34 71
      scripts/release.js

+ 34 - 71
scripts/release.js

@@ -1,81 +1,44 @@
-import inquirer from 'inquirer'
+#!/usr/bin/env node
 import { execSync } from 'child_process'
-import fs from 'fs'
-import path from 'path'
-
-// changelog 文件路径
-const changelogPath = path.resolve('CHANGELOG.md')
-
-// 运行命令工具
-const run = (cmd) => execSync(cmd, { stdio: 'inherit' })
-
-// 检查 Git 工作区是否干净
-const isGitClean = () => {
-  const status = execSync('git status --porcelain').toString().trim()
-  return status === ''
-}
+import inquirer from 'inquirer'
 
-// 检查是否有符合规范的提交
-const hasConventionalCommits = () => {
-  let lastTag = ''
-  try {
-    lastTag = execSync('git describe --tags --abbrev=0', { stdio: 'pipe' }).toString().trim()
-  } catch {
-    // 没有 tag 不影响
-  }
-  const logCmd = lastTag
-    ? `git log ${lastTag}..HEAD --pretty=format:%s`
-    : `git log --pretty=format:%s`
-  const log = execSync(logCmd, { stdio: 'pipe' }).toString().trim()
-  return /^(feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?:/m.test(log)
+function run(cmd) {
+  execSync(cmd, { stdio: 'inherit' })
 }
 
-// 1. 确保 changelog 文件存在
-if (!fs.existsSync(changelogPath)) {
-  console.log(`📄 检测到不存在 ${changelogPath},已自动创建空文件`)
-  fs.writeFileSync(changelogPath, '', 'utf-8')
-}
+async function main() {
+  console.log('🚀 开始发布流程...')
+
+  // 1. 选择版本类型
+  const { versionType } = await inquirer.prompt([
+    {
+      type: 'list',
+      name: 'versionType',
+      message: '请选择版本类型:',
+      choices: [
+        { name: '补丁版本 (patch)', value: 'patch' },
+        { name: '小版本 (minor)', value: 'minor' },
+        { name: '大版本 (major)', value: 'major' },
+      ],
+    },
+  ])
+
+  // 2. 全量生成 changelog
+  console.log('📝 正在全量生成 changelog...')
+  run('npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0')
 
-// 2. 检查 git 工作区
-if (!isGitClean()) {
-  console.error('❌ Git 工作区有未提交的改动,请先提交后再发版!')
-  process.exit(1)
-}
+  // 3. 自动提交 changelog
+  run('git add CHANGELOG.md')
+  run('git commit -m "chore: update changelog"')
 
-// 3. 检查提交规范
-if (!hasConventionalCommits()) {
-  console.error('⚠️ 没有检测到符合 Angular 提交规范的提交,跳过 changelog 生成!')
-  process.exit(1)
-}
+  // 4. 更新版本号
+  run(`npm version ${versionType} -m "new version published: v%s"`)
 
-// 4. 询问生成 changelog 的方式
-const { mode } = await inquirer.prompt([
-  {
-    type: 'list',
-    name: 'mode',
-    message: '请选择 changelog 生成方式:',
-    choices: [
-      { name: '只追加日志(推荐)', value: 'incremental' },
-      { name: '全量生成日志(会覆盖原文件)', value: 'all' },
-    ],
-    default: 'incremental',
-  },
-])
+  // 5. 推送代码 & tag
+  run('git push')
+  run('git push --tags')
 
-// 5. 生成 changelog
-if (mode === 'all') {
-  console.log('📝 正在全量生成 changelog...')
-  fs.writeFileSync(changelogPath, '', 'utf-8') // 先清空
-  run('npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0')
-} else {
-  console.log('📝 正在追加 changelog...')
-  run('npx conventional-changelog -p angular -i CHANGELOG.md -s')
+  console.log('✅ 发布完成!')
 }
 
-// 6. 更新版本号(补丁版本)
-run('npm version patch -m "new version published: v%s"')
-
-// 7. 推送代码和 tag
-run('git push && git push --tags')
-
-console.log('✅ 发布完成!')
+main()