Skip to content

Commit 6360037

Browse files
committed
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.
1 parent 1026032 commit 6360037

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/config/user.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ const HOOK_STAGES = [
2727

2828
function promisifyHooks (hooks) {
2929
Object.keys(hooks).forEach((key) => {
30-
hooks[key] = promisify(hooks[key])
30+
if (hooks[key].length) {
31+
// hook takes args, is expecting a callback so promisify it
32+
hooks[key] = promisify(hooks[key])
33+
}
3134
})
3235

3336
return hooks

test/config/user.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,60 @@ describe('config - user', () => {
6767
})
6868
})
6969
})
70+
71+
describe('config - user with async hooks', () => {
72+
let config
73+
74+
before(() => {
75+
mock('../../src/utils', {
76+
getPkg () {
77+
return Promise.resolve({
78+
name: 'example'
79+
})
80+
},
81+
getUserConfig () {
82+
return {
83+
webpack: {
84+
devtool: 'eval'
85+
},
86+
entry: 'src/main.js',
87+
hooks: {
88+
async pre () {
89+
await Promise.resolve()
90+
91+
return 'pre done async'
92+
},
93+
async post () {
94+
await Promise.resolve()
95+
96+
return 'post done async'
97+
}
98+
}
99+
}
100+
},
101+
getLibraryName () {
102+
return 'Example'
103+
},
104+
getPathToDist () {
105+
return 'dist'
106+
},
107+
getPathToNodeModules () {
108+
return 'aegir/node_modules'
109+
},
110+
fromRoot () {
111+
return './src/index.js'
112+
}
113+
})
114+
115+
config = mock.reRequire('../../src/config/user')()
116+
})
117+
118+
after(() => {
119+
mock.stop('../../src/utils.js')
120+
})
121+
122+
it('supports async hooks', async () => {
123+
expect(await config.hooks.browser.pre()).to.eql('pre done async')
124+
expect(await config.hooks.browser.post()).to.eql('post done async')
125+
})
126+
})

0 commit comments

Comments
 (0)