Skip to content

Commit

Permalink
feat: support async hooks
Browse files Browse the repository at this point in the history
A common use of hooks in aegir is to start and stop an ipfs node for
browser tests to run against.

Since they all use ipfsd-ctl and that is moving to async/await in
ipfs/js-ipfsd-ctl#353 as part of ipfs/js-ipfs#1670, it would be
nice to not have to mix async and callbacks in the hooks.

This PR adds support for async hooks while not breaking existing
callback based ones.
  • Loading branch information
achingbrain committed Jul 5, 2019
1 parent 1026032 commit 6360037
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const HOOK_STAGES = [

function promisifyHooks (hooks) {
Object.keys(hooks).forEach((key) => {
hooks[key] = promisify(hooks[key])
if (hooks[key].length) {
// hook takes args, is expecting a callback so promisify it
hooks[key] = promisify(hooks[key])
}
})

return hooks
Expand Down
57 changes: 57 additions & 0 deletions test/config/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,60 @@ describe('config - user', () => {
})
})
})

describe('config - user with async hooks', () => {
let config

before(() => {
mock('../../src/utils', {
getPkg () {
return Promise.resolve({
name: 'example'
})
},
getUserConfig () {
return {
webpack: {
devtool: 'eval'
},
entry: 'src/main.js',
hooks: {
async pre () {
await Promise.resolve()

return 'pre done async'
},
async post () {
await Promise.resolve()

return 'post done async'
}
}
}
},
getLibraryName () {
return 'Example'
},
getPathToDist () {
return 'dist'
},
getPathToNodeModules () {
return 'aegir/node_modules'
},
fromRoot () {
return './src/index.js'
}
})

config = mock.reRequire('../../src/config/user')()
})

after(() => {
mock.stop('../../src/utils.js')
})

it('supports async hooks', async () => {
expect(await config.hooks.browser.pre()).to.eql('pre done async')
expect(await config.hooks.browser.post()).to.eql('post done async')
})
})

0 comments on commit 6360037

Please sign in to comment.