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

Show lerna-changelog output _before_ version prompt. #12

Merged
merged 1 commit into from
Mar 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 45 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@ const { format } = require('release-it/lib/util');
const tmp = require('tmp');
const execa = require('execa');

const LERNA_PATH = require.resolve('lerna-changelog/bin/cli');

// using a const here, because we may need to change this value in the future
// and this makes it much simpler
const UNRELEASED = 'Unreleased';

module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
get lernaPath() {
return require.resolve('lerna-changelog/bin/cli');
async init() {
let from = (await this.getTagForHEAD()) || (await this.getFirstCommit());
let changelog = await this._execLernaChangelog(from);

this.setContext({ changelog });
}

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

return nextVersion;
}

getTagNameFromVersion(version) {
Expand All @@ -16,47 +32,34 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
return format(tagName, { version });
}

async hasTag(tag) {
try {
await this.exec(`git show-ref --tags --quiet --verify -- "refs/tags/${tag}"`, {
options: { write: false },
});

return true;
} catch (e) {
this.debug(`hasTag(${tag}): ${e}`);
async getTagForHEAD() {
let tag = await this.exec('git describe --tags --abbrev=0', { options: { write: false } });

return false;
}
return tag;
}

async getFirstCommit() {
let firstCommit = await this.exec(`git rev-list --max-parents=0 HEAD`);
if (this._firstCommit) {
return this._firstCommit;
}

this._firstCommit = await this.exec(`git rev-list --max-parents=0 HEAD`, {
options: { write: false },
});

return firstCommit;
return this._firstCommit;
}

async _execLernaChangelog(from, nextVersion) {
let changelog = await this.exec(
`${this.lernaPath} --next-version=${nextVersion} --from=${from}`,
{
options: { write: false },
}
);
async _execLernaChangelog(from) {
let changelog = await this.exec(`${LERNA_PATH} --next-version=${UNRELEASED} --from=${from}`, {
options: { write: false },
});

return changelog;
}

async getChangelog(_from) {
let { version, latestVersion } = this.config.getContext();
let from = _from || this.getTagNameFromVersion(latestVersion);
let nextVersion = this.getTagNameFromVersion(version);

if (!(await this.hasTag(from))) {
from = await this.getFirstCommit();
}

let changelog = await this._execLernaChangelog(from, nextVersion);
async processChangelog(_changelog) {
let changelog = _changelog.replace(UNRELEASED, this.nextVersion);

let finalChangelog = await this.reviewChangelog(changelog);

Expand Down Expand Up @@ -115,10 +118,13 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
}

if (!hasInfile) {
// generate an initial CHANGELOG.md with all of the versions
let firstCommit = await this.getFirstCommit();

if (firstCommit) {
changelog = await this.getChangelog(firstCommit);
changelog = await this._execLernaChangelog(firstCommit, this.nextVersion);
changelog = changelog.replace(UNRELEASED, this.nextVersion);

this.debug({ changelog });
} else {
// do something when there is no commit? not sure what our options are...
Expand All @@ -138,22 +144,24 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
}

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

this.debug({ changelog });
this.debug({ changelog: processedChangelog });

// remove first two lines to prevent release notes
// from including the version number/date (it looks odd
// in the Github/Gitlab UIs)
let changelogWithoutVersion = changelog
let changelogWithoutVersion = processedChangelog
.split(EOL)
.slice(2)
.join(EOL);

this.config.setContext({ changelog: changelogWithoutVersion });

if (this.options.infile) {
await this.writeChangelog(changelog);
await this.writeChangelog(processedChangelog);
}
}
};
82 changes: 51 additions & 31 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const EDITOR = process.env.EDITOR || null;

tmp.setGracefulCleanup();

const LERNA_PATH = require.resolve('lerna-changelog/bin/cli');
const namespace = 'release-it-lerna-changelog';

function resetEDITOR() {
Expand Down Expand Up @@ -37,9 +38,19 @@ class TestPlugin extends Plugin {
constructor() {
super(...arguments);

this.responses = {
// always assume v1.0.0 unless specifically overridden
'git describe --tags --abbrev=0': 'v1.0.0',

[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`]: '### Unreleased (2020-03-18)\n\nThe changelog',
};

this.commands = [];
this.shell.execFormattedCommand = async (command, options) => {
this.commands.push([command, options]);
if (this.responses[command]) {
return Promise.resolve(this.responses[command]);
}
};
}

Expand All @@ -63,8 +74,8 @@ test('it invokes lerna-changelog', async t => {
await runTasks(plugin);

t.deepEqual(plugin.commands, [
[`git show-ref --tags --quiet --verify -- "refs/tags/1.0.0"`, { write: false }],
[`${plugin.lernaPath} --next-version=1.0.1 --from=1.0.0`, { write: false }],
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
]);
});

Expand All @@ -76,17 +87,15 @@ test('it honors custom git.tagName formatting', async t => {
await runTasks(plugin);

t.deepEqual(plugin.commands, [
[`git show-ref --tags --quiet --verify -- "refs/tags/v1.0.0"`, { write: false }],
[`${plugin.lernaPath} --next-version=v1.0.1 --from=v1.0.0`, { write: false }],
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
]);
});

test('it sets the changelog without version information onto the config', async t => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });

plugin.getChangelog = () => Promise.resolve('## v9.9.9 (2019-01-01)\n\nThe changelog');

await runTasks(plugin);

const { changelog } = plugin.config.getContext();
Expand All @@ -95,27 +104,41 @@ test('it sets the changelog without version information onto the config', async

test('it writes the changelog to the specified file when it did not exist', async t => {
let infile = tmp.fileSync().name;
fs.unlinkSync(infile);

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

plugin.getChangelog = () => Promise.resolve('## v9.9.9 (2019-01-01)\n\nThe changelog');
Object.assign(plugin.responses, {
'git rev-list --max-parents=0 HEAD': 'aabc',
[`${LERNA_PATH} --next-version=Unreleased --from=aabc`]: `### Unreleased\n\nThe changelog\n### v1.0.0\n\nThe old changelog`,
});

await runTasks(plugin);

const changelog = fs.readFileSync(infile);
t.is(changelog.toString().trim(), '## v9.9.9 (2019-01-01)\n\nThe changelog');
t.deepEqual(plugin.commands, [
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
['git rev-list --max-parents=0 HEAD', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=aabc`, { write: false }],
[`git add ${infile}`, {}],
]);

const changelog = fs.readFileSync(infile, { encoding: 'utf8' });
t.is(changelog.trim(), '### v1.0.1\n\nThe changelog\n### v1.0.0\n\nThe old changelog');
});

test('prepends the changelog to the existing file', async t => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });
plugin.getChangelog = () => Promise.resolve('## v9.9.9 (2019-01-01)\n\nThe changelog');
plugin.config.setContext({ git: { tagName: 'v${version}' } });

fs.writeFileSync(infile, 'Old contents', { encoding: 'utf8' });

await runTasks(plugin);

const changelog = fs.readFileSync(infile);
t.is(changelog.toString().trim(), '## v9.9.9 (2019-01-01)\n\nThe changelog\n\nOld contents');
t.is(changelog.toString().trim(), '### v1.0.1 (2020-03-18)\n\nThe changelog\n\nOld contents');
});

test('uses launchEditor command', async t => {
Expand Down Expand Up @@ -158,8 +181,8 @@ test('throws if launchEditor is `true` and no $EDITOR present', async t => {
await runTasks(plugin);
} catch (error) {
t.deepEqual(plugin.commands, [
[`git show-ref --tags --quiet --verify -- "refs/tags/1.0.0"`, { write: false }],
[`${plugin.lernaPath} --next-version=1.0.1 --from=1.0.0`, { write: false }],
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
]);

t.is(
Expand All @@ -172,33 +195,30 @@ test('throws if launchEditor is `true` and no $EDITOR present', async t => {
});

test('launches configured editor, updates infile, and propogates changes to context', async t => {
class TestPlugin extends Plugin {
constructor() {
super(...arguments);

this.commands = [];
}

async _execLernaChangelog() {
return '## v9.9.9 (2019-01-01)\n\nThe changelog';
}

async _launchEditor(tmpFile) {
let originalChangelog = await this._execLernaChangelog();

fs.writeFileSync(tmpFile, originalChangelog + '\nExtra stuff!', { encoding: 'utf-8' });
}
let fakeEditorFile = tmp.fileSync().name;
// using a function here so it is easier to author (vs a giant string interpolation)
function fakeEditor() {
let fs = require('fs');
let fileName = process.argv[2];

let contents = fs.readFileSync(fileName, { encoding: 'utf8' });
fs.writeFileSync(fileName, `${contents}\nExtra stuff!`, { encoding: 'utf8' });
}
fs.writeFileSync(fakeEditorFile, `${fakeEditor.toString()}\nfakeEditor();`, { encoding: 'utf8' });

let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile, launchEditor: 'foo-editor -w ${file}' }, TestPlugin);
let plugin = buildPlugin({
infile,
launchEditor: `${process.execPath} ${fakeEditorFile} \${file}`,
});
plugin.config.setContext({ git: { tagName: 'v${version}' } });

await runTasks(plugin);

const changelogFileContents = fs.readFileSync(infile);
t.is(
changelogFileContents.toString().trim(),
'## v9.9.9 (2019-01-01)\n\nThe changelog\nExtra stuff!'
'### v1.0.1 (2020-03-18)\n\nThe changelog\nExtra stuff!'
);

const { changelog } = plugin.config.getContext();
Expand Down