Skip to content

Commit 6a3cecf

Browse files
committed
Refactor changelog out of changes
1 parent 34c7ffb commit 6a3cecf

File tree

2 files changed

+76
-41
lines changed

2 files changed

+76
-41
lines changed

lib/changelog.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) Maximilian Antoni <max@javascript.studio>
3+
*
4+
* @license MIT
5+
*/
6+
'use strict';
7+
8+
const $ = require('child_process');
9+
10+
const VARIABLE_RE = /\$\{([^}]+)\}/g;
11+
12+
function parseAuthor(author) {
13+
const m = author.match(/[<(]/);
14+
return m ? author.substring(0, m.index).trim() : author;
15+
}
16+
17+
module.exports = function ({
18+
log_range,
19+
commits,
20+
newline,
21+
pkg
22+
}) {
23+
24+
let flags = '--format="» ';
25+
if (commits) {
26+
commits = commits.replace(VARIABLE_RE, (match, key) => pkg[key]);
27+
flags += `[\\\`%h\\\`](${commits}/%H)« `;
28+
}
29+
flags += '%s (%an)%n%n%b" --no-merges';
30+
let changes;
31+
try {
32+
changes = $.execSync(`git log ${log_range} ${flags}`, {
33+
encoding: 'utf8'
34+
});
35+
} catch (e) {
36+
process.exit(1);
37+
return null;
38+
}
39+
// Remove blanks (if no body) and indent body
40+
changes = changes
41+
.replace(/\n{3,}/g, '\n')
42+
// Indent body with quotes:
43+
.replace(/^([^»])/gm, ' > $1')
44+
// Remove trainling whitespace on blank quote lines
45+
.replace(/^ {4}> \n/gm, ' >\n');
46+
47+
changes = changes
48+
// Replace commit markers with dashes:
49+
.replace(/^»/gm, '-')
50+
// Replace newline markers with newlines:
51+
.replace(/«/gm, '\n')
52+
// Restore original newlines:
53+
.replace(/\n/gm, newline);
54+
55+
// Only mention contributors
56+
const { author } = pkg;
57+
if (author) {
58+
const author_name = typeof author === 'object'
59+
? author.name
60+
: parseAuthor(author);
61+
changes = changes.replace(new RegExp(` \\(${author_name}\\)$`, 'gm'), '');
62+
}
63+
64+
return changes;
65+
};

lib/changes.js

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const fs = require('fs');
99
const $ = require('child_process');
10+
const changelog = require('./changelog');
1011

1112
const CHANGES_HEADING = '# Changes';
1213
const DEFAULT_CHANGES_FILE = 'CHANGES.md';
@@ -27,11 +28,6 @@ function buildTag(options, version, pkg) {
2728
);
2829
}
2930

30-
function parseAuthor(author) {
31-
const m = author.match(/[<(]/);
32-
return m ? author.substring(0, m.index).trim() : author;
33-
}
34-
3531
// Write the commit history to the changes file
3632
exports.write = function (options, callback) {
3733
if (typeof options === 'function') {
@@ -45,10 +41,11 @@ exports.write = function (options, callback) {
4541
callback = () => {};
4642
}
4743
}
44+
4845
const changes_file = options.changes_file || DEFAULT_CHANGES_FILE;
4946
const package_json = fs.readFileSync('package.json', 'utf8');
5047
const pkg = JSON.parse(package_json);
51-
const { version, author } = pkg;
48+
const { version } = pkg;
5249

5350
// Get previous file content
5451
let previous;
@@ -75,7 +72,7 @@ exports.write = function (options, callback) {
7572
if (version_match) {
7673
log_range = `${buildTag(options, version_match[1], pkg)}..HEAD`;
7774
}
78-
let flags = '--format="» ';
75+
7976
let commits = options.commits;
8077
if (commits) {
8178
if (commits === true) {
@@ -87,42 +84,14 @@ exports.write = function (options, callback) {
8784
return;
8885
}
8986
}
90-
commits = commits.replace(VARIABLE_RE, (match, key) => pkg[key]);
91-
flags += `[\\\`%h\\\`](${commits}/%H)« `;
92-
}
93-
flags += '%s (%an)%n%n%b" --no-merges';
94-
let changes;
95-
try {
96-
changes = $.execSync(`git log ${log_range} ${flags}`, {
97-
encoding: 'utf8'
98-
});
99-
} catch (e) {
100-
process.exit(1);
101-
return;
10287
}
103-
// Remove blanks (if no body) and indent body
104-
changes = changes
105-
.replace(/\n{3,}/g, '\n')
106-
// Indent body with quotes:
107-
.replace(/^([^»])/gm, ' > $1')
108-
// Remove trainling whitespace on blank quote lines
109-
.replace(/^ {4}> \n/gm, ' >\n');
11088

111-
changes = changes
112-
// Replace commit markers with dashes:
113-
.replace(/^»/gm, '-')
114-
// Replace newline markers with newlines:
115-
.replace(/«/gm, '\n')
116-
// Restore original newlines:
117-
.replace(/\n/gm, newline);
118-
119-
// Only mention contributors
120-
if (author) {
121-
const author_name = typeof author === 'object'
122-
? author.name
123-
: parseAuthor(author);
124-
changes = changes.replace(new RegExp(` \\(${author_name}\\)$`, 'gm'), '');
125-
}
89+
const changes = changelog({
90+
log_range,
91+
commits,
92+
newline,
93+
pkg
94+
});
12695

12796
// Do not allow version to be added twice
12897
if (exists(previous, version)) {
@@ -142,6 +111,7 @@ exports.write = function (options, callback) {
142111
next += `${newline}${remain}`;
143112
}
144113
fs.writeFileSync(changes_file, next);
114+
145115
callback(null, { previous, changes_file });
146116
};
147117

0 commit comments

Comments
 (0)