Explorar o código

feat: 小程序发布支持自动递增版本号

- 体验版发布时自动递增版本号(如 0.0.39 -> 0.0.40)
- 自动更新 package.json 和 project.config.json 版本号
- 仍可通过 VERSION 环境变量指定特定版本

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
yourname hai 3 semanas
pai
achega
5e07fac498

+ 57 - 5
.claude/skills/weapp-publish/scripts/publish-weapp.js

@@ -79,6 +79,15 @@ async function uploadExperienceProject(config, options) {
   console.log(`   版本: ${version}`)
   console.log(`   描述: ${desc}`)
 
+  // 更新 project.config.json 中的版本号(确保微信后台正确显示)
+  const projectConfigPath = path.join(config.projectPath, 'project.config.json')
+  if (fs.existsSync(projectConfigPath)) {
+    const projectConfig = JSON.parse(fs.readFileSync(projectConfigPath, 'utf-8'))
+    projectConfig.version = version
+    fs.writeFileSync(projectConfigPath, JSON.stringify(projectConfig, null, 2))
+    console.log(`   已更新 project.config.json 版本号为: ${version}`)
+  }
+
   const project = new ci.Project({
     appid: config.appid,
     type: 'miniProgram',
@@ -186,6 +195,41 @@ async function previewDevProject(config, options) {
   }
 }
 
+/**
+ * 递增版本号 (例如: 0.0.39 -> 0.0.40)
+ */
+function bumpVersion(version) {
+  const parts = version.split('.')
+  const patch = parseInt(parts[2] || '0')
+  parts[2] = (patch + 1).toString()
+  return parts.join('.')
+}
+
+/**
+ * 从 package.json 读取当前版本号
+ */
+function getCurrentVersion(config) {
+  const packageJsonPath = path.join(PROJECT_ROOT, config.dir, 'package.json')
+  if (fs.existsSync(packageJsonPath)) {
+    const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
+    return packageJson.version || '1.0.0'
+  }
+  return '1.0.0'
+}
+
+/**
+ * 更新 package.json 中的版本号
+ */
+function updatePackageVersion(config, version) {
+  const packageJsonPath = path.join(PROJECT_ROOT, config.dir, 'package.json')
+  if (fs.existsSync(packageJsonPath)) {
+    const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
+    packageJson.version = version
+    fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
+    console.log(`   已更新 package.json 版本号为: ${version}`)
+  }
+}
+
 /**
  * 主函数
  */
@@ -211,9 +255,20 @@ async function main() {
   // 开发版默认跳过构建(除非指定 --build)
   const shouldSkipBuild = action === 'dev' && !forceBuild
 
-  // 解析参数
+  // 获取构建配置(需要提前获取以便读取版本号)
+  const config = getBuildConfig(MINI_CONFIGS[miniType], action)
+
+  // 解析参数 - 体验版如果没有指定 VERSION,则自动递增版本号
+  let version = process.env.VERSION
+  if (action === 'experience' && !version) {
+    const currentVersion = getCurrentVersion(config)
+    version = bumpVersion(currentVersion)
+    console.log(`\n🔢 自动递增版本号: ${currentVersion} -> ${version}`)
+    updatePackageVersion(config, version)
+  }
+
   const options = {
-    version: process.env.VERSION || '1.0.0',
+    version: version || '1.0.0',
     desc: process.env.DESC || '自动化发布',
     robot: parseInt(process.env.ROBOT || '1'),
   }
@@ -224,9 +279,6 @@ async function main() {
     process.exit(1)
   }
 
-  // 根据类型和动作获取构建配置
-  const config = getBuildConfig(MINI_CONFIGS[miniType], action)
-
   try {
     // 构建项目(跳过或显式指定构建时)
     if (!shouldSkipBuild && !noBuild) {

+ 1 - 1
mini/package.json

@@ -1,6 +1,6 @@
 {
   "name": "mini",
-  "version": "1.0.0",
+  "version": "0.0.40",
   "private": true,
   "description": "",
   "templateInfo": {

+ 1 - 0
mini/project.config.json

@@ -2,6 +2,7 @@
   "miniprogramRoot": "./dist",
   "projectname": "mini",
   "description": "",
+  "version": "0.0.39",
   "appid": "wx1e791ed2e0229eb8",
   "setting": {
     "urlCheck": false,