Skip to content

Commit af06c37

Browse files
committed
feat: 增加picklog init来协助初始化 .picklogrc 文件
1 parent 1f436c7 commit af06c37

File tree

3 files changed

+112
-28
lines changed

3 files changed

+112
-28
lines changed

.picklogrc.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const origin = 'https://github.com/BearJ/picklog';
1+
const origin = 'https://BearJ@github.com/BearJ/picklog';
22
const comparePath = `${origin}/compare/`;
33
const commitPath = `${origin}/commit/`;
44

@@ -19,35 +19,35 @@ module.exports = {
1919
{
2020
name: 'Reverts',
2121
regExp: /^revert(\(.*?\))?:\s/i,
22-
}
22+
},
2323
],
24-
parse(picklog){
24+
parse(picklog) {
2525
// RegExp.prototype.toJSON = RegExp.prototype.toString; // JSON.stringify会调用正则表达式的toJSON
2626
// return JSON.stringify(picklog, null, 2);
2727

2828
let output = '';
2929

3030
picklog.forEach((log) => {
3131
let date = new Date(log.timestamp * 1000);
32-
date = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).substr(-2)}-${('0' + date.getDate()).substr(-2)}`;
32+
date = `${date.getFullYear()}-${(`0${date.getMonth() + 1}`).substr(-2)}-${(`0${date.getDate()}`).substr(-2)}`;
3333

34-
output += `### [${log.tag}](${comparePath}${log.previousTag || ''}...${log.tag}) (${date})\n\n`;
34+
let currentTag = log.tag || log.commits[0].h;
35+
let prevTag = log.previousTag || log.commits[log.commits.length - 1].h;
36+
output += `### [${currentTag}](${comparePath}${prevTag || ''}...${currentTag}) (${date})\n\n`;
3537

3638
log.results.forEach((result) => {
3739
output += `#### ${result.filter.name}\n`;
3840

3941
result.commits.forEach((commit) => {
40-
const regExp = result.filter.regExp;
42+
const { regExp } = result.filter;
4143
let subject = commit.s.match(regExp) || '';
4244

43-
if(subject[1]){
45+
if (subject[1]) {
4446
const type = subject[1].match(/\((.*?)\)/)[1];
45-
4647
subject = `**${type}:** ${commit.s.replace(regExp, '')}`;
47-
}else{
48+
} else {
4849
subject = commit.s.replace(regExp, '');
4950
}
50-
5151
output += `* ${subject}([${commit.h}](${commitPath}${commit.h}))\n`;
5252
});
5353

bin/.picklogrc.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const origin = '<%= GitURL %>';
2+
const comparePath = `${origin}/compare/`;
3+
const commitPath = `${origin}/commit/`;
4+
5+
module.exports = {
6+
filters: [
7+
{
8+
name: 'Features',
9+
regExp: /^feat(\(.*?\))?:\s/i,
10+
},
11+
{
12+
name: 'Bugfixes',
13+
regExp: /^fix(\(.*?\))?:\s/i,
14+
},
15+
{
16+
name: 'Performance Improvements',
17+
regExp: /^perf(\(.*?\))?:\s/i,
18+
},
19+
{
20+
name: 'Reverts',
21+
regExp: /^revert(\(.*?\))?:\s/i,
22+
},
23+
],
24+
parse(picklog) {
25+
// RegExp.prototype.toJSON = RegExp.prototype.toString; // JSON.stringify会调用正则表达式的toJSON
26+
// return JSON.stringify(picklog, null, 2);
27+
28+
let output = '';
29+
30+
picklog.forEach((log) => {
31+
let date = new Date(log.timestamp * 1000);
32+
date = `${date.getFullYear()}-${(`0${date.getMonth() + 1}`).substr(-2)}-${(`0${date.getDate()}`).substr(-2)}`;
33+
34+
let currentTag = log.tag || log.commits[0].h;
35+
let prevTag = log.previousTag || log.commits[log.commits.length - 1].h;
36+
output += `### [${currentTag}](${comparePath}${prevTag || ''}...${currentTag}) (${date})\n\n`;
37+
38+
log.results.forEach((result) => {
39+
output += `#### ${result.filter.name}\n`;
40+
41+
result.commits.forEach((commit) => {
42+
const { regExp } = result.filter;
43+
let subject = commit.s.match(regExp) || '';
44+
45+
if (subject[1]) {
46+
const type = subject[1].match(/\((.*?)\)/)[1];
47+
subject = `**${type}:** ${commit.s.replace(regExp, '')}`;
48+
} else {
49+
subject = commit.s.replace(regExp, '');
50+
}
51+
output += `* ${subject}([${commit.h}](${commitPath}${commit.h}))\n`;
52+
});
53+
54+
output += '\n';
55+
});
56+
57+
output += '\n\n';
58+
});
59+
60+
return output;
61+
},
62+
};

bin/picklog.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33
const fs = require('fs');
44
const { resolve } = require('path');
55
const yargs = require('yargs');
6+
const git = require('simple-git')();
67
const picklog = require('../index');
78

9+
function initPicklog() {
10+
try {
11+
fs.accessSync(resolve('.picklogrc.js'));
12+
console.log('File .picklogrc is in your project, so it\'s no need to init.');
13+
} catch (err) {
14+
git.raw(['config', '--get', 'remote.origin.url'], (err, result) => {
15+
if (err) throw err;
16+
17+
const gitUrl = result.replace(/\n/g, '').replace(/\.git$/, '');
18+
fs.writeFileSync('.picklogrc.js', fs.readFileSync(resolve(__dirname, '.picklogrc.js'), 'utf-8').replace(/<%= GitURL %>/, gitUrl));
19+
console.log('Init Success!');
20+
});
21+
}
22+
}
23+
824
const { argv } = yargs
925
.alias('h', 'help')
1026
.alias('v', 'version')
1127

28+
.command('init', 'Generator a .picklogrc file.')
1229
.option('g', {
1330
alias: 'gitLogArgs',
1431
describe: 'Pass the arg to "git log". Splited by comma. e.g: picklog -g v2.0.0',
@@ -27,23 +44,28 @@ const { argv } = yargs
2744
describe: 'Overwrite stdout to this file.',
2845
});
2946

30-
if (argv.overwrite && typeof argv.overwrite !== 'string') {
31-
console.warn('Please type a file name. eg: picklog -o changelog.md');
32-
return;
33-
}
47+
if (argv._.indexOf('init') > -1) {
48+
initPicklog();
49+
} else {
50+
if (argv.overwrite && typeof argv.overwrite !== 'string') {
51+
console.warn('Please type a file name. eg: picklog -o changelog.md');
52+
return;
53+
}
3454

35-
if (argv.write && typeof argv.write !== 'string') {
36-
console.warn('Please type a file name. eg: picklog -w changelog.md');
37-
return;
38-
}
55+
if (argv.write && typeof argv.write !== 'string') {
56+
console.warn('Please type a file name. eg: picklog -w changelog.md');
57+
return;
58+
}
3959

40-
picklog(argv)
41-
.then((commits) => {
42-
if (argv.overwrite) {
43-
fs.writeFileSync(resolve(argv.overwrite), commits, 'utf8');
44-
} else if (argv.write) {
45-
fs.writeFileSync(resolve(argv.write), commits + fs.readFileSync(resolve(argv.write), 'utf8'));
46-
} else {
47-
console.log(commits);
48-
}
49-
});
60+
61+
picklog(argv)
62+
.then((commits) => {
63+
if (argv.overwrite) {
64+
fs.writeFileSync(resolve(argv.overwrite), commits, 'utf8');
65+
} else if (argv.write) {
66+
fs.writeFileSync(resolve(argv.write), commits + fs.readFileSync(resolve(argv.write), 'utf8'));
67+
} else {
68+
console.log(commits);
69+
}
70+
});
71+
}

0 commit comments

Comments
 (0)