Skip to content

Commit 1bb7b96

Browse files
committed
feat: 增加 tagFilter 来选取自己所需的 tag
1 parent 7263516 commit 1bb7b96

File tree

15 files changed

+856
-32
lines changed

15 files changed

+856
-32
lines changed

.picklogrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ module.exports = {
5959

6060
return output;
6161
},
62+
tagFilter: /^v\d+\.\d+\.\d+$/,
6263
};

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ module.exports = {
8282
parse(commits){
8383
return JSON.stringify(commits, null, 2);
8484
},
85+
tagFilter: /^v\d+\.\d+\.\d+$/, // Optional
8586
};
8687
```
8788

88-
| 参数 | 必填 | 说明 | 类型 |
89+
| 参数 (Args) | 必填 (Required) | 说明 (Introduction) | 类型 (Type) |
8990
| ------ | ------ | ------ | ------ |
9091
| filters | Yes | 规定了选取log的正则,你也可以在output里获得它。( `filters` use regexp filter logs, you can alse get this in output. ) | Array |
9192
| parse | Yes | 你可以对你过滤的logs进行解析的函数。参数`commits`的结构可看[这里](./test/getCommits/output.json)。( `parse` is the function that you can parse your output with the logs you filter. [Here](./test/getCommits/output.json) is the`commits` example. ) | Function |
93+
| tagFilter | False | 规定了选取tag的正则。( `tagFilter` use regexp filter tag. ) | RegExp |
9294

9395

9496
### 我想要Markdown ( I want Markdown )
@@ -127,7 +129,8 @@ module.exports = {
127129
});
128130

129131
return output;
130-
}
132+
},
133+
tagFilter: /^v\d+\.\d+\.\d+$/, // Optional
131134
};
132135
```
133136

@@ -179,6 +182,7 @@ module.exports = {
179182
});
180183

181184
return output;
182-
}
185+
},
186+
tagFilter: /^v\d+\.\d+\.\d+$/, // Optional
183187
};
184188
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Pickup the logs that you filter, so you can generation changelog from it.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "jest",
7+
"test": "jest --detectOpenHandles",
88
"lint": "eslint **/*.js",
99
"changelog": "node ./bin/picklog.js -l -w CHANGELOG.md",
1010
"preversion": "npm run lint && npm test",

