Skip to content

Commit 3d20343

Browse files
committed
Move footer generation to own file
1 parent 0eed36c commit 3d20343

File tree

4 files changed

+118
-107
lines changed

4 files changed

+118
-107
lines changed

lib/changes.js

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const fs = require('fs');
99
const $ = require('child_process');
1010
const hostedGitInfo = require('hosted-git-info');
1111
const changelog = require('./changelog');
12-
const github = require('./github');
12+
const footer = require('./footer');
1313

1414
const CHANGES_HEADING = '# Changes';
1515
const DEFAULT_CHANGES_FILE = 'CHANGES.md';
@@ -30,15 +30,6 @@ function buildTag(options, version, pkg) {
3030
);
3131
}
3232

33-
function buildFooter(newline, author, homepage) {
34-
let footer = `${newline}_Released`;
35-
if (author) {
36-
footer += homepage ? ` by [${author}](${homepage})` : ` by ${author}`;
37-
}
38-
const today = new Date().toISOString().split('T')[0];
39-
return `${footer} on ${today}._${newline}`;
40-
}
41-
4233
// Write the commit history to the changes file
4334
exports.write = async function (options = {}) {
4435
const changes_file = options.changes_file || DEFAULT_CHANGES_FILE;
@@ -103,44 +94,31 @@ exports.write = async function (options = {}) {
10394
pkg
10495
});
10596

