Skip to content

Commit d1803da

Browse files
Moritz Schmitz von Hülstboxcee
authored andcommitted
feat: add template functionality
1 parent 1c48000 commit d1803da

File tree

7 files changed

+52
-42
lines changed

7 files changed

+52
-42
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ async function verifyConditions(pluginConfig, context) {
1616
pluginConfig.changelogFile = defaultTo(pluginConfig.changelogFile, preparePlugin.changelogFile);
1717
}
1818

19-
await verifyChangelog(pluginConfig);
19+
await verifyChangelog(pluginConfig, context);
2020
verified = true;
2121
}
2222

2323
async function prepare(pluginConfig, context) {
2424
if (!verified) {
25-
await verifyChangelog(pluginConfig);
25+
await verifyChangelog(pluginConfig, context);
2626
verified = true;
2727
}
2828

lib/prepare.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ const path = require('path');
22
const {readFile, writeFile, ensureFile} = require('fs-extra');
33
const resolveConfig = require('./resolve-config.js');
44

5-
module.exports = async (pluginConfig, {cwd, nextRelease: {notes}, logger}) => {
6-
const {changelogFile, changelogTitle} = resolveConfig(pluginConfig);
5+
module.exports = async (pluginConfig, {cwd, stdout, stderr, logger, ...context}) => {
6+
const {changelogFile, changelogTitle} = resolveConfig(pluginConfig, context);
77
const changelogPath = path.resolve(cwd, changelogFile);
8+
const {
9+
nextRelease: {notes},
10+
} = context;
811

912
if (notes) {
1013
await ensureFile(changelogPath);

lib/resolve-config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const {isNil} = require('lodash');
1+
const {isNil, template} = require('lodash');
22

3-
module.exports = ({changelogFile, changelogTitle}) => ({
4-
changelogFile: isNil(changelogFile) ? 'CHANGELOG.md' : changelogFile,
3+
module.exports = ({changelogFile, changelogTitle}, context) => ({
4+
changelogFile: isNil(changelogFile) ? 'CHANGELOG.md' : template(changelogFile)(context),
55
changelogTitle,
66
});

lib/verify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const VALIDATORS = {
1010
changelogTitle: isNonEmptyString,
1111
};
1212

13-
module.exports = (pluginConfig) => {
14-
const options = resolveConfig(pluginConfig);
13+
module.exports = (pluginConfig, {cwd, stdout, stderr, logger, ...context}) => {
14+
const options = resolveConfig(pluginConfig, context);
1515

1616
const errors = Object.entries(options).reduce(
1717
(errors, [option, value]) =>

test/integration.test.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ test.serial('Create new CHANGELOG.md', async (t) => {
3636
t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
3737
});
3838

39+
test.serial('Create new changelog with template', async (t) => {
40+
const cwd = tempy.directory();
41+
const notes = 'Test release note';
42+
const version = '1.2.3-development.0';
43+
const changelogFile = `docs/CHANGELOG-\${nextRelease.version}.txt`;
44+
const changelogPath = path.resolve(cwd, `docs/CHANGELOG-${version}.txt`);
45+
46+
await t.context.m.prepare(
47+
{changelogFile},
48+
{cwd, options: {}, nextRelease: {notes, version}, logger: t.context.logger}
49+
);
50+
51+
// Verify the content of the CHANGELOG.md
52+
t.is((await readFile(changelogPath)).toString(), `${notes}\n`);
53+
54+
t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
55+
});
56+
3957
test.serial('Skip changelog update if the release is empty', async (t) => {
4058
const cwd = tempy.directory();
4159
const changelogFile = 'CHANGELOG.txt';
@@ -65,19 +83,3 @@ test.serial('Verify only on the fist call', async (t) => {
6583

6684
t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
6785
});
68-
69-
test('Throw SemanticReleaseError if prepare "changelogFile" option is not a string', async (t) => {
70-
const cwd = tempy.directory();
71-
const changelogFile = 42;
72-
const errors = [
73-
...(await t.throwsAsync(
74-
t.context.m.verifyConditions(
75-
{},
76-
{cwd, options: {prepare: ['@semantic-release/git', {path: '@semantic-release/changelog', changelogFile}]}}
77-
)
78-
)),
79-
];
80-
81-
t.is(errors[0].name, 'SemanticReleaseError');
82-
t.is(errors[0].code, 'EINVALIDCHANGELOGFILE');
83-
});

test/prepare.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,16 @@ test.serial('Create new changelog with title if specified', async (t) => {
9090

9191
t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n`);
9292
});
93+
94+
test('Create new changelog with template', async t => {
95+
const cwd = tempy.directory();
96+
const notes = 'Test release note';
97+
const version = '1.2.3';
98+
const changelogTitle = '# My Changelog Title';
99+
const changelogFile = `HISTORY-\${nextRelease.version}.md`;
100+
const changelogPath = path.resolve(cwd, `HISTORY-${version}.md`);
101+
102+
await prepare({changelogTitle, changelogFile}, {cwd, nextRelease: {notes, version}, logger: t.context.logger});
103+
104+
t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n`);
105+
});

test/verify.test.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,50 @@
11
const test = require('ava');
22
const verify = require('../lib/verify.js');
33

4-
test('Verify String "changelogFile" and "chagngelogTitle"', (t) => {
4+
test('Verify String "changelogFile" and "changelogTitle"', (t) => {
55
const changelogFile = 'docs/changelog.txt';
66
const changelogTitle = '# My title here';
7-
t.notThrows(() => verify({changelogFile, changelogTitle}));
7+
t.notThrows(() => verify({changelogFile, changelogTitle}, {}));
88
});
99

10-
test('Verify undefined "changelogFile" and "chagngelogTitle"', (t) => {
11-
t.notThrows(() => verify({}));
12-
});
13-
14-
test('Throw SemanticReleaseError if "changelogFile" option is not a String', (t) => {
15-
const changelogFile = 42;
16-
const [error] = t.throws(() => verify({changelogFile}));
17-
18-
t.is(error.name, 'SemanticReleaseError');
19-
t.is(error.code, 'EINVALIDCHANGELOGFILE');
10+
test('Verify undefined "changelogFile" and "changelogTitle"', (t) => {
11+
t.notThrows(() => verify({}, {}));
2012
});
2113

2214
test('Throw SemanticReleaseError if "changelogFile" option is an empty String', (t) => {
2315
const changelogFile = '';
24-
const [error] = t.throws(() => verify({changelogFile}));
16+
const [error] = t.throws(() => verify({changelogFile}, {}));
2517

2618
t.is(error.name, 'SemanticReleaseError');
2719
t.is(error.code, 'EINVALIDCHANGELOGFILE');
2820
});
2921

3022
test('Throw SemanticReleaseError if "changelogFile" option is a whitespace String', (t) => {
3123
const changelogFile = ' \n \r ';
32-
const [error] = t.throws(() => verify({changelogFile}));
24+
const [error] = t.throws(() => verify({changelogFile}, {}));
3325

3426
t.is(error.name, 'SemanticReleaseError');
3527
t.is(error.code, 'EINVALIDCHANGELOGFILE');
3628
});
3729

3830
test('Throw SemanticReleaseError if "changelogTitle" option is not a String', (t) => {
3931
const changelogTitle = 42;
40-
const [error] = t.throws(() => verify({changelogTitle}));
32+
const [error] = t.throws(() => verify({changelogTitle}, {}));
4133

4234
t.is(error.name, 'SemanticReleaseError');
4335
t.is(error.code, 'EINVALIDCHANGELOGTITLE');
4436
});
4537

4638
test('Throw SemanticReleaseError if "changelogTitle" option is an empty String', (t) => {
47-
const [error] = t.throws(() => verify({changelogTitle: ''}));
39+
const [error] = t.throws(() => verify({changelogTitle: ''}, {}));
4840

4941
t.is(error.name, 'SemanticReleaseError');
5042
t.is(error.code, 'EINVALIDCHANGELOGTITLE');
5143
});
5244

5345
test('Throw SemanticReleaseError if "changelogTitle" option is a whitespace String', (t) => {
5446
const changelogTitle = ' \n \r ';
55-
const [error] = t.throws(() => verify({changelogTitle}));
47+
const [error] = t.throws(() => verify({changelogTitle}, {}));
5648

5749
t.is(error.name, 'SemanticReleaseError');
5850
t.is(error.code, 'EINVALIDCHANGELOGTITLE');

0 commit comments

Comments
 (0)