Skip to content

Commit

Permalink
add template functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Schmitz von Hülst committed Nov 26, 2020
1 parent 07627bf commit 0dac835
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 40 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ async function verifyConditions(pluginConfig, context) {
pluginConfig.changelogFile = defaultTo(pluginConfig.changelogFile, preparePlugin.changelogFile);
}

await verifyChangelog(pluginConfig);
await verifyChangelog(pluginConfig, context);
verified = true;
}

async function prepare(pluginConfig, context) {
if (!verified) {
await verifyChangelog(pluginConfig);
await verifyChangelog(pluginConfig, context);
verified = true;
}

Expand Down
7 changes: 5 additions & 2 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ const path = require('path');
const {readFile, writeFile, ensureFile} = require('fs-extra');
const resolveConfig = require('./resolve-config');

module.exports = async (pluginConfig, {cwd, nextRelease: {notes}, logger}) => {
const {changelogFile, changelogTitle} = resolveConfig(pluginConfig);
module.exports = async (pluginConfig, {cwd, stdout, stderr, logger, ...context}) => {
const {changelogFile, changelogTitle} = resolveConfig(pluginConfig, context);
const changelogPath = path.resolve(cwd, changelogFile);
const {
nextRelease: {notes},
} = context;

if (notes) {
await ensureFile(changelogPath);
Expand Down
6 changes: 3 additions & 3 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {isNil} = require('lodash');
const {isNil, template} = require('lodash');

module.exports = ({changelogFile, changelogTitle}) => ({
changelogFile: isNil(changelogFile) ? 'CHANGELOG.md' : changelogFile,
module.exports = ({changelogFile, changelogTitle}, context) => ({
changelogFile: isNil(changelogFile) ? 'CHANGELOG.md' : template(changelogFile)(context),
changelogTitle,
});
4 changes: 2 additions & 2 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const VALIDATORS = {
changelogTitle: isNonEmptyString,
};

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

const errors = Object.entries(options).reduce(
(errors, [option, value]) =>
Expand Down
34 changes: 18 additions & 16 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ test.serial('Create new CHANGELOG.md', async t => {
t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
});

test.serial('Create new changelog with template', async t => {
const cwd = tempy.directory();
const notes = 'Test release note';
const version = '1.2.3-development.0';
const changelogFile = `docs/CHANGELOG-\${nextRelease.version}.txt`;
const changelogPath = path.resolve(cwd, `docs/CHANGELOG-${version}.txt`);

await t.context.m.prepare(
{changelogFile},
{cwd, options: {}, nextRelease: {notes, version}, logger: t.context.logger}
);

// Verify the content of the CHANGELOG.md
t.is((await readFile(changelogPath)).toString(), `${notes}\n`);

t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
});

test.serial('Skip changelog update if the release is empty', async t => {
const cwd = tempy.directory();
const changelogFile = 'CHANGELOG.txt';
Expand Down Expand Up @@ -65,19 +83,3 @@ test.serial('Verify only on the fist call', async t => {

t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
});

test('Throw SemanticReleaseError if prepare "changelogFile" option is not a string', async t => {
const cwd = tempy.directory();
const changelogFile = 42;
const errors = [
...(await t.throwsAsync(
t.context.m.verifyConditions(
{},
{cwd, options: {prepare: ['@semantic-release/git', {path: '@semantic-release/changelog', changelogFile}]}}
)
)),
];

t.is(errors[0].name, 'SemanticReleaseError');
t.is(errors[0].code, 'EINVALIDCHANGELOGFILE');
});
13 changes: 13 additions & 0 deletions test/prepare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,16 @@ test.serial('Create new changelog with title if specified', async t => {

t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n`);
});

test('Create new changelog with template', async t => {
const cwd = tempy.directory();
const notes = 'Test release note';
const version = '1.2.3';
const changelogTitle = '# My Changelog Title';
const changelogFile = `HISTORY-\${nextRelease.version}.md`;
const changelogPath = path.resolve(cwd, `HISTORY-${version}.md`);

await prepare({changelogTitle, changelogFile}, {cwd, nextRelease: {notes, version}, logger: t.context.logger});

t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n`);
});
22 changes: 7 additions & 15 deletions test/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,47 @@ const verify = require('../lib/verify');
test('Verify String "changelogFile" and "chagngelogTitle"', t => {
const changelogFile = 'docs/changelog.txt';
const changelogTitle = '# My title here';
t.notThrows(() => verify({changelogFile, changelogTitle}));
t.notThrows(() => verify({changelogFile, changelogTitle}, {}));
});

test('Verify undefined "changelogFile" and "chagngelogTitle"', t => {
t.notThrows(() => verify({}));
});

test('Throw SemanticReleaseError if "changelogFile" option is not a String', t => {
const changelogFile = 42;
const [error] = t.throws(() => verify({changelogFile}));

t.is(error.name, 'SemanticReleaseError');
t.is(error.code, 'EINVALIDCHANGELOGFILE');
t.notThrows(() => verify({}, {}));
});

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

t.is(error.name, 'SemanticReleaseError');
t.is(error.code, 'EINVALIDCHANGELOGFILE');
});

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

t.is(error.name, 'SemanticReleaseError');
t.is(error.code, 'EINVALIDCHANGELOGFILE');
});

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

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

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

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

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

t.is(error.name, 'SemanticReleaseError');
t.is(error.code, 'EINVALIDCHANGELOGTITLE');
Expand Down

0 comments on commit 0dac835

Please sign in to comment.