Skip to content

Commit

Permalink
Prepare and always send full config argument to conventional-changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Apr 22, 2020
1 parent db421b5 commit efc2795
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
40 changes: 33 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,32 @@ class ConventionalChangelog extends Plugin {
return 'version';
}

getInitialOptions(options, namespace) {
options[namespace].tagName = options.git.tagName;
return options[namespace];
}

async getChangelog(latestVersion) {
const { version, previousTag, currentTag } = await this.getConventionalConfig(latestVersion);
this.setContext({ version, previousTag, currentTag });
return this.generateChangelog();
}

async getConventionalConfig(latestVersion) {
const { increment, isPreRelease, preReleaseId } = this.config.getContext('version');
const version = await this.getIncrementedVersion({ increment, latestVersion, isPreRelease, preReleaseId });
this.setContext({ version });

const previousTag = this.config.getContext('latestTag');
const tagTemplate = this.options.tagName || ((previousTag || '').match(/^v/) ? 'v${version}' : '${version}');
const currentTag = tagTemplate.replace('${version}', version);

return { version, previousTag, currentTag };
}

getIncrementedVersion({ increment, latestVersion, isPreRelease, preReleaseId }) {
const { version } = this.getContext();
if (version) return version;
this.debug({ increment, latestVersion, isPreRelease, preReleaseId });
return new Promise((resolve, reject) =>
conventionalRecommendedBump(this.options, (err, result) => {
Expand All @@ -35,14 +60,15 @@ class ConventionalChangelog extends Plugin {
);
}

getChangelog(latestVersion) {
return this.generateChangelog();
}

getChangelogStream(options = {}) {
return conventionalChangelog(Object.assign(options, this.options), null, {
debug: this.config.isDebug ? this.debug : null
});
const { version, previousTag, currentTag } = this.getContext();
return conventionalChangelog(
Object.assign(options, this.options),
{ version, previousTag, currentTag },
{
debug: this.config.isDebug ? this.debug : null
}
);
}

generateChangelog(options) {
Expand Down
19 changes: 10 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,48 @@ const Plugin = proxyquire('.', {
const namespace = 'conventional-changelog';
const preset = 'angular';
const infile = 'CHANGES.md';
const git = { tagName: '${version}' };

test('should not throw', async t => {
const options = { [namespace]: { preset } };
const options = { [namespace]: { preset }, git };
const plugin = factory(Plugin, { namespace, options });
await assert.doesNotReject(runTasks(plugin));
});

test('should set changelog', async t => {
const options = { [namespace]: { preset } };
const options = { [namespace]: { preset }, git };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const { changelog } = plugin.config.getContext();
assert.strictEqual(changelog, 'The changelog');
});

test('should use recommended bump', async t => {
const options = { [namespace]: { preset } };
const options = { [namespace]: { preset }, git };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const { version } = plugin.config.getContext();
assert.strictEqual(version, '1.1.0');
});

test('should use provided increment', async t => {
const options = { increment: 'major', [namespace]: { preset } };
const options = { increment: 'major', [namespace]: { preset }, git };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const { version } = plugin.config.getContext();
assert.strictEqual(version, '2.0.0');
});

test('should use provided version', async t => {
const options = { increment: '1.2.3', [namespace]: { preset } };
const options = { increment: '1.2.3', [namespace]: { preset }, git };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const { version } = plugin.config.getContext();
assert.strictEqual(version, '1.2.3');
});

test(`should write and update infile (${infile})`, async t => {
const options = { [namespace]: { preset, infile } };
const options = { [namespace]: { preset, infile }, git };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const changelog = fs.readFileSync(infile);
Expand All @@ -85,20 +86,20 @@ test(`should write and update infile (${infile})`, async t => {
});

test('should reject if conventional bump passes error', async t => {
const options = { [namespace]: { preset: 'what?' } };
const options = { [namespace]: { preset: 'what?' }, git };
const plugin = factory(Plugin, { namespace, options });
await assert.rejects(runTasks(plugin), /Something went wrong/);
});

test('should reject if conventional changelog has error', async t => {
const options = { [namespace]: { preset, releaseCount: -1 } };
const options = { [namespace]: { preset, releaseCount: -1 }, git };
const plugin = factory(Plugin, { namespace, options });
await assert.rejects(runTasks(plugin), /Something went wrong/);
});

test('should not write infile in dry run', async t => {
const infile = 'DRYRUN.md';
const options = { [namespace]: { preset, infile } };
const options = { [namespace]: { preset, infile }, git };
const plugin = factory(Plugin, { namespace, options, global: { isDryRun: true } });
const spy = sinon.spy(plugin, 'writeChangelog');
await runTasks(plugin);
Expand Down

0 comments on commit efc2795

Please sign in to comment.