@anov/gis 1.8.4 → 1.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +102 -82
- package/README.md +338 -337
- package/dist/index.js +1 -1
- package/package.json +88 -88
- package/scripts/release.js +322 -322
package/scripts/release.js
CHANGED
|
@@ -1,323 +1,323 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 一键发版脚本:递增版本号(默认 patch)+ 自动从 git log 生成 CHANGELOG 段落
|
|
3
|
-
*
|
|
4
|
-
* 用法:
|
|
5
|
-
* node scripts/release.js # patch (默认)
|
|
6
|
-
* node scripts/release.js --dry-run # 只预览,不写文件
|
|
7
|
-
* node scripts/release.js --minor # 手动指定 minor 递增
|
|
8
|
-
* node scripts/release.js --major # 手动指定 major 递增
|
|
9
|
-
* node scripts/release.js --force # 跳过 CHANGELOG 滞后检查,直接发版
|
|
10
|
-
* node scripts/release.js --backfill # 回填 CHANGELOG 缺失的版本条目
|
|
11
|
-
* node scripts/release.js --backfill --dry-run
|
|
12
|
-
*
|
|
13
|
-
* 自动变更来源:
|
|
14
|
-
* 1. 优先:找到最近一次把 version 设为当前版本的 commit(上次发版点),取之后的 commits
|
|
15
|
-
* 2. 回退:用最近一次修改 CHANGELOG.md 的 commit 作为锚点
|
|
16
|
-
*
|
|
17
|
-
* 行为:
|
|
18
|
-
* - 按 semver 递增 package.json 的 version(默认 patch)
|
|
19
|
-
* - 检测 CHANGELOG 滞后于 package.json 时,自动报错并提示 --backfill
|
|
20
|
-
* - 在 CHANGELOG.md 顶部插入新版本段落,内容为提交列表(短 hash + subject)
|
|
21
|
-
* - backfill 模式:扫描 git 历史里所有版本号,补齐 CHANGELOG 缺失的条目
|
|
22
|
-
* - 不做任何 git 操作 —— commit / tag 由用户手动完成
|
|
23
|
-
*/
|
|
24
|
-
const fs = require('fs');
|
|
25
|
-
const path = require('path');
|
|
26
|
-
const { spawnSync } = require('child_process');
|
|
27
|
-
|
|
28
|
-
const ROOT = path.join(__dirname, '..');
|
|
29
|
-
const PKG_PATH = path.join(ROOT, 'package.json');
|
|
30
|
-
const CHANGELOG_PATH = path.join(ROOT, 'CHANGELOG.md');
|
|
31
|
-
const LINK_URL = 'https://172.16.222.31:8866/';
|
|
32
|
-
|
|
33
|
-
// ---- semver & 工具函数 ---------------------------------------------------
|
|
34
|
-
|
|
35
|
-
function bumpVersion(current, level) {
|
|
36
|
-
const [major, minor, patch] = current.split('.').map(Number);
|
|
37
|
-
if (level === 'major') return `${major + 1}.0.0`;
|
|
38
|
-
if (level === 'minor') return `${major}.${minor + 1}.0`;
|
|
39
|
-
return `${major}.${minor}.${patch + 1}`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function formatDate(d = new Date()) {
|
|
43
|
-
const pad = (n) => String(n).padStart(2, '0');
|
|
44
|
-
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function buildEntry(version, date, body) {
|
|
48
|
-
return `## [${version}] - ${date}\n\n${body}\n\n[${version}]: ${LINK_URL}\n\n`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// ---- git 查询 ------------------------------------------------------------
|
|
52
|
-
|
|
53
|
-
function git(args) {
|
|
54
|
-
const result = spawnSync('git', ['log', ...args], { encoding: 'utf8' });
|
|
55
|
-
return result.status === 0 ? result.stdout : '';
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function gitRaw(args) {
|
|
59
|
-
return spawnSync('git', args, { encoding: 'utf8' });
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// 找到最近一次把 version 字段设为 currentVersion 的 commit
|
|
63
|
-
function findLastBumpCommit(currentVersion) {
|
|
64
|
-
const escaped = currentVersion.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
65
|
-
const out = git(['--oneline', '-G', `"version":\\s*"${escaped}"`, '--', 'package.json']);
|
|
66
|
-
const lines = out.split('\n').filter(Boolean);
|
|
67
|
-
if (!lines.length) return null;
|
|
68
|
-
const last = lines[lines.length - 1];
|
|
69
|
-
return last.split(/\s+/)[0];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// 回退方案:最近一次修改 CHANGELOG.md 的 commit
|
|
73
|
-
function findLastChangelogCommit() {
|
|
74
|
-
const hash = git(['-1', '--pretty=format:%H', '--', 'CHANGELOG.md']);
|
|
75
|
-
return hash || null;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function getCommitsSince(sinceHash, untilHash = 'HEAD') {
|
|
79
|
-
const out = git([`${sinceHash}..${untilHash}`, '--pretty=format:%h %s']);
|
|
80
|
-
return out ? out.split('\n').filter(Boolean) : [];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// 找到 git 历史里所有稳定版本号变更的锚点(按时间正序,自动去重)
|
|
84
|
-
function findAllBumpAnchors() {
|
|
85
|
-
const out = git(['--reverse', '--pretty=format:%H', '--', 'package.json']);
|
|
86
|
-
const hashes = out.split('\n').filter(Boolean);
|
|
87
|
-
|
|
88
|
-
const firstSeen = new Map(); // version -> 首次出现的 hash(用于排序)
|
|
89
|
-
const lastSeen = new Map(); // version -> 最后出现的 hash(用作锚点)
|
|
90
|
-
|
|
91
|
-
for (const hash of hashes) {
|
|
92
|
-
const result = gitRaw(['show', `${hash}:package.json`]);
|
|
93
|
-
if (result.status !== 0) continue;
|
|
94
|
-
try {
|
|
95
|
-
const pkg = JSON.parse(result.stdout);
|
|
96
|
-
const v = pkg.version;
|
|
97
|
-
// 只考虑稳定版本 (1.2.3),跳过 0.0.0 / 1.2.3-beta.0 等
|
|
98
|
-
if (!v || !/^\d+\.\d+\.\d+$/.test(v)) continue;
|
|
99
|
-
if (!firstSeen.has(v)) firstSeen.set(v, hash);
|
|
100
|
-
lastSeen.set(v, hash); // 后出现会覆盖,保留最后一次(实际发版点)
|
|
101
|
-
} catch {}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// 按 Map 插入顺序返回(就是时间正序)
|
|
105
|
-
const anchors = [];
|
|
106
|
-
for (const [version] of firstSeen) {
|
|
107
|
-
anchors.push({ hash: lastSeen.get(version), version });
|
|
108
|
-
}
|
|
109
|
-
return anchors;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function compareSemver(a, b) {
|
|
113
|
-
const [aM, am, ap] = a.split('.').map(Number);
|
|
114
|
-
const [bM, bm, bp] = b.split('.').map(Number);
|
|
115
|
-
return (aM - bM) || (am - bm) || (ap - bp);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function getCommitDate(hash) {
|
|
119
|
-
const out = git(['-1', '--pretty=format:%ai', hash]);
|
|
120
|
-
const date = out.split(' ')[0];
|
|
121
|
-
return date || formatDate();
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// ---- 文本处理 ------------------------------------------------------------
|
|
125
|
-
|
|
126
|
-
function parseChangelogVersions(content) {
|
|
127
|
-
const set = new Set();
|
|
128
|
-
const re = /##\s+\[(\d+\.\d+\.\d+)\]/g;
|
|
129
|
-
let m;
|
|
130
|
-
while ((m = re.exec(content)) !== null) set.add(m[1]);
|
|
131
|
-
return set;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function formatCommitsAsList(commits) {
|
|
135
|
-
if (!commits.length) return '无变更';
|
|
136
|
-
return commits
|
|
137
|
-
.map((line) => {
|
|
138
|
-
const idx = line.indexOf(' ');
|
|
139
|
-
if (idx === -1) return `- ${line}`;
|
|
140
|
-
return `- ${line.slice(idx + 1)} (\`${line.slice(0, idx)}\`)`;
|
|
141
|
-
})
|
|
142
|
-
.join('\n');
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// 取 prev 到 current 之间的 commits —— 中间严格区间 + 加上 currentHash 自身
|
|
146
|
-
// (发版 commit message 通常就是本次的变更说明)
|
|
147
|
-
function getCommitsForVersion(prevHash, currentHash) {
|
|
148
|
-
// 中间:严格在 prev 之后、current 之前。用 currentHash^ 排除 currentHash 本身
|
|
149
|
-
const middle = prevHash ? getCommitsSince(prevHash, `${currentHash}^`) : [];
|
|
150
|
-
|
|
151
|
-
// 把当前版本的发版 commit 自身放到最前
|
|
152
|
-
const subject = git(['-1', '--pretty=format:%s', currentHash]).trim();
|
|
153
|
-
const bumpLine = `${currentHash.slice(0, 7)} ${subject}`;
|
|
154
|
-
|
|
155
|
-
return [bumpLine, ...middle];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// ---- 模式 1:正常发版 -----------------------------------------------------
|
|
159
|
-
|
|
160
|
-
function runRelease(level, dryRun, force) {
|
|
161
|
-
const pkg = JSON.parse(fs.readFileSync(PKG_PATH, 'utf8'));
|
|
162
|
-
const oldVersion = pkg.version;
|
|
163
|
-
const newVersion = bumpVersion(oldVersion, level);
|
|
164
|
-
|
|
165
|
-
// 检测 CHANGELOG 是否滞后于 package.json(典型场景:先发版后补文档)
|
|
166
|
-
if (!force) {
|
|
167
|
-
const changelog = fs.readFileSync(CHANGELOG_PATH, 'utf8');
|
|
168
|
-
const existing = parseChangelogVersions(changelog);
|
|
169
|
-
if (!existing.has(oldVersion)) {
|
|
170
|
-
const allAnchors = findAllBumpAnchors();
|
|
171
|
-
const sortedListed = [...existing].sort(compareSemver);
|
|
172
|
-
const maxListed = sortedListed[sortedListed.length - 1];
|
|
173
|
-
const missing = maxListed
|
|
174
|
-
? allAnchors.filter((a) => compareSemver(a.version, maxListed) > 0)
|
|
175
|
-
: allAnchors;
|
|
176
|
-
|
|
177
|
-
console.error(`✗ CHANGELOG 滞后于 package.json`);
|
|
178
|
-
console.error(`✗ package.json 当前版本: ${oldVersion}`);
|
|
179
|
-
console.error(`✗ CHANGELOG 最新版本: ${maxListed || '(无)'}`);
|
|
180
|
-
if (missing.length) {
|
|
181
|
-
console.error(`✗ 缺失版本: ${missing.map((m) => m.version).join(', ')}`);
|
|
182
|
-
}
|
|
183
|
-
console.error('');
|
|
184
|
-
console.error('请先跑补齐命令:');
|
|
185
|
-
console.error(' npm run release -- --backfill');
|
|
186
|
-
console.error('');
|
|
187
|
-
console.error('或跳过检查直接发版(忽略历史缺失):');
|
|
188
|
-
console.error(' npm run release -- --force');
|
|
189
|
-
process.exit(1);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const bumpAnchor = findLastBumpCommit(oldVersion);
|
|
194
|
-
const changelogAnchor = bumpAnchor ? null : findLastChangelogCommit();
|
|
195
|
-
const anchor = bumpAnchor || changelogAnchor;
|
|
196
|
-
const anchorSource = bumpAnchor ? `上次 bump ${oldVersion}` : '上次修改 CHANGELOG.md';
|
|
197
|
-
|
|
198
|
-
if (!anchor) {
|
|
199
|
-
console.error(`✗ 未找到 ${oldVersion} 之后的发版锚点`);
|
|
200
|
-
console.error(' 请确认在 git 仓库中执行');
|
|
201
|
-
process.exit(1);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const commits = getCommitsSince(anchor);
|
|
205
|
-
if (commits.length === 0) {
|
|
206
|
-
console.error(`✗ 自 ${anchorSource}(${anchor.slice(0, 7)})以来无新提交,无需发版`);
|
|
207
|
-
console.error(' 请先 commit 一些改动再发版');
|
|
208
|
-
process.exit(1);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const description = formatCommitsAsList(commits);
|
|
212
|
-
const entry = buildEntry(newVersion, formatDate(), description);
|
|
213
|
-
|
|
214
|
-
console.log(`✓ package.json: ${oldVersion} -> ${newVersion}`);
|
|
215
|
-
console.log(`✓ 锚点: ${anchorSource} (${anchor.slice(0, 7)})`);
|
|
216
|
-
console.log(`✓ 收集到 ${commits.length} 条提交`);
|
|
217
|
-
console.log('');
|
|
218
|
-
console.log('--- 即将写入 CHANGELOG.md 的段落 ---');
|
|
219
|
-
console.log(entry);
|
|
220
|
-
console.log('-----------------------------------');
|
|
221
|
-
|
|
222
|
-
if (dryRun) {
|
|
223
|
-
console.log('[dry-run] 未修改任何文件');
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
pkg.version = newVersion;
|
|
228
|
-
fs.writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 4) + '\n', 'utf8');
|
|
229
|
-
|
|
230
|
-
let changelog = fs.readFileSync(CHANGELOG_PATH, 'utf8');
|
|
231
|
-
const firstSectionIdx = changelog.indexOf('\n## [');
|
|
232
|
-
if (firstSectionIdx === -1) {
|
|
233
|
-
changelog = changelog.replace(/\s+$/, '') + '\n\n' + entry;
|
|
234
|
-
} else {
|
|
235
|
-
changelog = changelog.slice(0, firstSectionIdx + 1) + entry + changelog.slice(firstSectionIdx + 1);
|
|
236
|
-
}
|
|
237
|
-
fs.writeFileSync(CHANGELOG_PATH, changelog, 'utf8');
|
|
238
|
-
|
|
239
|
-
console.log('待手动提交:');
|
|
240
|
-
console.log(' git add package.json CHANGELOG.md');
|
|
241
|
-
console.log(` git commit -m "release: 发布 v${newVersion}"`);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// ---- 模式 2:回填历史缺失版本 ---------------------------------------------
|
|
245
|
-
|
|
246
|
-
function runBackfill(dryRun) {
|
|
247
|
-
const changelog = fs.readFileSync(CHANGELOG_PATH, 'utf8');
|
|
248
|
-
const existing = parseChangelogVersions(changelog);
|
|
249
|
-
|
|
250
|
-
const anchors = findAllBumpAnchors();
|
|
251
|
-
if (anchors.length === 0) {
|
|
252
|
-
console.error('✗ 未找到任何稳定版本 bump 记录(需在 git 仓库中执行)');
|
|
253
|
-
process.exit(1);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// 仅补齐 CHANGELOG 已有最新版本之后的缺失版本(避免从 1.0.x 一路回填)
|
|
257
|
-
const sortedListed = [...existing].sort(compareSemver);
|
|
258
|
-
const maxListed = sortedListed[sortedListed.length - 1];
|
|
259
|
-
const missing = maxListed
|
|
260
|
-
? anchors.filter((a) => compareSemver(a.version, maxListed) > 0)
|
|
261
|
-
: anchors;
|
|
262
|
-
|
|
263
|
-
if (missing.length === 0) {
|
|
264
|
-
console.log(`✓ CHANGELOG 已是最新,所有 >= ${maxListed} 的版本都已记录`);
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
console.log(`✓ 发现 ${missing.length} 个缺失版本(${maxListed} 之后): ${missing.map((m) => m.version).join(', ')}`);
|
|
269
|
-
console.log('');
|
|
270
|
-
|
|
271
|
-
const newEntries = [];
|
|
272
|
-
for (const m of missing) {
|
|
273
|
-
const idx = anchors.indexOf(m);
|
|
274
|
-
const prevHash = idx === 0 ? null : anchors[idx - 1].hash;
|
|
275
|
-
const commits = getCommitsForVersion(prevHash, m.hash);
|
|
276
|
-
|
|
277
|
-
const body = commits.length ? formatCommitsAsList(commits) : '无变更';
|
|
278
|
-
const date = getCommitDate(m.hash);
|
|
279
|
-
newEntries.push(buildEntry(m.version, date, body));
|
|
280
|
-
|
|
281
|
-
console.log(`--- [${m.version}] - ${date} ---`);
|
|
282
|
-
console.log(body);
|
|
283
|
-
console.log('');
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
if (dryRun) {
|
|
287
|
-
console.log('[dry-run] 未修改任何文件');
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
let updated = changelog;
|
|
292
|
-
const firstSectionIdx = updated.indexOf('\n## [');
|
|
293
|
-
const combined = newEntries.join('');
|
|
294
|
-
if (firstSectionIdx === -1) {
|
|
295
|
-
updated = updated.replace(/\s+$/, '') + '\n\n' + combined;
|
|
296
|
-
} else {
|
|
297
|
-
updated = updated.slice(0, firstSectionIdx + 1) + combined + updated.slice(firstSectionIdx + 1);
|
|
298
|
-
}
|
|
299
|
-
fs.writeFileSync(CHANGELOG_PATH, updated, 'utf8');
|
|
300
|
-
|
|
301
|
-
const versions = missing.map((m) => m.version).join(' / ');
|
|
302
|
-
console.log(`✓ CHANGELOG.md 已补充 ${missing.length} 个版本段落`);
|
|
303
|
-
console.log('');
|
|
304
|
-
console.log('待手动提交:');
|
|
305
|
-
console.log(' git add CHANGELOG.md');
|
|
306
|
-
console.log(` git commit -m "docs: 补充 ${versions} 的 CHANGELOG 条目"`);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// ---- 入口 ----------------------------------------------------------------
|
|
310
|
-
|
|
311
|
-
const args = process.argv.slice(2);
|
|
312
|
-
const dryRun = args.includes('--dry-run');
|
|
313
|
-
const backfill = args.includes('--backfill');
|
|
314
|
-
const force = args.includes('--force');
|
|
315
|
-
let level = 'patch';
|
|
316
|
-
if (args.includes('--minor')) level = 'minor';
|
|
317
|
-
else if (args.includes('--major')) level = 'major';
|
|
318
|
-
|
|
319
|
-
if (backfill) {
|
|
320
|
-
runBackfill(dryRun);
|
|
321
|
-
} else {
|
|
322
|
-
runRelease(level, dryRun, force);
|
|
1
|
+
/**
|
|
2
|
+
* 一键发版脚本:递增版本号(默认 patch)+ 自动从 git log 生成 CHANGELOG 段落
|
|
3
|
+
*
|
|
4
|
+
* 用法:
|
|
5
|
+
* node scripts/release.js # patch (默认)
|
|
6
|
+
* node scripts/release.js --dry-run # 只预览,不写文件
|
|
7
|
+
* node scripts/release.js --minor # 手动指定 minor 递增
|
|
8
|
+
* node scripts/release.js --major # 手动指定 major 递增
|
|
9
|
+
* node scripts/release.js --force # 跳过 CHANGELOG 滞后检查,直接发版
|
|
10
|
+
* node scripts/release.js --backfill # 回填 CHANGELOG 缺失的版本条目
|
|
11
|
+
* node scripts/release.js --backfill --dry-run
|
|
12
|
+
*
|
|
13
|
+
* 自动变更来源:
|
|
14
|
+
* 1. 优先:找到最近一次把 version 设为当前版本的 commit(上次发版点),取之后的 commits
|
|
15
|
+
* 2. 回退:用最近一次修改 CHANGELOG.md 的 commit 作为锚点
|
|
16
|
+
*
|
|
17
|
+
* 行为:
|
|
18
|
+
* - 按 semver 递增 package.json 的 version(默认 patch)
|
|
19
|
+
* - 检测 CHANGELOG 滞后于 package.json 时,自动报错并提示 --backfill
|
|
20
|
+
* - 在 CHANGELOG.md 顶部插入新版本段落,内容为提交列表(短 hash + subject)
|
|
21
|
+
* - backfill 模式:扫描 git 历史里所有版本号,补齐 CHANGELOG 缺失的条目
|
|
22
|
+
* - 不做任何 git 操作 —— commit / tag 由用户手动完成
|
|
23
|
+
*/
|
|
24
|
+
const fs = require('fs');
|
|
25
|
+
const path = require('path');
|
|
26
|
+
const { spawnSync } = require('child_process');
|
|
27
|
+
|
|
28
|
+
const ROOT = path.join(__dirname, '..');
|
|
29
|
+
const PKG_PATH = path.join(ROOT, 'package.json');
|
|
30
|
+
const CHANGELOG_PATH = path.join(ROOT, 'CHANGELOG.md');
|
|
31
|
+
const LINK_URL = 'https://172.16.222.31:8866/';
|
|
32
|
+
|
|
33
|
+
// ---- semver & 工具函数 ---------------------------------------------------
|
|
34
|
+
|
|
35
|
+
function bumpVersion(current, level) {
|
|
36
|
+
const [major, minor, patch] = current.split('.').map(Number);
|
|
37
|
+
if (level === 'major') return `${major + 1}.0.0`;
|
|
38
|
+
if (level === 'minor') return `${major}.${minor + 1}.0`;
|
|
39
|
+
return `${major}.${minor}.${patch + 1}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function formatDate(d = new Date()) {
|
|
43
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
44
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function buildEntry(version, date, body) {
|
|
48
|
+
return `## [${version}] - ${date}\n\n${body}\n\n[${version}]: ${LINK_URL}\n\n`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ---- git 查询 ------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
function git(args) {
|
|
54
|
+
const result = spawnSync('git', ['log', ...args], { encoding: 'utf8' });
|
|
55
|
+
return result.status === 0 ? result.stdout : '';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function gitRaw(args) {
|
|
59
|
+
return spawnSync('git', args, { encoding: 'utf8' });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 找到最近一次把 version 字段设为 currentVersion 的 commit
|
|
63
|
+
function findLastBumpCommit(currentVersion) {
|
|
64
|
+
const escaped = currentVersion.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
65
|
+
const out = git(['--oneline', '-G', `"version":\\s*"${escaped}"`, '--', 'package.json']);
|
|
66
|
+
const lines = out.split('\n').filter(Boolean);
|
|
67
|
+
if (!lines.length) return null;
|
|
68
|
+
const last = lines[lines.length - 1];
|
|
69
|
+
return last.split(/\s+/)[0];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 回退方案:最近一次修改 CHANGELOG.md 的 commit
|
|
73
|
+
function findLastChangelogCommit() {
|
|
74
|
+
const hash = git(['-1', '--pretty=format:%H', '--', 'CHANGELOG.md']);
|
|
75
|
+
return hash || null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function getCommitsSince(sinceHash, untilHash = 'HEAD') {
|
|
79
|
+
const out = git([`${sinceHash}..${untilHash}`, '--pretty=format:%h %s']);
|
|
80
|
+
return out ? out.split('\n').filter(Boolean) : [];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 找到 git 历史里所有稳定版本号变更的锚点(按时间正序,自动去重)
|
|
84
|
+
function findAllBumpAnchors() {
|
|
85
|
+
const out = git(['--reverse', '--pretty=format:%H', '--', 'package.json']);
|
|
86
|
+
const hashes = out.split('\n').filter(Boolean);
|
|
87
|
+
|
|
88
|
+
const firstSeen = new Map(); // version -> 首次出现的 hash(用于排序)
|
|
89
|
+
const lastSeen = new Map(); // version -> 最后出现的 hash(用作锚点)
|
|
90
|
+
|
|
91
|
+
for (const hash of hashes) {
|
|
92
|
+
const result = gitRaw(['show', `${hash}:package.json`]);
|
|
93
|
+
if (result.status !== 0) continue;
|
|
94
|
+
try {
|
|
95
|
+
const pkg = JSON.parse(result.stdout);
|
|
96
|
+
const v = pkg.version;
|
|
97
|
+
// 只考虑稳定版本 (1.2.3),跳过 0.0.0 / 1.2.3-beta.0 等
|
|
98
|
+
if (!v || !/^\d+\.\d+\.\d+$/.test(v)) continue;
|
|
99
|
+
if (!firstSeen.has(v)) firstSeen.set(v, hash);
|
|
100
|
+
lastSeen.set(v, hash); // 后出现会覆盖,保留最后一次(实际发版点)
|
|
101
|
+
} catch {}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// 按 Map 插入顺序返回(就是时间正序)
|
|
105
|
+
const anchors = [];
|
|
106
|
+
for (const [version] of firstSeen) {
|
|
107
|
+
anchors.push({ hash: lastSeen.get(version), version });
|
|
108
|
+
}
|
|
109
|
+
return anchors;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function compareSemver(a, b) {
|
|
113
|
+
const [aM, am, ap] = a.split('.').map(Number);
|
|
114
|
+
const [bM, bm, bp] = b.split('.').map(Number);
|
|
115
|
+
return (aM - bM) || (am - bm) || (ap - bp);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function getCommitDate(hash) {
|
|
119
|
+
const out = git(['-1', '--pretty=format:%ai', hash]);
|
|
120
|
+
const date = out.split(' ')[0];
|
|
121
|
+
return date || formatDate();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---- 文本处理 ------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
function parseChangelogVersions(content) {
|
|
127
|
+
const set = new Set();
|
|
128
|
+
const re = /##\s+\[(\d+\.\d+\.\d+)\]/g;
|
|
129
|
+
let m;
|
|
130
|
+
while ((m = re.exec(content)) !== null) set.add(m[1]);
|
|
131
|
+
return set;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function formatCommitsAsList(commits) {
|
|
135
|
+
if (!commits.length) return '无变更';
|
|
136
|
+
return commits
|
|
137
|
+
.map((line) => {
|
|
138
|
+
const idx = line.indexOf(' ');
|
|
139
|
+
if (idx === -1) return `- ${line}`;
|
|
140
|
+
return `- ${line.slice(idx + 1)} (\`${line.slice(0, idx)}\`)`;
|
|
141
|
+
})
|
|
142
|
+
.join('\n');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 取 prev 到 current 之间的 commits —— 中间严格区间 + 加上 currentHash 自身
|
|
146
|
+
// (发版 commit message 通常就是本次的变更说明)
|
|
147
|
+
function getCommitsForVersion(prevHash, currentHash) {
|
|
148
|
+
// 中间:严格在 prev 之后、current 之前。用 currentHash^ 排除 currentHash 本身
|
|
149
|
+
const middle = prevHash ? getCommitsSince(prevHash, `${currentHash}^`) : [];
|
|
150
|
+
|
|
151
|
+
// 把当前版本的发版 commit 自身放到最前
|
|
152
|
+
const subject = git(['-1', '--pretty=format:%s', currentHash]).trim();
|
|
153
|
+
const bumpLine = `${currentHash.slice(0, 7)} ${subject}`;
|
|
154
|
+
|
|
155
|
+
return [bumpLine, ...middle];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ---- 模式 1:正常发版 -----------------------------------------------------
|
|
159
|
+
|
|
160
|
+
function runRelease(level, dryRun, force) {
|
|
161
|
+
const pkg = JSON.parse(fs.readFileSync(PKG_PATH, 'utf8'));
|
|
162
|
+
const oldVersion = pkg.version;
|
|
163
|
+
const newVersion = bumpVersion(oldVersion, level);
|
|
164
|
+
|
|
165
|
+
// 检测 CHANGELOG 是否滞后于 package.json(典型场景:先发版后补文档)
|
|
166
|
+
if (!force) {
|
|
167
|
+
const changelog = fs.readFileSync(CHANGELOG_PATH, 'utf8');
|
|
168
|
+
const existing = parseChangelogVersions(changelog);
|
|
169
|
+
if (!existing.has(oldVersion)) {
|
|
170
|
+
const allAnchors = findAllBumpAnchors();
|
|
171
|
+
const sortedListed = [...existing].sort(compareSemver);
|
|
172
|
+
const maxListed = sortedListed[sortedListed.length - 1];
|
|
173
|
+
const missing = maxListed
|
|
174
|
+
? allAnchors.filter((a) => compareSemver(a.version, maxListed) > 0)
|
|
175
|
+
: allAnchors;
|
|
176
|
+
|
|
177
|
+
console.error(`✗ CHANGELOG 滞后于 package.json`);
|
|
178
|
+
console.error(`✗ package.json 当前版本: ${oldVersion}`);
|
|
179
|
+
console.error(`✗ CHANGELOG 最新版本: ${maxListed || '(无)'}`);
|
|
180
|
+
if (missing.length) {
|
|
181
|
+
console.error(`✗ 缺失版本: ${missing.map((m) => m.version).join(', ')}`);
|
|
182
|
+
}
|
|
183
|
+
console.error('');
|
|
184
|
+
console.error('请先跑补齐命令:');
|
|
185
|
+
console.error(' npm run release -- --backfill');
|
|
186
|
+
console.error('');
|
|
187
|
+
console.error('或跳过检查直接发版(忽略历史缺失):');
|
|
188
|
+
console.error(' npm run release -- --force');
|
|
189
|
+
process.exit(1);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const bumpAnchor = findLastBumpCommit(oldVersion);
|
|
194
|
+
const changelogAnchor = bumpAnchor ? null : findLastChangelogCommit();
|
|
195
|
+
const anchor = bumpAnchor || changelogAnchor;
|
|
196
|
+
const anchorSource = bumpAnchor ? `上次 bump ${oldVersion}` : '上次修改 CHANGELOG.md';
|
|
197
|
+
|
|
198
|
+
if (!anchor) {
|
|
199
|
+
console.error(`✗ 未找到 ${oldVersion} 之后的发版锚点`);
|
|
200
|
+
console.error(' 请确认在 git 仓库中执行');
|
|
201
|
+
process.exit(1);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const commits = getCommitsSince(anchor);
|
|
205
|
+
if (commits.length === 0) {
|
|
206
|
+
console.error(`✗ 自 ${anchorSource}(${anchor.slice(0, 7)})以来无新提交,无需发版`);
|
|
207
|
+
console.error(' 请先 commit 一些改动再发版');
|
|
208
|
+
process.exit(1);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const description = formatCommitsAsList(commits);
|
|
212
|
+
const entry = buildEntry(newVersion, formatDate(), description);
|
|
213
|
+
|
|
214
|
+
console.log(`✓ package.json: ${oldVersion} -> ${newVersion}`);
|
|
215
|
+
console.log(`✓ 锚点: ${anchorSource} (${anchor.slice(0, 7)})`);
|
|
216
|
+
console.log(`✓ 收集到 ${commits.length} 条提交`);
|
|
217
|
+
console.log('');
|
|
218
|
+
console.log('--- 即将写入 CHANGELOG.md 的段落 ---');
|
|
219
|
+
console.log(entry);
|
|
220
|
+
console.log('-----------------------------------');
|
|
221
|
+
|
|
222
|
+
if (dryRun) {
|
|
223
|
+
console.log('[dry-run] 未修改任何文件');
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
pkg.version = newVersion;
|
|
228
|
+
fs.writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 4) + '\n', 'utf8');
|
|
229
|
+
|
|
230
|
+
let changelog = fs.readFileSync(CHANGELOG_PATH, 'utf8');
|
|
231
|
+
const firstSectionIdx = changelog.indexOf('\n## [');
|
|
232
|
+
if (firstSectionIdx === -1) {
|
|
233
|
+
changelog = changelog.replace(/\s+$/, '') + '\n\n' + entry;
|
|
234
|
+
} else {
|
|
235
|
+
changelog = changelog.slice(0, firstSectionIdx + 1) + entry + changelog.slice(firstSectionIdx + 1);
|
|
236
|
+
}
|
|
237
|
+
fs.writeFileSync(CHANGELOG_PATH, changelog, 'utf8');
|
|
238
|
+
|
|
239
|
+
console.log('待手动提交:');
|
|
240
|
+
console.log(' git add package.json CHANGELOG.md');
|
|
241
|
+
console.log(` git commit -m "release: 发布 v${newVersion}"`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ---- 模式 2:回填历史缺失版本 ---------------------------------------------
|
|
245
|
+
|
|
246
|
+
function runBackfill(dryRun) {
|
|
247
|
+
const changelog = fs.readFileSync(CHANGELOG_PATH, 'utf8');
|
|
248
|
+
const existing = parseChangelogVersions(changelog);
|
|
249
|
+
|
|
250
|
+
const anchors = findAllBumpAnchors();
|
|
251
|
+
if (anchors.length === 0) {
|
|
252
|
+
console.error('✗ 未找到任何稳定版本 bump 记录(需在 git 仓库中执行)');
|
|
253
|
+
process.exit(1);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 仅补齐 CHANGELOG 已有最新版本之后的缺失版本(避免从 1.0.x 一路回填)
|
|
257
|
+
const sortedListed = [...existing].sort(compareSemver);
|
|
258
|
+
const maxListed = sortedListed[sortedListed.length - 1];
|
|
259
|
+
const missing = maxListed
|
|
260
|
+
? anchors.filter((a) => compareSemver(a.version, maxListed) > 0)
|
|
261
|
+
: anchors;
|
|
262
|
+
|
|
263
|
+
if (missing.length === 0) {
|
|
264
|
+
console.log(`✓ CHANGELOG 已是最新,所有 >= ${maxListed} 的版本都已记录`);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
console.log(`✓ 发现 ${missing.length} 个缺失版本(${maxListed} 之后): ${missing.map((m) => m.version).join(', ')}`);
|
|
269
|
+
console.log('');
|
|
270
|
+
|
|
271
|
+
const newEntries = [];
|
|
272
|
+
for (const m of missing) {
|
|
273
|
+
const idx = anchors.indexOf(m);
|
|
274
|
+
const prevHash = idx === 0 ? null : anchors[idx - 1].hash;
|
|
275
|
+
const commits = getCommitsForVersion(prevHash, m.hash);
|
|
276
|
+
|
|
277
|
+
const body = commits.length ? formatCommitsAsList(commits) : '无变更';
|
|
278
|
+
const date = getCommitDate(m.hash);
|
|
279
|
+
newEntries.push(buildEntry(m.version, date, body));
|
|
280
|
+
|
|
281
|
+
console.log(`--- [${m.version}] - ${date} ---`);
|
|
282
|
+
console.log(body);
|
|
283
|
+
console.log('');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (dryRun) {
|
|
287
|
+
console.log('[dry-run] 未修改任何文件');
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let updated = changelog;
|
|
292
|
+
const firstSectionIdx = updated.indexOf('\n## [');
|
|
293
|
+
const combined = newEntries.join('');
|
|
294
|
+
if (firstSectionIdx === -1) {
|
|
295
|
+
updated = updated.replace(/\s+$/, '') + '\n\n' + combined;
|
|
296
|
+
} else {
|
|
297
|
+
updated = updated.slice(0, firstSectionIdx + 1) + combined + updated.slice(firstSectionIdx + 1);
|
|
298
|
+
}
|
|
299
|
+
fs.writeFileSync(CHANGELOG_PATH, updated, 'utf8');
|
|
300
|
+
|
|
301
|
+
const versions = missing.map((m) => m.version).join(' / ');
|
|
302
|
+
console.log(`✓ CHANGELOG.md 已补充 ${missing.length} 个版本段落`);
|
|
303
|
+
console.log('');
|
|
304
|
+
console.log('待手动提交:');
|
|
305
|
+
console.log(' git add CHANGELOG.md');
|
|
306
|
+
console.log(` git commit -m "docs: 补充 ${versions} 的 CHANGELOG 条目"`);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// ---- 入口 ----------------------------------------------------------------
|
|
310
|
+
|
|
311
|
+
const args = process.argv.slice(2);
|
|
312
|
+
const dryRun = args.includes('--dry-run');
|
|
313
|
+
const backfill = args.includes('--backfill');
|
|
314
|
+
const force = args.includes('--force');
|
|
315
|
+
let level = 'patch';
|
|
316
|
+
if (args.includes('--minor')) level = 'minor';
|
|
317
|
+
else if (args.includes('--major')) level = 'major';
|
|
318
|
+
|
|
319
|
+
if (backfill) {
|
|
320
|
+
runBackfill(dryRun);
|
|
321
|
+
} else {
|
|
322
|
+
runRelease(level, dryRun, force);
|
|
323
323
|
}
|