-
Notifications
You must be signed in to change notification settings - Fork 58
/
user.js
90 lines (71 loc) · 1.73 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict'
const _ = require('lodash')
const Joi = require('@hapi/joi')
const { promisify } = require('es6-promisify')
const utils = require('../utils')
const hookSchema = Joi.object().keys({
pre: Joi.func(),
post: Joi.func()
}).unknown(false)
const envSchema = Joi.object().keys({
browser: hookSchema,
node: hookSchema
}).unknown(false)
const HOOK_ENVS = [
'browser',
'node'
]
const HOOK_STAGES = [
'pre',
'post'
]
function promisifyHooks (hooks) {
Object.keys(hooks).forEach((key) => {
if (hooks[key].length) {
// hook takes args, is expecting a callback so promisify it
hooks[key] = promisify(hooks[key])
}
})
return hooks
}
function normalizeHooks (hooks) {
const keys = Object.keys(hooks)
// no hooks provided
if (keys.length === 0) {
return hooks
}
// same hooks for all envs
if (_.every(keys, (k) => _.includes(HOOK_STAGES, k))) {
const v = promisifyHooks(Joi.attempt(hooks, hookSchema))
const res = {}
HOOK_ENVS.forEach((env) => {
res[env] = v
})
return res
}
// regular per env hook specification
if (_.every(keys, (k) => _.includes(HOOK_ENVS, k))) {
const res = Joi.attempt(hooks, envSchema)
keys.forEach((key) => {
res[key] = promisifyHooks(res[key])
})
return res
}
throw new Error(`Found unknown keys in hook definiton: "${keys.join(' ')}"`)
}
function userConfig () {
const config = utils.getUserConfig()
const user = _.defaultsDeep({}, config, {
webpack: {},
karma: {},
hooks: {},
entry: utils.fromRoot('src', 'index.js'),
bundlesize: {
path: './dist/index.min.js',
maxSize: '100kB'
}
})
user.hooks = normalizeHooks(user.hooks)
return user
}
module.exports = userConfig