Skip to content

Commit 7580cb7

Browse files
fix: better warnings for missing $map functions
1 parent a7ed34a commit 7580cb7

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

lib/utils/index.js

+41-21
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,22 @@ function deepAssign(target, ...sources) {
4242
return target
4343
}
4444

45-
async function keysAsFunctionsRecursive(obj, suffix = '$map') {
45+
46+
/** *
47+
* @param {object} obj the config
48+
* @param {string} suffix defaults to '$map'
49+
* recursively converts
50+
* {
51+
* myPlugins: ['hello'],
52+
* myPlugins$map: { aPlugin: str => `${str}bar` },
53+
* myPlugins$options: { aPlugin: 'foo', _n: 123 }
54+
* }
55+
* to
56+
* {
57+
* myPlugins: ['hello', 'foobar', 123]
58+
* }
59+
*/
60+
async function keysAsFunctionsRecursive(obj, suffix = '$map', breadcrumbs = []) {
4661
const maps = []
4762

4863
// process all descendants' maps first
@@ -51,7 +66,7 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
5166
if (mapKey.endsWith(suffix))
5267
maps.push({ mapKey, value })
5368
else
54-
return keysAsFunctionsRecursive(value, suffix)
69+
return keysAsFunctionsRecursive(value, suffix, [...breadcrumbs, mapKey])
5570
}
5671
})
5772
await Promise.all(children)
@@ -60,10 +75,13 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
6075
await Promise.all(maps.map(async ({ mapKey, value }) => {
6176
const key = mapKey.substr(0, mapKey.length - suffix.length)
6277
const optionsKey = `${key}$options`
63-
const result = await keysAsFunctions(obj[optionsKey], value, key)
78+
const result = await keysAsFunctions(obj[optionsKey], value, breadcrumbs)
6479
delete obj[mapKey]
6580
delete obj[optionsKey]
66-
obj[key] = result
81+
82+
// make sure the array exists
83+
obj[key] = obj[key] || []
84+
obj[key].push(...result)
6785
}))
6886

6987
return obj
@@ -78,7 +96,9 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
7896
* @param {object} obj
7997
* @param {Object.<string, function>} map
8098
*/
81-
async function keysAsFunctions(obj, map, name) {
99+
async function keysAsFunctions(obj, map, breadcrumbs) {
100+
const name = breadcrumbs.join('.')
101+
82102
if (!isObject(obj))
83103
throw new Error(`expected an object for "${name}", but got ${JSON.stringify(obj, null, 2)}`)
84104
const { log } = require('./log')
@@ -87,11 +107,11 @@ async function keysAsFunctions(obj, map, name) {
87107
const fnName = key.split('.')[0]
88108
const fn = map[fnName]
89109

90-
if (!fn && isFn) log.info(
91-
`there's no map method named ${key}. Available methods: ${Object.keys(map)}.` +
92-
`\nRenaming to "_${key}" will hide this message.` +
93-
`\nvalue: ${JSON.stringify(value, null, 2)}` +
94-
`\nobj: ${JSON.stringify(obj, null, 2)}`
110+
if (!fn && isFn) log.warn(
111+
`--${name}$map-- does not have a method called --${key}--. Available methods: --${Object.keys(map)}--.` +
112+
`\nRenaming to --${key}-- or pushing the value of --${key}-- to --${name}-- will hide this message.`
113+
// `\nvalue: ${JSON.stringify(value, null, 2)}` +
114+
// `\nobj: ${JSON.stringify(obj, null, 2)}`
95115
)
96116
return fn && value ? fn(value) : value
97117
})
@@ -135,20 +155,20 @@ function sortHooks(hooks) {
135155
|| obstacleOrderings.find(order => order.before === pluginName)
136156
)
137157
return true
138-
})
139-
140-
if (obstacle) {
141-
obstacles.push({
142-
plugin: hook.plugin.name,
143-
obstacle: obstacle.plugin.name
144158
})
145-
} else {
146-
sortedHooks.push(hooks.splice(index, 1)[0])
147-
break;
159+
160+
if (obstacle) {
161+
obstacles.push({
162+
plugin: hook.plugin.name,
163+
obstacle: obstacle.plugin.name
164+
})
165+
} else {
166+
sortedHooks.push(hooks.splice(index, 1)[0])
167+
break;
168+
}
148169
}
149170
}
150-
}
151-
return sortedHooks
171+
return sortedHooks
152172
}
153173

154174

0 commit comments

Comments
 (0)