-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement hooks and fix some bugs
- Loading branch information
1 parent
07d3554
commit e0f716c
Showing
14 changed files
with
386 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,83 @@ | ||
'use strict' | ||
|
||
const _ = require('lodash') | ||
const Joi = require('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) => { | ||
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() | ||
|
||
return _.defaultsDeep({}, config, { | ||
const user = _.defaultsDeep({}, config, { | ||
webpack: {}, | ||
karma: {}, | ||
hooks: {}, | ||
entry: 'src/index.js' | ||
}) | ||
|
||
user.hooks = normalizeHooks(user.hooks) | ||
|
||
return user | ||
} | ||
|
||
module.exports = userConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
config: 'mine' | ||
} |
Oops, something went wrong.