Skip to content

Commit

Permalink
fix: better warnings for missing $map functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Oct 23, 2020
1 parent a7ed34a commit 7580cb7
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,22 @@ function deepAssign(target, ...sources) {
return target
}

async function keysAsFunctionsRecursive(obj, suffix = '$map') {

/** *
* @param {object} obj the config
* @param {string} suffix defaults to '$map'
* recursively converts
* {
* myPlugins: ['hello'],
* myPlugins$map: { aPlugin: str => `${str}bar` },
* myPlugins$options: { aPlugin: 'foo', _n: 123 }
* }
* to
* {
* myPlugins: ['hello', 'foobar', 123]
* }
*/
async function keysAsFunctionsRecursive(obj, suffix = '$map', breadcrumbs = []) {
const maps = []

// process all descendants' maps first
Expand All @@ -51,7 +66,7 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
if (mapKey.endsWith(suffix))
maps.push({ mapKey, value })
else
return keysAsFunctionsRecursive(value, suffix)
return keysAsFunctionsRecursive(value, suffix, [...breadcrumbs, mapKey])
}
})
await Promise.all(children)
Expand All @@ -60,10 +75,13 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
await Promise.all(maps.map(async ({ mapKey, value }) => {
const key = mapKey.substr(0, mapKey.length - suffix.length)
const optionsKey = `${key}$options`
const result = await keysAsFunctions(obj[optionsKey], value, key)
const result = await keysAsFunctions(obj[optionsKey], value, breadcrumbs)
delete obj[mapKey]
delete obj[optionsKey]
obj[key] = result

// make sure the array exists
obj[key] = obj[key] || []
obj[key].push(...result)
}))

return obj
Expand All @@ -78,7 +96,9 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
* @param {object} obj
* @param {Object.<string, function>} map
*/
async function keysAsFunctions(obj, map, name) {
async function keysAsFunctions(obj, map, breadcrumbs) {
const name = breadcrumbs.join('.')

if (!isObject(obj))
throw new Error(`expected an object for "${name}", but got ${JSON.stringify(obj, null, 2)}`)
const { log } = require('./log')
Expand All @@ -87,11 +107,11 @@ async function keysAsFunctions(obj, map, name) {
const fnName = key.split('.')[0]
const fn = map[fnName]

if (!fn && isFn) log.info(
`there's no map method named ${key}. Available methods: ${Object.keys(map)}.` +
`\nRenaming to "_${key}" will hide this message.` +
`\nvalue: ${JSON.stringify(value, null, 2)}` +
`\nobj: ${JSON.stringify(obj, null, 2)}`
if (!fn && isFn) log.warn(
`--${name}$map-- does not have a method called --${key}--. Available methods: --${Object.keys(map)}--.` +
`\nRenaming to --${key}-- or pushing the value of --${key}-- to --${name}-- will hide this message.`
// `\nvalue: ${JSON.stringify(value, null, 2)}` +
// `\nobj: ${JSON.stringify(obj, null, 2)}`
)
return fn && value ? fn(value) : value
})
Expand Down Expand Up @@ -135,20 +155,20 @@ function sortHooks(hooks) {
|| obstacleOrderings.find(order => order.before === pluginName)
)
return true
})

if (obstacle) {
obstacles.push({
plugin: hook.plugin.name,
obstacle: obstacle.plugin.name
})
} else {
sortedHooks.push(hooks.splice(index, 1)[0])
break;

if (obstacle) {
obstacles.push({
plugin: hook.plugin.name,
obstacle: obstacle.plugin.name
})
} else {
sortedHooks.push(hooks.splice(index, 1)[0])
break;
}
}
}
}
return sortedHooks
return sortedHooks
}


Expand Down

0 comments on commit 7580cb7

Please sign in to comment.