106-
function write() {
107-
// Do not allow version to be added twice
108-
if (exists(previous, version)) {
109-
console.error(`Version ${version} is already in ${changes_file}\n`);
110-
if (changes) {
111-
console.error('# Changes for next release:\n');
112-
console.error(changes);
113-
}
114-
process.exit(1);
115-
return null;
116-
}
97+
if (options.footer) {
98+
const foot = await footer.generate();
99+
changes += `${newline}${foot}${newline}`;
100+
}
117101

118-
// Generate new changes
119-
let next = `${heading}## ${version}${newline}${newline}${changes}`;
120-
const remain = previous.substring(heading.length);
121-
if (remain) {
122-
next += `${newline}${remain}`;
102+
// Do not allow version to be added twice
103+
if (exists(previous, version)) {
104+
console.error(`Version ${version} is already in ${changes_file}\n`);
105+
if (changes) {
106+
console.error('# Changes for next release:\n');
107+
console.error(changes);
123108
}
124-
fs.writeFileSync(changes_file, next);
125-
126-
return { previous, changes_file };
109+
process.exit(1);
110+
return null;
127111
}
128112

129-
if (options.footer) {
130-
const author = process.env.GIT_AUTHOR_NAME;
131-
if (author) {
132-
const email = process.env.GIT_AUTHOR_EMAIL;
133-
if (email) {
134-
const homepage = await github.fetchUserHomepage(email);
135-
changes += buildFooter(newline, author, homepage);
136-
return write();
137-
}
138-
changes += buildFooter(newline, author);
139-
} else {
140-
changes += buildFooter(newline);
141-
}
113+
// Generate new changes
114+
let next = `${heading}## ${version}${newline}${newline}${changes}`;
115+
const remain = previous.substring(heading.length);
116+
if (remain) {
117+
next += `${newline}${remain}`;
142118
}
143-
return write();
119+
fs.writeFileSync(changes_file, next);
120+
121+
return { previous, changes_file };
144122
};
145123

146124
// Roll back changes

lib/footer.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const github = require('./github');
4+
5+
function buildFooter(author, homepage) {
6+
let footer = '_Released';
7+
if (author) {
8+
footer += homepage ? ` by [${author}](${homepage})` : ` by ${author}`;
9+
}
10+
const today = new Date().toISOString().split('T')[0];
11+
return `${footer} on ${today}._`;
12+
}
13+
14+
async function generateFooter() {
15+
const author = process.env.GIT_AUTHOR_NAME;
16+
if (author) {
17+
const email = process.env.GIT_AUTHOR_EMAIL;
18+
if (email) {
19+
const homepage = await github.fetchUserHomepage(email);
20+
return buildFooter(author, homepage);
21+
}
22+
return buildFooter(author);
23+
}
24+
return buildFooter();
25+
}
26+
27+
exports.generate = generateFooter;

test/changes-test.js

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fs = require('fs');
44
const $ = require('child_process');
55
const { assert, refute, sinon } = require('@sinonjs/referee-sinon');
6-
const github = require('../lib/github');
6+
const footer = require('../lib/footer');
77
const changes = require('..');
88

99
describe('changes', () => {
@@ -17,8 +17,6 @@ describe('changes', () => {
1717

1818
afterEach(() => {
1919
sinon.restore();
20-
delete process.env.GIT_AUTHOR_NAME;
21-
delete process.env.GIT_AUTHOR_EMAIL;
2220
});
2321

2422
function packageJson(json) {
@@ -516,76 +514,18 @@ describe('changes', () => {
516514
);
517515
});
518516

519-
function today() {
520-
return new Date().toISOString().split('T')[0];
521-
}
522-
523-
it('generates footer without author', async () => {
524-
packageJson();
525-
missingChanges();
526-
setLog('» Inception (Studio)\n\n\n');
527-
528-
await changes.write({ footer: true });
529-
530-
assert.calledOnceWith(
531-
fs.writeFileSync,
532-
'CHANGES.md',
533-
`# Changes\n\n## 1.0.0\n\n- Inception\n\n_Released on ${today()}._\n`
534-
);
535-
});
536-
537-
it('generates footer with author author without link', async () => {
538-
process.env.GIT_AUTHOR_NAME = 'Maximilian Antoni';
539-
packageJson();
540-
missingChanges();
541-
setLog('» Inception (Studio)\n\n\n');
542-
543-
await changes.write({ footer: true });
544-
545-
assert.calledOnceWith(
546-
fs.writeFileSync,
547-
'CHANGES.md',
548-
'# Changes\n\n## 1.0.0\n\n- Inception\n\n' +
549-
`_Released by Maximilian Antoni on ${today()}._\n`
550-
);
551-
});
552-
553-
it('generates footer with author author with github homepage link', async () => {
554-
sinon.replace(
555-
github,
556-
'fetchUserHomepage',
557-
sinon.fake.resolves('https://github.com/mantoni')
558-
);
559-
process.env.GIT_AUTHOR_NAME = 'Maximilian Antoni';
560-
process.env.GIT_AUTHOR_EMAIL = 'mail@maxantoni.de';
517+
it('generates footer', async () => {
518+
sinon.replace(footer, 'generate', sinon.fake.resolves('**The footer**'));
561519
packageJson();
562520
missingChanges();
563521
setLog('» Inception (Studio)\n\n\n');
564522

565523
await changes.write({ footer: true });
566524

567-
assert.calledOnceWith(github.fetchUserHomepage, 'mail@maxantoni.de');
568525
assert.calledOnceWith(
569526
fs.writeFileSync,
570527
'CHANGES.md',
571-
'# Changes\n\n## 1.0.0\n\n- Inception\n\n' +
572-
'_Released by [Maximilian Antoni](https://github.com/mantoni) ' +
573-
`on ${today()}._\n`
528+
`# Changes\n\n## 1.0.0\n\n- Inception\n\n**The footer**\n`
574529
);
575530
});
576-
577-
it('fails if github homepage link can not be retrieved', async () => {
578-
const error = new Error('Oh noes!');
579-
sinon.replace(github, 'fetchUserHomepage', sinon.fake.rejects(error));
580-
process.env.GIT_AUTHOR_NAME = 'Maximilian Antoni';
581-
process.env.GIT_AUTHOR_EMAIL = 'mail@maxantoni.de';
582-
packageJson();
583-
missingChanges();
584-
setLog('» Inception (Studio)\n\n\n');
585-
586-
const promise = changes.write({ footer: true });
587-
588-
await assert.rejects(promise, error);
589-
refute.called(fs.writeFileSync);
590-
});
591531
});

test/footer-test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
const { assert, sinon } = require('@sinonjs/referee-sinon');
4+
const github = require('../lib/github');
5+
const footer = require('../lib/footer');
6+
7+
function today() {
8+
return new Date().toISOString().split('T')[0];
9+
}
10+
11+
describe('footer', () => {
12+
before(() => {
13+
delete process.env.GIT_AUTHOR_NAME;
14+
delete process.env.GIT_AUTHOR_EMAIL;
15+
});
16+
17+
afterEach(() => {
18+
sinon.restore();
19+
delete process.env.GIT_AUTHOR_NAME;
20+
delete process.env.GIT_AUTHOR_EMAIL;
21+
});
22+
23+
it('generates footer without author', async () => {
24+
const foot = await footer.generate();
25+
26+
assert.equals(foot, `_Released on ${today()}._`);
27+
});
28+
29+
it('generates footer with author author without link', async () => {
30+
process.env.GIT_AUTHOR_NAME = 'Maximilian Antoni';
31+
32+
const foot = await footer.generate();
33+
34+
assert.equals(foot, `_Released by Maximilian Antoni on ${today()}._`);
35+
});
36+
37+
it('generates footer with author author with github homepage link', async () => {
38+
sinon.replace(
39+
github,
40+
'fetchUserHomepage',
41+
sinon.fake.resolves('https://github.com/mantoni')
42+
);
43+
process.env.GIT_AUTHOR_NAME = 'Maximilian Antoni';
44+
process.env.GIT_AUTHOR_EMAIL = 'mail@maxantoni.de';
45+
46+
const foot = await footer.generate();
47+
48+
assert.calledOnceWith(github.fetchUserHomepage, 'mail@maxantoni.de');
49+
assert.equals(
50+
foot,
51+
'_Released by [Maximilian Antoni](https://github.com/mantoni) on ' +
52+
`${today()}._`
53+
);
54+
});
55+
56+
it('fails if github homepage link can not be retrieved', async () => {
57+
const error = new Error('Oh noes!');
58+
sinon.replace(github, 'fetchUserHomepage', sinon.fake.rejects(error));
59+
process.env.GIT_AUTHOR_NAME = 'Maximilian Antoni';
60+
process.env.GIT_AUTHOR_EMAIL = 'mail@maxantoni.de';
61+
62+
const promise = footer.generate();
63+
64+
await assert.rejects(promise, error);
65+
});
66+
});

0 commit comments

Comments
 (0)