-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (107 loc) · 2.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
var cp = require('child_process');
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var async = require('neo-async');
var cchangelog = require('conventional-changelog');
var semver = require('semver');
var COMMIT_PATTERN = /^(\w*)(\(([\w\$\.\-\* ]*)\))?\: (.*)$/;
function getUrl() {
return require(path.join(__dirname, '..', 'package.json')).homepage;
}
function defaultCallback(err) {
if (err) {
console.error(err);
}
}
module.exports = function(url, callback) {
if (arguments.length === 0) {
callback = defaultCallback;
url = null;
}
if (arguments.length === 1) {
if (typeof url === 'function') {
callback = url;
url = null;
} else {
callback = defaultCallback;
}
}
if (!url)
url = getUrl();
var dates = {};
async.waterfall([
function(next) {
cp.exec('git for-each-ref --format "%(refname:short) %09 %(taggerdate:short)" refs/tags', function(err, stdout) {
if (err)
return next(err);
var list = [];
String(stdout).split('\n').forEach(function(v) {
var _v = v.split('\t');
if (!_v || !_v[0]) {
return;
}
var ver = _v[0].trim();
list.push(ver);
dates[ver] = _v[1].trim();
});
list.sort(semver.compare);
var _list = _.clone(list);
_list.shift();
list.pop();
next(null, _.zip(list, _list));
});
},
function(pair, next) {
function matcher(subject) {
var match = subject.match(COMMIT_PATTERN);
if (match && match[1] && match[4]) {
return match;
}
var _s = subject.split(' ');
if (_s[0] === 'Merge' && _s[1] === 'pull' && _s[2] === 'request') {
return;
}
if (_s.length === 1 && semver.valid(_s[0])) {
return;
}
var list = [
subject,
_s[0].replace(/\./g, '').toLowerCase(),
null,
null,
subject
];
list.index = 0;
list.input = subject;
return list;
}
var str = '';
async.eachSeries(pair, function(p, done) {
cchangelog({
repository: url,
version: p[1],
file: '',
from: p[0],
to: p[1],
grep: '',
matcher: matcher,
date: dates[p[1]]
}, function(err, result) {
if (err)
return done(err);
str = result + str;
done();
});
}, function(err) {
if (err)
return next(err);
next(null, str);
});
},
function(contents, next) {
fs.writeFile('CHANGELOG.md', contents, { encoding: 'utf8' }, next);
},
], callback);
};