src/getCommits.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module.exports = function getCommits(args, setting) {
6262
git.raw(['log', ...gitLogArgs, PrettyFormats.arg], (err, result) => {
6363
if (err) throw err;
6464

65-
let tagCommitObjList = splitCommitByTag(PrettyFormats.parse(result));
65+
let tagCommitObjList = splitCommitByTag(PrettyFormats.parse(result), setting);
6666

6767
if (args.latest) {
6868
tagCommitObjList = tagCommitObjList.splice(0, 1);

src/lib/defaultSetting.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ module.exports = {
2323
RegExp.prototype.toJSON = RegExp.prototype.toString; // JSON.stringify会调用正则表达式的toJSON
2424
return JSON.stringify(logs, null, 2);
2525
},
26+
tagFilter: /^v\d+\.\d+\.\d+$/, // Optional
2627
};

src/splitCommitByTag.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ function getCommitObj(commit) {
1717
};
1818
}
1919

20-
module.exports = function splitCommitByTag(commits) {
20+
module.exports = function splitCommitByTag(commits, setting) {
2121
if (!commits || commits.length < 1) return [];
2222

2323
const result = [];
2424
let commitObject = getCommitObj(commits.splice(0, 1)[0]);
2525

2626
commits.forEach((commit) => {
27-
const tagName = getTagName(commit.d);
27+
let tagName = getTagName(commit.d);
28+
if (setting.tagFilter) {
29+
tagName = setting.tagFilter.test(tagName) ? tagName : '';
30+
}
2831

2932
if (tagName) {
3033
// 该commit有tag

test/.picklogrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ module.exports = {
2323
RegExp.prototype.toJSON = RegExp.prototype.toString; // JSON.stringify会调用正则表达式的toJSON
2424
return JSON.stringify(logs, null, 2);
2525
},
26+
tagFilter: /^v\d+\.\d+\.\d+$/,
2627
};

test/getCommits/getCommits.test.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-disable no-undef */
22

3-
const { readFileSync } = require('fs');
4-
const { resolve } = require('path');
3+
const fs = require('fs');
4+
const path = require('path');
55

66
const getCommits = require('../../src/getCommits.js');
77
const setting = require('../.picklogrc');
88

9-
const output = readFileSync(resolve(__dirname, 'output.json'), 'utf-8');
10-
const outputWithLatest = readFileSync(resolve(__dirname, 'outputWithLatest.json'), 'utf-8');
9+
const output = fs.readFileSync(path.resolve(__dirname, 'output.json'), 'utf-8');
10+
const outputWithLatest = fs.readFileSync(path.resolve(__dirname, 'outputWithLatest.json'), 'utf-8');
1111

12-
test('Test getCommits.js', () => {
12+
test('Test getCommits.js', () => new Promise((resolve) => {
1313
getCommits({
1414
gitLogArgs: 'v1.2.3',
1515
}, setting)
@@ -30,10 +30,11 @@ test('Test getCommits.js', () => {
3030
/* eslint-disable-next-line no-extend-native */
3131
RegExp.prototype.toJSON = RegExp.prototype.toString; // JSON.stringify会调用正则表达式的toJSON
3232
expect(JSON.stringify(commits, null, 2)).toBe(output);
33+
resolve();
3334
});
34-
});
35+
}));
3536

36-
test('Test getCommits.js with arg "latest"', () => {
37+
test('Test getCommits.js with arg "latest"', () => new Promise((resolve) => {
3738
getCommits({
3839
latest: true,
3940
gitLogArgs: 'v1.2.3',
@@ -55,5 +56,6 @@ test('Test getCommits.js with arg "latest"', () => {
5556
/* eslint-disable-next-line no-extend-native */
5657
RegExp.prototype.toJSON = RegExp.prototype.toString; // JSON.stringify会调用正则表达式的toJSON
5758
expect(JSON.stringify(commits, null, 2)).toBe(outputWithLatest);
59+
resolve();
5860
});
59-
});
61+
}));

test/getFilterCommitsWithSetting/getFilterCommitsWithSetting.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable no-undef */
22

3-
const { readFileSync } = require('fs');
4-
const { resolve } = require('path');
3+
const fs = require('fs');
4+
const path = require('path');
55

66
const getFilterCommitsWithSetting = require('../../src/getFilterCommitsWithSetting.js');
77
const input = require('./input');
88
const setting = require('../.picklogrc');
99

10-
const output = readFileSync(resolve(__dirname, 'output.json'), 'utf-8');
10+
const output = fs.readFileSync(path.resolve(__dirname, 'output.json'), 'utf-8');
1111

1212
test('Test getFilterCommitsWithSetting', () => {
1313
/* eslint-disable-next-line no-extend-native */

test/index/index.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/* eslint-disable no-undef */
22

3-
const { readFileSync } = require('fs');
4-
const { resolve } = require('path');
3+
const fs = require('fs');
4+
const path = require('path');
55

66
const picklog = require('../../index');
77

8-
const output = readFileSync(resolve(__dirname, 'output.md'), 'utf-8');
8+
const output = fs.readFileSync(path.resolve(__dirname, 'output.md'), 'utf-8');
99

10-
test('Test index.js', () => {
10+
test('Test index.js', () => new Promise((resolve) => {
1111
picklog({
1212
gitLogArgs: 'v1.2.3',
1313
}).then((text) => {
14+
fs.writeFileSync('output.md', text);
1415
expect(text).toBe(output);
16+
resolve();
1517
});
16-
});
18+
}));

test/splitCommitByTag/input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module.exports = [
153153
cr: '2 days ago',
154154
ct: '1550065429',
155155
ci: '2019-02-13 21:43:49 +0800',
156-
d: '',
156+
d: ' (tag: bugfix)',
157157
e: '',
158158
s: 'fix: 修复bin的入口',
159159
f: 'fix-bin',
@@ -261,7 +261,7 @@ module.exports = [
261261
cr: '2 days ago',
262262
ct: '1550062380',
263263
ci: '2019-02-13 20:53:00 +0800',
264-
d: '',
264+
d: ' (tag: changelog)',
265265
e: '',
266266
s: 'changelog',
267267
f: 'changelog',

test/splitCommitByTag/output.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
"cr": "2 days ago",
167167
"ct": "1550065429",
168168
"ci": "2019-02-13 21:43:49 +0800",
169-
"d": "",
169+
"d": " (tag: bugfix)",
170170
"e": "",
171171
"s": "fix: 修复bin的入口",
172172
"f": "fix-bin",
@@ -282,7 +282,7 @@
282282
"cr": "2 days ago",
283283
"ct": "1550062380",
284284
"ci": "2019-02-13 20:53:00 +0800",
285-
"d": "",
285+
"d": " (tag: changelog)",
286286
"e": "",
287287
"s": "changelog",
288288
"f": "changelog",
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
/* eslint-disable no-undef */
1+
/* eslint-disable no-undef,max-len */
22

3-
const { readFileSync } = require('fs');
4-
const { resolve } = require('path');
3+
const fs = require('fs');
4+
const path = require('path');
55

66
const splitCommitByTag = require('../../src/splitCommitByTag.js');
77
const commits = require('./input');
88

9-
const output = readFileSync(resolve(__dirname, 'output.json'), 'utf-8');
9+
const setting = require('../.picklogrc');
10+
const settingWithoutTagFilter = require('./without-tag-filter/.picklogrc');
11+
12+
const output = fs.readFileSync(path.resolve(__dirname, 'output.json'), 'utf-8');
13+
const outputWithoutTagFilter = fs.readFileSync(path.resolve(__dirname, './without-tag-filter/output.json'), 'utf-8');
1014

1115
test('Test splitCommitByTag', () => {
12-
expect(JSON.stringify(splitCommitByTag(commits), null, 2)).toBe(output);
16+
expect(JSON.stringify(splitCommitByTag(commits, setting), null, 2)).toBe(output);
17+
});
18+
19+
test('Test splitCommitByTag without tagFilter', () => {
20+
expect(JSON.stringify(splitCommitByTag(commits, settingWithoutTagFilter), null, 2)).toBe(outputWithoutTagFilter);
1321
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
filters: [
3+
{
4+
name: 'Features',
5+
regExp: /^feat(\(.*?\))?:\s/i,
6+
},
7+
{
8+
name: 'Bugfixes',
9+
regExp: /^fix(\(.*?\))?:\s/i,
10+
},
11+
{
12+
name: 'Performance Improvements',
13+
regExp: /^perf(\(.*?\))?:\s/i,
14+
},
15+
{
16+
name: 'Reverts',
17+
regExp: /^revert(\(.*?\))?:\s/i,
18+
},
19+
],
20+
21+
parse(logs) {
22+
/* eslint-disable-next-line no-extend-native */
23+
RegExp.prototype.toJSON = RegExp.prototype.toString; // JSON.stringify会调用正则表达式的toJSON
24+
return JSON.stringify(logs, null, 2);
25+
},
26+
};

0 commit comments

Comments
 (0)