refactor: always return an Array of results/errors from a plugin pipeline

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.
This commit is contained in:
Pierre Vanduynslager 2018-07-06 23:39:58 -04:00
parent cac48823f1
commit ed9c456f5e
4 changed files with 10 additions and 31 deletions

View File

@ -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');
@ -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,
@ -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(
@ -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};
@ -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);
}

View File

@ -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;
};

View File

@ -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);
@ -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);

View File

@ -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'});
});