|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +// ── 读取版本号 ── |
| 5 | +const pkgPath = path.join(__dirname, '..', 'miniprogram', 'package.json'); |
| 6 | +const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); |
| 7 | +const version = pkg.version; |
| 8 | + |
| 9 | +// ── 解析 push 携带的 commit 信息 ── |
| 10 | +let commits = []; |
| 11 | +try { |
| 12 | + commits = JSON.parse(process.env.PUSH_COMMITS || '[]'); |
| 13 | +} catch { |
| 14 | + console.warn('Failed to parse PUSH_COMMITS, using default entry.'); |
| 15 | +} |
| 16 | + |
| 17 | +// ── commit → changelog detail ── |
| 18 | +const details = commits |
| 19 | + .filter(c => c.message && !/^Merge/i.test(c.message)) |
| 20 | + .map(c => { |
| 21 | + let msg = c.message.trim(); |
| 22 | + let type = 'feature'; |
| 23 | + |
| 24 | + // 解析 Conventional Commits 格式 |
| 25 | + if (/^fix[:(]/.test(msg)) { |
| 26 | + type = 'bug'; |
| 27 | + msg = msg.replace(/^fix[:(]\s*/, '').replace(/\)\s*$/, '').replace(/\):/, ':'); |
| 28 | + } else if (/^feat[:(]/.test(msg)) { |
| 29 | + type = 'feature'; |
| 30 | + msg = msg.replace(/^feat[:(]\s*/, '').replace(/\)\s*$/, '').replace(/\):/, ':'); |
| 31 | + } else if (/^chore[:(]/.test(msg) || /^build[:(]/.test(msg) || /^ci[:(]/.test(msg)) { |
| 32 | + // skip chore/build/ci commits — not user-facing |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + return { type, value: msg.slice(0, 200) }; |
| 37 | + }) |
| 38 | + .filter(Boolean); |
| 39 | + |
| 40 | +// 没有任何有效 commit 时给一个占位 |
| 41 | +if (details.length === 0) { |
| 42 | + details.push({ type: 'feature', value: '版本更新' }); |
| 43 | +} |
| 44 | + |
| 45 | +// 去重:相同 value 只保留一条 |
| 46 | +const seen = new Set(); |
| 47 | +const unique = details.filter(d => { |
| 48 | + if (seen.has(d.value)) return false; |
| 49 | + seen.add(d.value); |
| 50 | + return true; |
| 51 | +}); |
| 52 | + |
| 53 | +// ── 生成日期 ── |
| 54 | +const today = new Date().toISOString().slice(0, 10); |
| 55 | + |
| 56 | +// ── 更新 log.js ── |
| 57 | +const logPath = path.join( |
| 58 | + __dirname, '..', 'miniprogram', 'pages', 'changelog', 'log.js' |
| 59 | +); |
| 60 | +let content = fs.readFileSync(logPath, 'utf8'); |
| 61 | + |
| 62 | +// 避免重复插入 |
| 63 | +if (content.includes(`version: '${version}'`)) { |
| 64 | + console.log(`⚠️ v${version} already in log.js, skipped.`); |
| 65 | + process.exit(0); |
| 66 | +} |
| 67 | + |
| 68 | +const entry = `{ |
| 69 | + version: '${version}', |
| 70 | + detail: ${JSON.stringify(unique, null, 4)}, |
| 71 | + date: '${today}' |
| 72 | +}, |
| 73 | +`; |
| 74 | + |
| 75 | +content = content.replace('export default [', `export default [\n ${entry}`); |
| 76 | +fs.writeFileSync(logPath, content, 'utf8'); |
| 77 | + |
| 78 | +console.log(`✅ Added changelog entry for v${version}`); |
| 79 | +console.log(JSON.stringify(unique, null, 2)); |
0 commit comments