From ed9c456f5ed8e50ed5f4cce3f680e6e27128bbd2 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Fri, 6 Jul 2018 23:39:58 -0400 Subject: [PATCH] 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. --- index.js | 15 ++++++--------- lib/plugins/pipeline.js | 4 ++-- test/plugins/pipeline.test.js | 20 +------------------- test/plugins/plugins.test.js | 2 +- 4 files changed, 10 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index e5395990..4b9c1f97 100644 --- a/index.js +++ b/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'); @@ -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); } diff --git a/lib/plugins/pipeline.js b/lib/plugins/pipeline.js index 69bc9d37..8e731d20 100644 --- a/lib/plugins/pipeline.js +++ b/lib/plugins/pipeline.js @@ -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; }; diff --git a/test/plugins/pipeline.test.js b/test/plugins/pipeline.test.js index 97cfc4be..da335059 100644 --- a/test/plugins/pipeline.test.js +++ b/test/plugins/pipeline.test.js @@ -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); diff --git a/test/plugins/plugins.test.js b/test/plugins/plugins.test.js index f67367d4..945da15e 100644 --- a/test/plugins/plugins.test.js +++ b/test/plugins/plugins.test.js @@ -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'}); });