-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
84 lines (66 loc) · 2.56 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
const $through = require('through2');
const $conventionalChangelogWriter = require('conventional-changelog-writer');
const $git = require('./lib/git');
const $constants = require('./lib/constants');
const $config = require('./lib/config');
const $utils = require('./lib/utils');
const $output = require('./lib/output');
const changelogConfig = $config.getConfig();
Promise.all([changelogConfig, $git.getAllCommits()])
.then(([ config, commits ]) => $utils.filterUnwantedCommits(config, commits))
.then(commits => {
return commits.reduce((promise, commit) => {
return promise
.then(() => $git.getCommitTag(commit.hash))
.then(tag => commit.tag = tag)
.then(() => commits);
}, Promise.resolve());
})
.then(commits => commits.filter(commit => !!commit.tag))
.then(commits => Promise.all([ commits, changelogConfig ]))
.then(([ commits, config ]) => {
// sort commits by git tag
const taggedCommits = $utils.groupCommitsByTag(commits);
// sort the tags by order
const orderedTags = $utils.sortTags(Object.keys(taggedCommits));
let previousTag;
const markdownPromise = orderedTags.reduce((promise, tag) => {
return promise.then((markdownSoFar) => new Promise(async (resolve) => {
// sort commits in tag
taggedCommits[tag].sort((a, b) => {
if (a.unixtime === b.unixtime) { return 0; }
return a.unixtime > b.unixtime ? -1 : 1;
});
const tagDate = await $git.getTagDate(tag);
const changelogWriter = $conventionalChangelogWriter({
version: tag,
repoUrl: config.repositoryUrl,
linkCompare: !!previousTag && tag !== $constants.UNRELEASED,
previousTag: previousTag,
currentTag: tag,
date: tagDate
}, config.writerOpts);
const upstream = $through.obj();
taggedCommits[tag].forEach(commit => upstream.write(commit));
upstream.end();
upstream
.pipe(changelogWriter)
.on('data', data => {
previousTag = tag;
resolve(data + markdownSoFar);
});
}));
}, Promise.resolve(''));
return Promise.all([ markdownPromise, config ]);
})
.then(([ markdown, config ]) => {
if (config.targetsMarkdown && config.targetsMarkdown.length) {
$output.asMarkdown(markdown, config.targetsMarkdown);
}
if (config.targetsHtml && config.targetsHtml.length) {
$output.asHtml(markdown, config.targetsHtml);
}
return markdown;
})
.then(result => console.log(result))
.catch(err => console.error(err));