forked from conventional-changelog/conventional-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (78 loc) · 2.42 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
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),
notes: '',
}, options || {});
if (!options.version) {
return done('No version specified');
}
readNotes(options.notes, options.file).then(function (notes) {
options.notes = notes;
git.getTags(function(err, tags) {
if (err || !tags) {
return done('Failed to read git tags.\n'+err);
}
writeChangelog(tags);
});
});
// consider anything before first anchor as release notes
function readNotes(notes, file) {
return new Promise(function (resolve, reject) {
if (notes || !file || !fs.existsSync(file)) {
resolve(notes);
return;
}
fs.readFile(file, {encoding:'UTF-8'}, function(err, contents) {
if (err) {
return done('Failed to read ' + file + '.\n'+err);
}
var idx = contents.indexOf('<a name="');
resolve(idx ? contents.substr(0, idx) : '');
});
});
}
function writeChangelog(tags) {
options.exclude = options.exclude || tags;
options.to = options.to || 'HEAD';
options.log('Generating changelog for %s...', options.version);
git.getLog({
exclude: options.exclude,
to: options.to,
}, function(err, gitLog) {
if (err) {
return done('Failed to read git log.\n'+err);
}
writeGitLog(gitLog);
});
}
function writeGitLog(gitLog) {
options.log('Parsed %d commits.', gitLog.commits.length);
options.log('Parsed %d contributors.', gitLog.contributors.length);
writer.writeLog(gitLog, 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);
}
// clear out release notes (already captured in log)
var idx = contents.indexOf('<a name="');
done(null, changelog + '\n' + String(contents.substring(idx)));
});
} else {
done(null, changelog);
}
});
}
}