Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
fix(hooks): fix undefined case when no plugins are installed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Garant committed Sep 15, 2019
1 parent 83fe630 commit 8685c24
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/cmds/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,8 @@ PullRequest.prototype.list = async function(user, repo) {
let options = instance.options
let json

await beforeHooks('pull-request.list', instance)

let sort = options.sort

if (options.sort === PullRequest.SORT_COMPLEXITY) {
Expand Down Expand Up @@ -765,6 +767,8 @@ PullRequest.prototype.list = async function(user, repo) {
logger.log('')
}
}

await afterHooks('pull-request.list', instance)
}

PullRequest.prototype.listFromAllRepositories = async function() {
Expand Down
28 changes: 19 additions & 9 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export async function afterHooks(path, scope) {
let context = createContext(scope)

if (!testing) {
context = await setupPlugins_(context, 'setupAfterHooks')
const pluginContext = await setupPlugins_(context, 'setupAfterHooks')

if (pluginContext) {
context = { ...context, ...pluginContext }
}
}

after.forEach(cmd => {
Expand All @@ -73,15 +77,19 @@ export async function beforeHooks(path, scope) {
let context = createContext(scope)

if (!testing) {
context = await setupPlugins_(context, 'setupBeforeHooks')
const pluginContext = await setupPlugins_(context, 'setupBeforeHooks')

if (pluginContext) {
context = { ...context, ...pluginContext }
}
}

before.forEach(cmd => {
wrapCommand_(cmd, context, 'before')
})
}

async function setupPlugins_(context, setupFn) {
async function setupPlugins_(context, setupFn): Promise<object> {
const plugins = configs.getPlugins()

const contextArr = await Promise.all(
Expand All @@ -99,13 +107,15 @@ async function setupPlugins_(context, setupFn) {
})
)

return contextArr.reduce((accum, curr) => {
if (accum) {
return { ...accum, ...curr }
}
return { ...curr }
}, {})
return contextArr.filter(plugin => plugin !== undefined).reduce(mergeArrayOfObjects, false)
}

function mergeArrayOfObjects(accumulatedObject, currentObject): object | boolean {
if (!currentObject) return accumulatedObject

return { ...accumulatedObject, ...currentObject }
}

export function wrapCommand_(cmd, context, when) {
const raw = logger.compileTemplate(cmd, context)

Expand Down

0 comments on commit 8685c24

Please sign in to comment.