Skip to content

Commit 5225dfb

Browse files
committed
✨ feat(template): add custom templates
1 parent e1bdfd8 commit 5225dfb

File tree

8 files changed

+112
-48
lines changed

8 files changed

+112
-48
lines changed

CHANGELOG.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ All notable changes to the "git-int-commit-plugin" extension will be documented
44

55
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
66

7-
## [1.0.6]
8-
- 🎈 perf(commitTemplate): 增加配置增加icon标签,可以自定义图标显示的位置
9-
- 🐞 fix(commitContent): 解决在没有body或者footer的情况下会多出换行的bug
10-
117
## [1.0.5]
128
- ✨ feat(commitTemplate): 插件新增提交模板配置
13-
149
## [1.0.4]
1510
- 🌈Style:[change icons](https://github.com/RedJue/git-commit-plugin/commit/611ecfb6c2cbf14436141056cc87da4530117c66)
1611
- 🐞Fix:issue [#36](https://github.com/RedJue/git-commit-plugin/issues/36)

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ A detailed explanation can be found in this [document](https://docs.google.com/d
8787
"customTypeName"
8888
] or [
8989
{
90+
// If there are duplicate labels, rewrite the config,otherwise add As a new configuration addition
9091
"label": "customTypeName",
91-
"detail": "customTypeDetail"
92+
"detail": "customTypeDetail",
93+
"icon":"customIcon"
9294
}
9395
]
9496
```
@@ -110,7 +112,9 @@ A detailed explanation can be found in this [document](https://docs.google.com/d
110112
},
111113
{
112114
"templateName": "git-cz",
113-
"templateContent": "<type>(<scope>):<space><icon><space><subject><enter><body><enter><footer>"
115+
"templateContent": "<type>(<scope>):<space><icon><space><subject><enter><body><enter><footer>",
116+
// Set as default commit template
117+
"default":true
114118
}
115119
]
116120
```

git-commit-plugin-1.0.1.vsix

-515 KB
Binary file not shown.

src/config/commit-detail.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface CommitDetailType extends QuickPickItem {
99
}
1010
//最大的 subject 限制字数 Max subject words
1111
export const MaxSubjectWords = workspace.getConfiguration('GitCommitPlugin').get<boolean>('MaxSubjectWords') || 20;
12+
1213
export const CommitDetailType: Array<CommitDetailType> = [
1314
{
1415
label: '<Scope>',
@@ -43,12 +44,18 @@ export const CommitDetailType: Array<CommitDetailType> = [
4344
key: 'complete',
4445
detail: '完成 commit 的编写'
4546
},
47+
{
48+
label: 'Select template for commit',
49+
key: 'template',
50+
detail: '选择提交使用的模板'
51+
},
4652
{
4753
label: 'Back',
4854
key: 'back',
4955
detail: '返回 commit type 选择页'
5056
}
5157
];
58+
5259
export const CommitDetailQuickPickOptions: QuickPickOptions = {
5360
matchOnDescription: true,
5461
matchOnDetail: true,

src/config/commit-type.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { workspace, QuickPickItem } from 'vscode';
44
* @description git commit 提交类型
55
*/
66
export interface CommitType extends QuickPickItem {
7-
icon: string;
7+
icon?: string;
88
}
9-
9+
//是否展现 Emoji图标 show Emoji or not
10+
const isShowEmoji = workspace.getConfiguration('GitCommitPlugin').get<boolean>('ShowEmoji');
1011
//新增的自定义commit type add custom Commit Type
1112
const CustomCommitType = workspace.getConfiguration('GitCommitPlugin').get<boolean>('CustomCommitType');
13+
1214
let CommitType: Array<CommitType> = [
1315
{
1416
label: 'feat',
@@ -61,37 +63,52 @@ let CommitType: Array<CommitType> = [
6163
icon:'🐳'
6264
},
6365
{
64-
label: 'revert',
66+
label: 'revert',
6567
detail: '回滚到上一个版本',
66-
icon:''
68+
icon:''
6769
}
6870
];
6971

70-
// if (!isShowEmoji) {
71-
// CommitType = CommitType.map((commitType) => {
72-
// const labelArr = [...commitType.label];
73-
// labelArr.shift();
74-
// commitType.label = labelArr.join('').trim();
75-
// return commitType;
76-
// });
77-
// }
7872
if (Array.isArray(CustomCommitType)) {
79-
const costom_commit_type: Array<CommitType> = CustomCommitType.map((item, index) => {
73+
CustomCommitType.forEach((item) => {
8074
let label = '',icon='',detail = '';
8175
if (typeof item === 'string') {
8276
label = detail = item;
8377
}
84-
if (Object.prototype.toString.call(item) === '[object Object]') {
85-
Reflect.has(item, 'label') && (label = item.label);
86-
Reflect.has(item, 'detail') && (detail = item.detail);
87-
Reflect.has(item, 'icon') && (icon = item.icon);
88-
}
89-
return {
78+
const resultType = {
9079
label,
91-
detail,
92-
icon
80+
icon,
81+
detail
9382
};
83+
if (Object.prototype.toString.call(item) === '[object Object]') {
84+
if(Reflect.has(item, 'label')){resultType.label = item.label;}else{Reflect.deleteProperty(resultType,'label');};
85+
if(Reflect.has(item, 'detail')){resultType.detail = item.detail;}else{Reflect.deleteProperty(resultType,'detail');};
86+
if(Reflect.has(item, 'icon')){resultType.icon = item.icon;}else{Reflect.deleteProperty(resultType,'icon');};
87+
}
88+
const target = CommitType.find((type)=>type.label === item.label);
89+
90+
if(target !== undefined){
91+
Object.assign(target,resultType);
92+
}else{
93+
CommitType.unshift(resultType);
94+
}
9495
});
95-
CommitType = costom_commit_type.concat(CommitType);
9696
}
97+
if (!isShowEmoji) {
98+
CommitType = CommitType.map((commitType) => {
99+
commitType.icon = '';
100+
return commitType;
101+
});
102+
}
103+
104+
if(isShowEmoji){
105+
CommitType.forEach((item)=>{
106+
// If there is an icon display
107+
if(typeof item.icon === 'string' && item.icon.length > 0){
108+
item.label = `${item.icon} ${item.label}`;
109+
}
110+
});
111+
}
112+
113+
97114
export default CommitType;

src/config/default-temp.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const Angular = {
2+
'templateName': 'Angular',
3+
'templateContent': '<icon><space><type>(<scope>):<space><subject><enter><body><enter><footer>'
4+
};
5+
6+
export const GitCz = {
7+
'templateName': 'git-cz',
8+
'templateContent': '<type>(<scope>):<space><icon><space><subject><enter><body><enter><footer>'
9+
};

src/config/template-type.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { workspace, QuickPickItem } from 'vscode';
2+
import * as defaultTemp from './default-temp';
23

34
/**
45
* @description git commit template 类型
@@ -8,18 +9,28 @@ export interface CommitTemplateType extends QuickPickItem {
89
detail: string;
910
templateName: string;
1011
templateContent: string;
12+
/** default template */
13+
default?:boolean;
1114
}
1215
let CommitTemplate: Array<CommitTemplateType> = new Array<CommitTemplateType>();
1316
const configTemplate = workspace.getConfiguration('GitCommitPlugin').get<Array<CommitTemplateType>>('Templates');
1417
if (Array.isArray(configTemplate)) {
1518
CommitTemplate = configTemplate.map((item, index) => {
16-
let templateName = '', label = '', detail = '', templateContent = '';
1719
return {
1820
label: item.templateName,
1921
detail: item.templateContent,
2022
templateName: item.templateName,
2123
templateContent:item.templateContent,
22-
}
24+
default:item.default || false
25+
};
2326
});
27+
2428
}
29+
const def:Array<CommitTemplateType> = Object.values(defaultTemp).map((item)=>({
30+
label: item.templateName,
31+
detail: item.templateContent,
32+
templateName: item.templateName,
33+
templateContent:item.templateContent,
34+
}));
35+
CommitTemplate = CommitTemplate.concat(def);
2536
export default CommitTemplate;

src/extension.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
3-
import { workspace } from 'vscode';
43
import * as vscode from 'vscode';
54
import { GitExtension } from './types/git';
65
import CommitType from './config/commit-type';
76
import { CommitDetailType, CommitDetailQuickPickOptions, MaxSubjectWords } from './config/commit-detail';
87
import CommitInputType from './config/commit-input';
98
import CommitTemplate from './config/template-type';
9+
import {Angular} from './config/default-temp';
1010
export interface GitMessage {
1111
[index: string]: string;
12+
templateName:string;
13+
templateContent:string;
14+
icon:string;
1215
type: string;
1316
scope: string;
1417
subject: string;
1518
body: string;
1619
footer: string;
1720
}
18-
//是否展现 Emoji图标 show Emoji or not
19-
const isShowEmoji = workspace.getConfiguration('GitCommitPlugin').get<boolean>('ShowEmoji');
21+
2022
// this method is called when your extension is activated
2123
// your extension is activated the very first time the command is executed
2224
export function activate(context: vscode.ExtensionContext) {
@@ -47,8 +49,8 @@ export function activate(context: vscode.ExtensionContext) {
4749
}
4850
//组合信息 Portfolio information
4951
function messageCombine(config: GitMessage) {
50-
let result = config.templateContent;
51-
result = isShowEmoji ? result.replace(/<icon>/g, config.icon) : result.replace(/<icon>/g, '');
52+
let result = config.templateContent || Angular.templateContent;
53+
result = config.icon ? result.replace(/<icon>/g, config.icon) : result.replace(/<icon>/g, '');
5254
result = config.type !=='' ? result.replace(/<type>/g, config.type) : result.replace(/<type>/g, '');
5355
result = config.scope !=='' ? result.replace(/<scope>/g, config.scope) : result.replace(/<scope>/g, '');
5456
result = config.subject !=='' ? result.replace(/<subject>/g, config.subject) : result.replace(/<subject>/g, '');
@@ -57,9 +59,6 @@ export function activate(context: vscode.ExtensionContext) {
5759
result = result.replace(/<enter>/g, '\n\n');
5860
result = result.replace(/<space>/g, ' ');
5961
return result.trim();
60-
// return [`${config.type}${config.scope ? '(' + config.scope + ')' : ''}: ${config.subject} -- ${config.templateName}`, config.body, config.footer]
61-
// .filter((item) => item)
62-
// .join('\n\n');
6362
}
6463
const gitExtension = getGitExtension();
6564
if (!gitExtension?.enabled) {
@@ -69,7 +68,6 @@ export function activate(context: vscode.ExtensionContext) {
6968

7069
//获取当前的 git仓库实例 Get git repo instance
7170
let repo: any = gitExtension.getAPI(1).repositories[0];
72-
console.log(repo, 'repo');
7371

7472
//输入提交详情 Input message detail
7573
const inputMessageDetail = (_key: string | number) => {
@@ -94,9 +92,26 @@ export function activate(context: vscode.ExtensionContext) {
9492
recursiveInputMessage(startMessageInput);
9593
});
9694
};
95+
//是否存在模板 If has template
96+
const existTemplete = () =>{
97+
return Array.isArray(CommitTemplate) && CommitTemplate.length > 0;
98+
};
99+
//完成输入 Complete input message
100+
const completeInputMessage = (select?:boolean) =>{
101+
vscode.commands.executeCommand('workbench.view.scm');
102+
if(existTemplete() && !select){
103+
const defaultTemp = CommitTemplate.find((item)=>item.default);
104+
if(defaultTemp !== undefined){
105+
message_config.templateName = defaultTemp.templateName;
106+
message_config.templateContent = defaultTemp.templateContent;
107+
}
108+
}
109+
repo.inputBox.value = messageCombine(message_config);
110+
};
97111
// 递归输入信息 Recursive input message
98112
const recursiveInputMessage = (startMessageInput?: () => void) => {
99113
CommitDetailQuickPickOptions.placeHolder = '搜索提交描述(Search Commit Describe)';
114+
100115
const _CommitDetailType: Array<CommitDetailType> = JSON.parse(JSON.stringify(CommitDetailType));
101116
_CommitDetailType.map((item: any) => {
102117
if (item.isEdit) {
@@ -109,8 +124,7 @@ export function activate(context: vscode.ExtensionContext) {
109124
if (label !== '') {
110125
const _key = select?.key || 'body';
111126
if (_key === 'complete') {
112-
vscode.commands.executeCommand('workbench.view.scm');
113-
repo.inputBox.value = messageCombine(message_config);
127+
completeInputMessage();
114128
clearMessage();
115129
return false;
116130
}
@@ -119,6 +133,10 @@ export function activate(context: vscode.ExtensionContext) {
119133
clearMessage();
120134
return false;
121135
}
136+
if(_key === 'template'){
137+
SelectTemplate();
138+
return false;
139+
}
122140
inputMessageDetail(_key);
123141
} else {
124142
clearMessage();
@@ -127,10 +145,13 @@ export function activate(context: vscode.ExtensionContext) {
127145
};
128146
//开始输入 Start input
129147
const startMessageInput = () => {
130-
CommitDetailQuickPickOptions.placeHolder = '搜索 Git 提交类型(Search Commit Type)';
148+
CommitDetailQuickPickOptions.placeHolder = '搜索 Git 提交类型(Search Commit Type)';
131149
vscode.window.showQuickPick(CommitType, CommitDetailQuickPickOptions).then((select) => {
132-
const label = (select && select.label) || '';
150+
let label = (select && select.label) || '';
133151
const icon = (select && select.icon) || '';
152+
if(typeof icon === 'string' && icon.length > 0){
153+
label = label.split(' ')[1];
154+
}
134155
message_config.type = label;
135156
message_config.icon = icon;
136157
if (label !== '') {
@@ -139,7 +160,7 @@ export function activate(context: vscode.ExtensionContext) {
139160
});
140161
};
141162
//选择commit 提交的模板
142-
const SelectTemplate = () => {
163+
const SelectTemplate = () => {
143164
CommitDetailQuickPickOptions.placeHolder = '选择提交使用的模板';
144165
vscode.window
145166
.showQuickPick(CommitTemplate, CommitDetailQuickPickOptions).then((select) => {
@@ -148,8 +169,8 @@ export function activate(context: vscode.ExtensionContext) {
148169
message_config.templateName = templateName;
149170
message_config.templateContent = templateContent;
150171
if (templateName !== '') {
151-
startMessageInput();
152-
// recursiveInputMessage(startMessageInput);
172+
completeInputMessage(true);
173+
clearMessage();
153174
}
154175
});
155176
};
@@ -161,7 +182,7 @@ export function activate(context: vscode.ExtensionContext) {
161182
return repo.rootUri.path === uri._rootUri.path;
162183
});
163184
}
164-
SelectTemplate();
185+
startMessageInput();
165186
});
166187
context.subscriptions.push(disposable);
167188
}

0 commit comments

Comments
 (0)