@zjex/git-workflow 0.4.0 → 0.4.2

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.
@@ -0,0 +1,607 @@
1
+ # API 文档
2
+
3
+ 本文档介绍 git-workflow 的内部 API 和核心模块。
4
+
5
+ ## 核心模块
6
+
7
+ ### config.ts - 配置管理
8
+
9
+ 配置管理模块负责读取、合并和验证配置文件。
10
+
11
+ #### 类型定义
12
+
13
+ ```typescript
14
+ interface Config {
15
+ branch?: BranchConfig;
16
+ commit?: CommitConfig;
17
+ tag?: TagConfig;
18
+ }
19
+
20
+ interface BranchConfig {
21
+ types?: string[];
22
+ baseBranch?: string;
23
+ includeDate?: boolean;
24
+ includeStoryId?: boolean;
25
+ storyIdRequired?: boolean;
26
+ }
27
+
28
+ interface CommitConfig {
29
+ useAI?: boolean;
30
+ format?: "conventional" | "simple";
31
+ useGitmoji?: boolean;
32
+ ai?: AIConfig;
33
+ }
34
+
35
+ interface AIConfig {
36
+ provider?: "github" | "openai" | "claude" | "ollama";
37
+ model?: string;
38
+ apiKey?: string;
39
+ baseURL?: string;
40
+ customPrompt?: string;
41
+ }
42
+
43
+ interface TagConfig {
44
+ prefix?: string;
45
+ prerelease?: string[];
46
+ }
47
+ ```
48
+
49
+ #### 主要函数
50
+
51
+ ##### readConfig()
52
+
53
+ 读取并合并配置文件。
54
+
55
+ ```typescript
56
+ function readConfig(): Config;
57
+ ```
58
+
59
+ **返回值:** 合并后的配置对象
60
+
61
+ **配置优先级:** 项目配置 > 全局配置 > 默认配置
62
+
63
+ **示例:**
64
+
65
+ ```typescript
66
+ import { readConfig } from "./config.js";
67
+
68
+ const config = readConfig();
69
+ console.log(config.branch.baseBranch); // "main"
70
+ ```
71
+
72
+ ##### getGlobalConfigPath()
73
+
74
+ 获取全局配置文件路径。
75
+
76
+ ```typescript
77
+ function getGlobalConfigPath(): string;
78
+ ```
79
+
80
+ **返回值:** `~/.gwrc.json` 的绝对路径
81
+
82
+ ##### getProjectConfigPath()
83
+
84
+ 获取项目配置文件路径。
85
+
86
+ ```typescript
87
+ function getProjectConfigPath(): string;
88
+ ```
89
+
90
+ **返回值:** 当前项目根目录下的 `.gwrc.json` 路径
91
+
92
+ ### ai-service.ts - AI 服务
93
+
94
+ AI 服务模块负责调用各种 AI 提供商生成提交消息。
95
+
96
+ #### 主要函数
97
+
98
+ ##### generateCommitMessage()
99
+
100
+ 使用 AI 生成提交消息。
101
+
102
+ ```typescript
103
+ async function generateCommitMessage(
104
+ diff: string,
105
+ config: AIConfig
106
+ ): Promise<string>;
107
+ ```
108
+
109
+ **参数:**
110
+
111
+ - `diff` - Git diff 输出
112
+ - `config` - AI 配置
113
+
114
+ **返回值:** 生成的提交消息
115
+
116
+ **抛出:** 如果 API 调用失败
117
+
118
+ **示例:**
119
+
120
+ ```typescript
121
+ import { generateCommitMessage } from "./ai-service.js";
122
+
123
+ const diff = execOutput("git diff --cached");
124
+ const config = {
125
+ provider: "github",
126
+ model: "gpt-4o",
127
+ apiKey: "your-api-key",
128
+ };
129
+
130
+ const message = await generateCommitMessage(diff, config);
131
+ console.log(message);
132
+ // feat(auth): 添加用户登录功能
133
+ ```
134
+
135
+ ##### cleanAIResponse()
136
+
137
+ 清理 AI 响应,移除多余的格式。
138
+
139
+ ```typescript
140
+ function cleanAIResponse(response: string): string;
141
+ ```
142
+
143
+ **参数:**
144
+
145
+ - `response` - AI 原始响应
146
+
147
+ **返回值:** 清理后的提交消息
148
+
149
+ ### utils.ts - 工具函数
150
+
151
+ 工具模块提供常用的辅助函数。
152
+
153
+ #### 主要函数
154
+
155
+ ##### execOutput()
156
+
157
+ 执行 shell 命令并返回输出。
158
+
159
+ ```typescript
160
+ function execOutput(command: string): string;
161
+ ```
162
+
163
+ **参数:**
164
+
165
+ - `command` - 要执行的命令
166
+
167
+ **返回值:** 命令输出(去除首尾空白)
168
+
169
+ **示例:**
170
+
171
+ ```typescript
172
+ import { execOutput } from "./utils.js";
173
+
174
+ const branch = execOutput("git branch --show-current");
175
+ console.log(branch); // "main"
176
+
177
+ const status = execOutput("git status --porcelain");
178
+ console.log(status); // "M file.js\nA new.js"
179
+ ```
180
+
181
+ ##### colors
182
+
183
+ 颜色工具对象,用于终端输出着色。
184
+
185
+ ```typescript
186
+ const colors: {
187
+ yellow: (text: string) => string;
188
+ green: (text: string) => string;
189
+ red: (text: string) => string;
190
+ blue: (text: string) => string;
191
+ dim: (text: string) => string;
192
+ bold: (text: string) => string;
193
+ // ... 更多颜色
194
+ };
195
+ ```
196
+
197
+ **示例:**
198
+
199
+ ```typescript
200
+ import { colors } from "./utils.js";
201
+
202
+ console.log(colors.green("成功"));
203
+ console.log(colors.red("错误"));
204
+ console.log(colors.yellow("警告"));
205
+ ```
206
+
207
+ ##### divider()
208
+
209
+ 打印分隔线。
210
+
211
+ ```typescript
212
+ function divider(): void;
213
+ ```
214
+
215
+ **示例:**
216
+
217
+ ```typescript
218
+ import { divider } from "./utils.js";
219
+
220
+ console.log("标题");
221
+ divider();
222
+ console.log("内容");
223
+ ```
224
+
225
+ ##### theme
226
+
227
+ Inquirer 主题配置。
228
+
229
+ ```typescript
230
+ const theme: Theme;
231
+ ```
232
+
233
+ ## 命令模块
234
+
235
+ ### branch.ts - 分支管理
236
+
237
+ #### 主要函数
238
+
239
+ ##### createBranch()
240
+
241
+ 创建新分支。
242
+
243
+ ```typescript
244
+ async function createBranch(type: BranchType): Promise<void>;
245
+ ```
246
+
247
+ **参数:**
248
+
249
+ - `type` - 分支类型("feature" | "hotfix")
250
+
251
+ **功能:**
252
+
253
+ 1. 获取 Story ID(可选)
254
+ 2. 获取分支描述
255
+ 3. 生成规范的分支名
256
+ 4. 创建并切换到新分支
257
+
258
+ ##### deleteBranch()
259
+
260
+ 删除分支。
261
+
262
+ ```typescript
263
+ async function deleteBranch(): Promise<void>;
264
+ ```
265
+
266
+ **功能:**
267
+
268
+ 1. 列出最近使用的分支
269
+ 2. 用户选择要删除的分支
270
+ 3. 确认删除
271
+ 4. 删除本地和远程分支
272
+
273
+ ##### getBranchName()
274
+
275
+ 生成规范的分支名。
276
+
277
+ ```typescript
278
+ async function getBranchName(type: BranchType): Promise<string | null>;
279
+ ```
280
+
281
+ **参数:**
282
+
283
+ - `type` - 分支类型
284
+
285
+ **返回值:** 格式化的分支名,或 null(用户取消)
286
+
287
+ **格式:** `<type>/<date>-<storyId>-<description>`
288
+
289
+ ### commit.ts - 提交管理
290
+
291
+ #### 主要函数
292
+
293
+ ##### commit()
294
+
295
+ 交互式提交代码。
296
+
297
+ ```typescript
298
+ async function commit(): Promise<void>;
299
+ ```
300
+
301
+ **功能:**
302
+
303
+ 1. 检查是否有变更
304
+ 2. 选择提交方式(AI 或手动)
305
+ 3. 生成或输入提交消息
306
+ 4. 确认并提交
307
+
308
+ ##### formatCommitMessage()
309
+
310
+ 格式化提交消息。
311
+
312
+ ```typescript
313
+ function formatCommitMessage(
314
+ type: string,
315
+ scope: string,
316
+ subject: string,
317
+ body?: string
318
+ ): string;
319
+ ```
320
+
321
+ **参数:**
322
+
323
+ - `type` - 提交类型
324
+ - `scope` - 影响范围
325
+ - `subject` - 简短描述
326
+ - `body` - 详细描述(可选)
327
+
328
+ **返回值:** 格式化的提交消息
329
+
330
+ ### tag.ts - 标签管理
331
+
332
+ #### 主要函数
333
+
334
+ ##### createTag()
335
+
336
+ 创建版本标签。
337
+
338
+ ```typescript
339
+ async function createTag(): Promise<void>;
340
+ ```
341
+
342
+ **功能:**
343
+
344
+ 1. 获取当前版本
345
+ 2. 选择版本类型(patch/minor/major)
346
+ 3. 计算新版本号
347
+ 4. 创建并推送 tag
348
+
349
+ ##### cleanInvalidTags()
350
+
351
+ 清理无效标签。
352
+
353
+ ```typescript
354
+ async function cleanInvalidTags(): Promise<void>;
355
+ ```
356
+
357
+ **功能:**
358
+
359
+ 1. 检测无效的 tag(如 vnull)
360
+ 2. 显示详情
361
+ 3. 确认删除
362
+ 4. 删除本地和远程 tag
363
+
364
+ ##### parseVersion()
365
+
366
+ 解析版本号。
367
+
368
+ ```typescript
369
+ function parseVersion(version: string): {
370
+ major: number;
371
+ minor: number;
372
+ patch: number;
373
+ prerelease?: string;
374
+ };
375
+ ```
376
+
377
+ **参数:**
378
+
379
+ - `version` - 版本字符串(如 "v1.2.3")
380
+
381
+ **返回值:** 版本对象
382
+
383
+ ### stash.ts - Stash 管理
384
+
385
+ #### 主要函数
386
+
387
+ ##### stash()
388
+
389
+ 交互式 stash 管理。
390
+
391
+ ```typescript
392
+ async function stash(): Promise<void>;
393
+ ```
394
+
395
+ **功能:**
396
+
397
+ 1. 列出所有 stash
398
+ 2. 选择操作(应用/弹出/删除/查看差异)
399
+ 3. 执行相应操作
400
+
401
+ ##### parseStashList()
402
+
403
+ 解析 stash 列表。
404
+
405
+ ```typescript
406
+ function parseStashList(): StashEntry[];
407
+ ```
408
+
409
+ **返回值:** Stash 条目数组
410
+
411
+ **类型:**
412
+
413
+ ```typescript
414
+ interface StashEntry {
415
+ index: number;
416
+ branch: string;
417
+ message: string;
418
+ date: string;
419
+ files: string[];
420
+ }
421
+ ```
422
+
423
+ ### log.ts - 日志查看
424
+
425
+ #### 主要函数
426
+
427
+ ##### log()
428
+
429
+ 显示 Git 日志。
430
+
431
+ ```typescript
432
+ async function log(options?: LogOptions): Promise<void>;
433
+ ```
434
+
435
+ **参数:**
436
+
437
+ ```typescript
438
+ interface LogOptions {
439
+ author?: string;
440
+ since?: string;
441
+ until?: string;
442
+ grep?: string;
443
+ limit?: number;
444
+ all?: boolean;
445
+ interactive?: boolean;
446
+ }
447
+ ```
448
+
449
+ **功能:**
450
+
451
+ 1. 获取 Git 日志
452
+ 2. 格式化为时间线样式
453
+ 3. 使用 less 分页显示
454
+
455
+ ### release.ts - 发布管理
456
+
457
+ #### 主要函数
458
+
459
+ ##### release()
460
+
461
+ 创建发布版本。
462
+
463
+ ```typescript
464
+ async function release(): Promise<void>;
465
+ ```
466
+
467
+ **功能:**
468
+
469
+ 1. 选择版本类型
470
+ 2. 更新版本号
471
+ 3. 生成 CHANGELOG
472
+ 4. 创建 tag
473
+ 5. 推送到远程
474
+
475
+ ### update.ts - 更新检查
476
+
477
+ #### 主要函数
478
+
479
+ ##### checkUpdate()
480
+
481
+ 检查是否有新版本。
482
+
483
+ ```typescript
484
+ async function checkUpdate(): Promise<void>;
485
+ ```
486
+
487
+ **功能:**
488
+
489
+ 1. 获取最新版本信息
490
+ 2. 比较当前版本
491
+ 3. 显示更新提示
492
+
493
+ ## 使用示例
494
+
495
+ ### 示例 1: 自定义分支创建
496
+
497
+ ```typescript
498
+ import { getBranchName } from "./commands/branch.js";
499
+ import { execSync } from "child_process";
500
+
501
+ async function createCustomBranch() {
502
+ const branchName = await getBranchName("feature");
503
+ if (branchName) {
504
+ execSync(`git checkout -b "${branchName}"`, { stdio: "inherit" });
505
+ console.log(`分支创建成功: ${branchName}`);
506
+ }
507
+ }
508
+ ```
509
+
510
+ ### 示例 2: 批量处理提交
511
+
512
+ ```typescript
513
+ import { generateCommitMessage } from "./ai-service.js";
514
+ import { execOutput } from "./utils.js";
515
+
516
+ async function batchCommit(files: string[]) {
517
+ for (const file of files) {
518
+ // 暂存文件
519
+ execSync(`git add ${file}`);
520
+
521
+ // 获取差异
522
+ const diff = execOutput("git diff --cached");
523
+
524
+ // 生成提交消息
525
+ const message = await generateCommitMessage(diff, {
526
+ provider: "github",
527
+ apiKey: process.env.GITHUB_TOKEN,
528
+ });
529
+
530
+ // 提交
531
+ execSync(`git commit -m "${message}"`);
532
+ }
533
+ }
534
+ ```
535
+
536
+ ### 示例 3: 自动化发布
537
+
538
+ ```typescript
539
+ import { createTag } from "./commands/tag.js";
540
+ import { execSync } from "child_process";
541
+
542
+ async function autoRelease() {
543
+ // 运行测试
544
+ execSync("npm test", { stdio: "inherit" });
545
+
546
+ // 构建
547
+ execSync("npm run build", { stdio: "inherit" });
548
+
549
+ // 创建 tag
550
+ await createTag();
551
+
552
+ // 发布
553
+ execSync("npm publish", { stdio: "inherit" });
554
+ }
555
+ ```
556
+
557
+ ## 扩展开发
558
+
559
+ ### 添加新命令
560
+
561
+ 1. 在 `src/commands/` 创建新文件
562
+ 2. 实现命令逻辑
563
+ 3. 在 `src/index.ts` 注册命令
564
+
565
+ ```typescript
566
+ // src/commands/custom.ts
567
+ export async function customCommand(): Promise<void> {
568
+ console.log("自定义命令");
569
+ }
570
+
571
+ // src/index.ts
572
+ import { customCommand } from "./commands/custom.js";
573
+
574
+ program
575
+ .command("custom")
576
+ .alias("c")
577
+ .description("自定义命令")
578
+ .action(customCommand);
579
+ ```
580
+
581
+ ### 添加新的 AI 提供商
582
+
583
+ 在 `src/ai-service.ts` 中添加:
584
+
585
+ ```typescript
586
+ async function generateCommitMessage(
587
+ diff: string,
588
+ config: AIConfig
589
+ ): Promise<string> {
590
+ switch (config.provider) {
591
+ case "custom":
592
+ return await callCustomAI(diff, config);
593
+ // ... 其他提供商
594
+ }
595
+ }
596
+
597
+ async function callCustomAI(diff: string, config: AIConfig): Promise<string> {
598
+ // 实现自定义 AI 调用
599
+ }
600
+ ```
601
+
602
+ ## 相关资源
603
+
604
+ - [开发指南](./development.md)
605
+ - [测试指南](./testing.md)
606
+ - [代码文档](https://github.com/iamzjt-front-end/git-workflow/blob/main/CODE_DOCUMENTATION.md)
607
+ - [TypeScript 文档](https://www.typescriptlang.org/docs/)