Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that even if there are no changes, we add _something_ to CHANGELOG.md. #47

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const LERNA_PATH = require.resolve('lerna-changelog/bin/cli');
// and this makes it much simpler
const UNRELEASED = 'Unreleased';

function getToday() {
const date = new Date().toISOString();

return date.slice(0, date.indexOf('T'));
}

module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
async init() {
let from = (await this.getTagForHEAD()) || (await this.getFirstCommit());
Expand All @@ -23,7 +29,9 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {

get nextVersion() {
let { version } = this.config.getContext();
let nextVersion = this.getTagNameFromVersion(version);

let tagName = this.config.getContext('git.tagName');
let nextVersion = tagName ? format(tagName, { version }) : version;

return nextVersion;
}
Expand All @@ -33,12 +41,6 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
return this.changelog;
}

getTagNameFromVersion(version) {
let tagName = this.config.getContext('git.tagName');

return format(tagName, { version });
}

async getTagForHEAD() {
try {
return await this.exec('git describe --tags --abbrev=0', { options: { write: false } });
Expand Down Expand Up @@ -67,8 +69,11 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
return changelog;
}

async processChangelog(_changelog) {
let changelog = _changelog.replace(UNRELEASED, this.nextVersion);
async processChangelog() {
// this is populated in `init`
let changelog = this.changelog
? this.changelog.replace(UNRELEASED, this.nextVersion)
: `## ${this.nextVersion} (${getToday()})`;

let finalChangelog = await this.reviewChangelog(changelog);

Expand Down Expand Up @@ -163,9 +168,7 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
}

async beforeRelease() {
// this is populated in `init`
let changelog = this.changelog || '';
let processedChangelog = await this.processChangelog(changelog);
let processedChangelog = await this.processChangelog();

this.debug({ changelog: processedChangelog });

Expand Down
18 changes: 17 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ test('it invokes lerna-changelog', async (t) => {
});

test('it honors custom git.tagName formatting', async (t) => {
let plugin = buildPlugin();
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });

plugin.config.setContext({ git: { tagName: 'v${version}' } });

Expand All @@ -105,6 +106,9 @@ test('it honors custom git.tagName formatting', async (t) => {
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
]);

const changelog = fs.readFileSync(infile, { encoding: 'utf8' });
t.is(changelog, `### v1.0.1 (2020-03-18)\n\nThe changelog\n\n`);
});

test('it sets the changelog without version information onto the config', async (t) => {
Expand All @@ -117,6 +121,18 @@ test('it sets the changelog without version information onto the config', async
t.is(changelog, 'The changelog');
});

test('it prints something to CHANGELOG.md when lerna-changelog returns no content', async (t) => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });

plugin.responses[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`] = '';

await runTasks(plugin);

const changelog = fs.readFileSync(infile, { encoding: 'utf8' });
t.is(changelog, `## 1.0.1 (${new Date().toISOString().slice(0, 10)})\n\n`);
});

test('it uses the first commit when no tags exist', async (t) => {
let infile = tmp.fileSync().name;

Expand Down