Skip to content

Commit dd7d9e0

Browse files
committed
feat(cz-git): ✨ support commit add emoji
BREAKING CHANGE : Need open option `useEmoji` link #1
1 parent c8ff83e commit dd7d9e0

5 files changed

Lines changed: 65 additions & 16 deletions

File tree

‎.commitlintrc.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
{ value: "chore", name: "chore: Other changes that don't modify src or test files", emoji: ":hammer:" },
3333
{ value: "revert", name: "revert: Reverts a previous commit", emoji: ":rewind:" }
3434
],
35-
useEmoji: false,
35+
useEmoji: true,
3636
scopes: [{ value: "cz-git", name: "cz-git: core control" }, { name: "guide" } , 'config'],
3737
allowCustomScopes: true,
3838
allowBreakingChanges: ['feat', 'fix'],

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ A better customizable and git support commitizen adapter.
4545

4646
## Feature
4747

48-
- Just to be a **lazy man** !!! Select if you can.
48+
- Just to be a **lazy man** !!! Search and Select if you can.
4949
- **Customize**, But follow conventional commit format like [Angular convention](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#-git-commit-guidelines).
50-
- Reduce errors and better support for issue.
50+
- Reduce errors | Better support for issue | ✅ Support emoji.
5151

5252
## Usage
5353

‎src/until.ts‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export const getProcessSubject = (text: string) => {
9898
return text.replace(/(^[\s]+|[\s\.]+$)/g, "") ?? "";
9999
};
100100

101+
const getEmojiStrLength = (options: CommitizenGitOptions, type?: string): number => {
102+
const item = options.types?.find((i) => i.value === type);
103+
// space
104+
return item?.emoji ? item.emoji.length + 1 : 0;
105+
};
106+
101107
export const getMaxSubjectLength = (
102108
type: Answers["type"],
103109
scope: Answers["scope"],
@@ -110,7 +116,7 @@ export const getMaxSubjectLength = (
110116
2 -
111117
// `()`
112118
(scope ? scope.length + 2 : 0) -
113-
(options.useEmoji ? 2 : 0)
119+
(options.useEmoji ? getEmojiStrLength(options, type) : 0)
114120
);
115121
};
116122

@@ -129,23 +135,24 @@ export const handleScopes = (scopes: ScopesType): Option[] => {
129135
});
130136
};
131137

132-
const addType = (type: string, options: CommitizenGitOptions, color?: boolean) => {
133-
type = color ? `\u001B[33m${type}\u001B[0m` : type;
134-
if (options.useEmoji) {
135-
const item = options.types?.find((i) => i.value === type);
136-
return item?.emoji ? `${item.emoji} ${type}` : type;
137-
} else {
138-
return type;
139-
}
140-
};
138+
const addType = (type: string, color?: boolean) => (color ? `\u001B[33m${type}\u001B[0m` : type);
141139

142140
const addScope = (scope?: string, color?: boolean) => {
143-
const separator = ": ";
141+
const separator = ":";
144142
if (!scope) return separator;
145143
scope = color ? `\u001B[35m${scope}\u001B[0m` : scope;
146144
return `(${scope.trim()})${separator}`;
147145
};
148146

147+
const addEmoji = (type: string, options: CommitizenGitOptions): string => {
148+
if (options.useEmoji && type !== "") {
149+
const item = options.types?.find((i) => i.value === type);
150+
return item?.emoji ? ` ${item.emoji} ` : " ";
151+
} else {
152+
return " ";
153+
}
154+
};
155+
149156
const addSubject = (subject?: string, color?: boolean) => {
150157
if (!subject) return "";
151158
subject = color ? `\u001B[36m${subject}\u001B[0m` : subject;
@@ -172,8 +179,9 @@ export const buildCommit = (answers: Answers, options: CommitizenGitOptions, col
172179
width: 100
173180
};
174181
const head =
175-
addType(answers.type ?? "", options, color) +
182+
addType(answers.type ?? "", color) +
176183
addScope(answers.scope, color) +
184+
addEmoji(answers.type ?? "", options) +
177185
addSubject(answers.subject, color);
178186
const body = wrap(answers.body ?? "", wrapOptions);
179187
const breaking = wrap(answers.breaking ?? "", wrapOptions);

‎test/loader.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildCommit } from "../src/until";
1+
import { buildCommit } from "../lib/until";
22

33
/**
44
* @description: buildCommit Test
@@ -13,4 +13,20 @@ describe("buildCommit()", () => {
1313
const options = {};
1414
expect(buildCommit(answers, options)).toEqual("feat(app): add a new feature");
1515
});
16+
17+
test("subject with emoji options standard separator", () => {
18+
const options = {
19+
types: [{ value: "feat", name: "feat: A new feature", emoji: ":sparkles:" }],
20+
useEmoji: true
21+
};
22+
expect(buildCommit(answers, options)).toEqual("feat(app): :sparkles: add a new feature");
23+
});
24+
25+
test("subject with emoji options and lost type standard separator", () => {
26+
const options = {
27+
types: [{ value: "feat", name: "feat: A new feature" }],
28+
useEmoji: true
29+
};
30+
expect(buildCommit(answers, options)).toEqual("feat(app): add a new feature");
31+
});
1632
});

‎test/until.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { getMaxLength } from "../lib/until";
2+
import { Rule } from "../lib/until";
3+
4+
/**
5+
* @description: getMaxLength Test
6+
*/
7+
describe("getMaxLength()", () => {
8+
test("use commitlint rule get subject max length", () => {
9+
const clConfig = {
10+
rules: {
11+
"subject-max-length": [2, "always", 80]
12+
}
13+
};
14+
expect(getMaxLength(clConfig.rules["subject-max-length"] as unknown as Rule)).toEqual(80);
15+
});
16+
17+
test("underfined commitlint subject rule", () => {
18+
const clConfig = {
19+
rules: {
20+
"subject-max-length": undefined
21+
}
22+
};
23+
expect(getMaxLength(clConfig.rules["subject-max-length"] as unknown as Rule)).toEqual(Infinity);
24+
});
25+
});

0 commit comments

Comments
 (0)