UNPKG

1.44 kBJavaScriptView Raw
1'use strict';
2
3var Bluebird = require('bluebird');
4var CP = Bluebird.promisifyAll(require('child_process'));
5
6var SEPARATOR = '===END===';
7var COMMIT_PATTERN = /^(\w*)(\(([\w\$\.\-\* ]*)\))?\: (.*)$/;
8var FORMAT = '%H%n%s%n%b%n' + SEPARATOR;
9
10/**
11 * Get all commits from the last tag (or the first commit if no tags).
12 * @returns {Promise<Array<Object>>} array of parsed commit objects
13 */
14exports.getCommits = function () {
15 return CP.execAsync('git describe --tags --abbrev=0')
16 .catch(function () {
17 return '';
18 })
19 .then(function (tag) {
20 tag = tag.toString().trim();
21 var revisions = tag ? tag + '..HEAD' : '';
22
23 return CP.execAsync('git log -E --format=' + FORMAT + ' ' + revisions);
24 })
25 .catch(function () {
26 throw new Error('no commits found');
27 })
28 .then(function (commits) {
29 return commits.split('\n' + SEPARATOR + '\n');
30 })
31 .map(function (raw) {
32 if (!raw) {
33 return null;
34 }
35
36 var lines = raw.split('\n');
37 var commit = {};
38
39 commit.hash = lines.shift();
40 commit.subject = lines.shift();
41 commit.body = lines.join('\n');
42
43 var parsed = commit.subject.match(COMMIT_PATTERN);
44
45 if (!parsed || !parsed[1] || !parsed[4]) {
46 return null;
47 }
48
49 commit.type = parsed[1].toLowerCase();
50 commit.category = parsed[3];
51 commit.subject = parsed[4];
52
53 return commit;
54 })
55 .filter(function (commit) {
56 return commit !== null;
57 });
58};