Skip to content

Commit

Permalink
refactor: always return an Array of results/errors from a plugin pi…
Browse files Browse the repository at this point in the history
…peline

Always return an `Array` from a pipeline simplify the function utilization as it's more deterministic. Previously, it would return/throw a single value/error when called with a single input and an `Array` of results/errors when called with an `Array` of input.
  • Loading branch information
pvdlg committed Jul 10, 2018
1 parent cac4882 commit ed9c456
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 31 deletions.
15 changes: 6 additions & 9 deletions index.js
@@ -1,4 +1,4 @@
const {template, isPlainObject, castArray} = require('lodash');
const {template, isPlainObject} = require('lodash');
const marked = require('marked');
const TerminalRenderer = require('marked-terminal');
const envCi = require('env-ci');
Expand Down Expand Up @@ -81,7 +81,7 @@ async function run(options, plugins) {
const commits = await getCommits(lastRelease.gitHead, options.branch, logger);

logger.log('Call plugin %s', 'analyze-commits');
const type = await plugins.analyzeCommits({
const [type] = await plugins.analyzeCommits({
options,
logger,
lastRelease,
Expand All @@ -101,14 +101,14 @@ async function run(options, plugins) {

if (options.dryRun) {
logger.log('Call plugin %s', 'generate-notes');
const notes = await plugins.generateNotes(generateNotesParam);
const [notes] = await plugins.generateNotes(generateNotesParam);
logger.log('Release note for version %s:\n', nextRelease.version);
if (notes) {
process.stdout.write(`${marked(notes)}\n`);
}
} else {
logger.log('Call plugin %s', 'generateNotes');
nextRelease.notes = await plugins.generateNotes(generateNotesParam);
[nextRelease.notes] = await plugins.generateNotes(generateNotesParam);

logger.log('Call plugin %s', 'prepare');
await plugins.prepare(
Expand All @@ -121,7 +121,7 @@ async function run(options, plugins) {
nextRelease.gitHead = newGitHead;
// Regenerate the release notes
logger.log('Call plugin %s', 'generateNotes');
nextRelease.notes = await plugins.generateNotes(generateNotesParam);
[nextRelease.notes] = await plugins.generateNotes(generateNotesParam);
}
// Call the next publish plugin with the updated `nextRelease`
return {options, logger, lastRelease, commits, nextRelease};
Expand All @@ -141,10 +141,7 @@ async function run(options, plugins) {
{transform: (release, step) => ({...(isPlainObject(release) ? release : {}), ...nextRelease, ...step})}
);

await plugins.success(
{options, logger, lastRelease, commits, nextRelease, releases: castArray(releases)},
{settleAll: true}
);
await plugins.success({options, logger, lastRelease, commits, nextRelease, releases}, {settleAll: true});

logger.log('Published release: %s', nextRelease.version);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/pipeline.js
Expand Up @@ -49,7 +49,7 @@ module.exports = steps => async (input, {settleAll = false, getNextInput = ident
input
);
if (errors.length > 0) {
throw errors.length === 1 ? errors[0] : new AggregateError(errors);
throw new AggregateError(errors);
}
return results.length <= 1 ? results[0] : results;
return results;
};
20 changes: 1 addition & 19 deletions test/plugins/pipeline.test.js
Expand Up @@ -18,24 +18,6 @@ test('Execute each function in series passing the same input', async t => {
t.true(step2.calledBefore(step3));
});

test('With one step, returns the step values rather than an Array ', async t => {
const step1 = stub().resolves(1);

const result = await pipeline([step1])(0);

t.deepEqual(result, 1);
t.true(step1.calledWith(0));
});

test('With one step, throws the error rather than an AggregateError ', async t => {
const error = new Error('test error 1');
const step1 = stub().rejects(error);

const thrown = await t.throws(pipeline([step1])(0));

t.is(error, thrown);
});

test('Execute each function in series passing a transformed input from "getNextInput"', async t => {
const step1 = stub().resolves(1);
const step2 = stub().resolves(2);
Expand Down Expand Up @@ -96,7 +78,7 @@ test('Execute each function in series calling "transform" to modify the results
t.deepEqual(getNextInput.args, [[5, 1 + 1], [5, 2 + 1], [5, 3 + 1], [5, 4 + 1]]);
});

test('Stop execution and throw error is a step rejects', async t => {
test('Stop execution and throw error if a step rejects', async t => {
const step1 = stub().resolves(1);
const step2 = stub().rejects(new Error('test error'));
const step3 = stub().resolves(3);
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/plugins.test.js
Expand Up @@ -143,7 +143,7 @@ test('Merge global options with plugin options', async t => {
t.context.logger
);

const result = await plugins.verifyRelease();
const [result] = await plugins.verifyRelease();

t.deepEqual(result.pluginConfig, {localOpt: 'local', globalOpt: 'global', otherOpt: 'locally-defined'});
});
Expand Down

0 comments on commit ed9c456

Please sign in to comment.