forked from ipfs/aegir
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: global config and ghtoken support
- Loading branch information
Showing
3 changed files
with
137 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'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 globalConfig () { | ||
const config = utils.getGlobalConfig() | ||
|
||
const global = _.defaultsDeep({}, config, { | ||
hooks: {}, | ||
release: {} | ||
}) | ||
|
||
global.hooks = normalizeHooks(global.hooks) | ||
|
||
return global | ||
} | ||
|
||
module.exports = globalConfig |
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