forked from hsrobmln/conventional-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (49 loc) · 1.57 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
var fs = require('fs');
var git = require('./lib/git');
var writer = require('./lib/writer');
var extend = require('lodash.assign');
module.exports = generate;
function generate(options, done) {
options = extend({
version: null,
to: 'HEAD',
file: 'CHANGELOG.md',
subtitle: '',
log: console.log.bind(console),
grep: '^\-'
}, options || {});
if (!options.version) {
return done('No version specified');
}
git.latestTag(function(err, tag) {
if (err || tag === undefined) return done('Failed to read git tags.\n'+err);
getChangelogCommits(tag);
});
function getChangelogCommits(latestTag) {
options.from = options.from || latestTag;
options.to = options.to || 'HEAD';
options.log('Generating changelog from %s to %s...', options.from, options.to);
git.getCommits({
from: options.from,
to: options.to,
grep: options.grep
}, function(err, commits) {
if (err) return done('Failed to read git log.\n'+err);
writeLog(commits);
});
}
function writeLog(commits) {
options.log('Parsed %d commits.', commits.length);
writer.writeLog(commits, options, function(err, changelog) {
if (err) return done('Failed to write changelog.\n'+err);
if (options.file && fs.existsSync(options.file)) {
fs.readFile(options.file, {encoding:'UTF-8'}, function(err, contents) {
if (err) return done('Failed to read ' + options.file + '.\n'+err);
done(null, changelog + '\n' + String(contents));
});
} else {
done(null, changelog);
}
});
}
}