Skip to content

Commit 6f418a7

Browse files
committed
✨ feat: support gitmoji unicode
1 parent cc0c0a4 commit 6f418a7

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed

packages/plugin/.changelogrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ const base = require('../../.changelogrc');
22

33
module.exports = {
44
...base,
5-
displayScopes: ['plugin'],
65
};

packages/plugin/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@
3636
"license": "MIT",
3737
"peerDependencies": {
3838
"commitlint": "^11.0.0"
39+
},
40+
"dependencies": {
41+
"emoji-unicode": "^2.0.1"
3942
}
4043
}

packages/plugin/src/gitmojiCode.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ if (!existsSync(filePath)) {
2727
}
2828
// eslint-disable-next-line import/no-dynamic-require
2929
const { gitmojis } = require(filePath);
30-
const gitmojiCodes: string[] = gitmojis.map((gitmoji) => gitmoji.code);
30+
export const gitmojiCodes: string[] = gitmojis.map((gitmoji) => gitmoji.code);
3131

32-
export default gitmojiCodes;
32+
export const gitmojiUnicode: string[] = gitmojis.map((gitmoji) =>
33+
gitmoji.entity.replace('&#', '').replace(';', '').replace('x', ''),
34+
);

packages/plugin/src/rule.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
import type { Rule } from '@commitlint/types';
2-
import gitmojiCodes from './gitmojiCode';
2+
import { gitmojiCodes, gitmojiUnicode } from './gitmojiCode';
3+
import emojiUnicode from 'emoji-unicode';
34

45
const emoji: Rule = (parsed) => {
56
const { raw } = parsed;
67

7-
const regex = /^(:\w*:)\s.*/gm; // regex test url: https://regex101.com/r/fSdOvB/1
8+
// code regex test url: https://regex101.com/r/fSdOvB/1
9+
const regex = /^(:\w*:)\s.*/gm;
10+
// unicode regex test url: https://regex101.com/r/OTMgWL/2
11+
const unicodeRegex = /(\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f\ude80-\udeff]|[\u2600-\u2B55])\s.*/gm;
12+
813
const result = regex.exec(raw);
14+
const unicodeResult = unicodeRegex.exec(raw);
915

1016
let pass;
1117
let errorMsg = 'passed';
1218

13-
// if don't has emoji
14-
if (!result) {
15-
pass = false;
16-
errorMsg =
17-
'Your commit should start with gitmoji code,please check the emoji code on https://gitmoji.dev/.';
18-
} else {
19+
// if gitmoji code is valid
20+
if (result) {
1921
const emojiCode = result[1];
20-
2122
pass = gitmojiCodes.includes(emojiCode);
2223
if (!pass) {
2324
errorMsg = `${emojiCode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
2425
}
26+
} else if (unicodeResult) {
27+
const unicode = unicodeResult[1];
28+
29+
const hasRaw = gitmojiUnicode.includes(emojiUnicode.raw(unicode));
30+
const hasHex = gitmojiUnicode.includes(emojiUnicode(unicode));
31+
pass = hasHex || hasRaw;
32+
33+
if (!pass) {
34+
errorMsg = `${unicode} is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.`;
35+
}
36+
} else {
37+
// if don't has gitmoji code or emoji unicode
38+
pass = false;
39+
errorMsg =
40+
'Your commit should start with gitmoji code,please check the emoji code on https://gitmoji.dev/.';
2541
}
2642

2743
return [pass, errorMsg];

packages/plugin/test/gitmojiCode.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ describe('gitmojiCodes work well', () => {
1010

1111
const gitmojiCodes = await import('../src/gitmojiCode');
1212

13-
expect(gitmojiCodes.default).toBeInstanceOf(Array);
13+
expect(gitmojiCodes.gitmojiCodes).toBeInstanceOf(Array);
1414
});
1515

1616
it('will download gitmoji json and write to file without gitmoji json', async () => {
1717
// 如果存在 gitmoji 直接删除
1818
if (existsSync(targetPath)) unlinkSync(targetPath);
1919
const gitmojiCodes = await import('../src/gitmojiCode');
20-
expect(gitmojiCodes.default).toBeInstanceOf(Array);
20+
expect(gitmojiCodes.gitmojiCodes).toBeInstanceOf(Array);
2121
});
2222
});
2323

@@ -36,7 +36,7 @@ describe('gitmojiCodes throw Error', () => {
3636
if (existsSync(targetPath)) unlinkSync(targetPath);
3737
try {
3838
const gitmojiCodes = await import('../src/gitmojiCode');
39-
expect(gitmojiCodes.default).toBeInstanceOf(Array);
39+
expect(gitmojiCodes.gitmojiCodes).toBeInstanceOf(Array);
4040
} catch (e) {
4141
expect(e).toEqual(
4242
Error(

packages/plugin/test/rule.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,34 @@ describe('commit start with gitmoji code', () => {
2121
]);
2222
});
2323

24-
it('should pass when return right commit message code', () => {
24+
it('should failed if commit start with unrecognized gitmoji unicode', () => {
25+
const value = emojiRule({ raw: '🤔 chore(scope): test' } as Commit, when);
26+
expect(value).toEqual([
27+
false,
28+
'Your commit should start with gitmoji code,please check the emoji code on https://gitmoji.dev/.',
29+
]);
30+
});
31+
32+
it('should failed if commit start with wrong gitmoji unicode', () => {
33+
const value = emojiRule({ raw: '🌙 chore(scope): test' } as Commit, when);
34+
expect(value).toEqual([
35+
false,
36+
'🌙 is not in the correct gitmoji list, please check the emoji code on https://gitmoji.dev/.',
37+
]);
38+
});
39+
40+
it('should pass when return correct commit message code', () => {
2541
const value = emojiRule({ raw: ':tada: test' } as Commit, when);
2642
expect(value).toEqual([true, 'passed']);
2743
});
44+
45+
it('should pass when with correct gitmoji unicode', () => {
46+
const value = emojiRule({ raw: '🎉 test' } as Commit, when);
47+
expect(value).toEqual([true, 'passed']);
48+
});
49+
50+
it('should pass when with correct test gitmoji unicode', () => {
51+
const value = emojiRule({ raw: '✅ test' } as Commit, when);
52+
expect(value).toEqual([true, 'passed']);
53+
});
2854
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5757,6 +5757,11 @@ emoji-regex@^9.0.0:
57575757
resolved "https://registry.npm.taobao.org/emoji-regex/download/emoji-regex-9.2.0.tgz?cache=0&sync_timestamp=1603212288390&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Femoji-regex%2Fdownload%2Femoji-regex-9.2.0.tgz#a26da8e832b16a9753309f25e35e3c0efb9a066a"
57585758
integrity sha1-om2o6DKxapdTMJ8l4148DvuaBmo=
57595759

5760+
emoji-unicode@^2.0.1:
5761+
version "2.0.1"
5762+
resolved "https://registry.npm.taobao.org/emoji-unicode/download/emoji-unicode-2.0.1.tgz#37b05f0cd20254fd366d1df4442b5c613a3a021a"
5763+
integrity sha1-N7BfDNICVP02bR30RCtcYTo6Aho=
5764+
57605765
emojis-list@^3.0.0:
57615766
version "3.0.0"
57625767
resolved "https://registry.npm.taobao.org/emojis-list/download/emojis-list-3.0.0.tgz#5570662046ad29e2e916e71aae260abdff4f6a78"

0 commit comments

Comments
 (0)