From 63600378128eb70af42d564b5d2aa79339a5fe11 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 5 Jul 2019 10:36:34 +0100 Subject: [PATCH] feat: support async hooks 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. --- src/config/user.js | 5 +++- test/config/user.spec.js | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/config/user.js b/src/config/user.js index f8c1c3c71..ce51d55ad 100644 --- a/src/config/user.js +++ b/src/config/user.js @@ -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 diff --git a/test/config/user.spec.js b/test/config/user.spec.js index dda8b6bd5..d4d419bdb 100644 --- a/test/config/user.spec.js +++ b/test/config/user.spec.js @@ -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') + }) +})