@@ -42,7 +42,22 @@ function deepAssign(target, ...sources) {
42
42
return target
43
43
}
44
44
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 = [ ] ) {
46
61
const maps = [ ]
47
62
48
63
// process all descendants' maps first
@@ -51,7 +66,7 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
51
66
if ( mapKey . endsWith ( suffix ) )
52
67
maps . push ( { mapKey, value } )
53
68
else
54
- return keysAsFunctionsRecursive ( value , suffix )
69
+ return keysAsFunctionsRecursive ( value , suffix , [ ... breadcrumbs , mapKey ] )
55
70
}
56
71
} )
57
72
await Promise . all ( children )
@@ -60,10 +75,13 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
60
75
await Promise . all ( maps . map ( async ( { mapKey, value } ) => {
61
76
const key = mapKey . substr ( 0 , mapKey . length - suffix . length )
62
77
const optionsKey = `${ key } $options`
63
- const result = await keysAsFunctions ( obj [ optionsKey ] , value , key )
78
+ const result = await keysAsFunctions ( obj [ optionsKey ] , value , breadcrumbs )
64
79
delete obj [ mapKey ]
65
80
delete obj [ optionsKey ]
66
- obj [ key ] = result
81
+
82
+ // make sure the array exists
83
+ obj [ key ] = obj [ key ] || [ ]
84
+ obj [ key ] . push ( ...result )
67
85
} ) )
68
86
69
87
return obj
@@ -78,7 +96,9 @@ async function keysAsFunctionsRecursive(obj, suffix = '$map') {
78
96
* @param {object } obj
79
97
* @param {Object.<string, function> } map
80
98
*/
81
- async function keysAsFunctions ( obj , map , name ) {
99
+ async function keysAsFunctions ( obj , map , breadcrumbs ) {
100
+ const name = breadcrumbs . join ( '.' )
101
+
82
102
if ( ! isObject ( obj ) )
83
103
throw new Error ( `expected an object for "${ name } ", but got ${ JSON . stringify ( obj , null , 2 ) } ` )
84
104
const { log } = require ( './log' )
@@ -87,11 +107,11 @@ async function keysAsFunctions(obj, map, name) {
87
107
const fnName = key . split ( '.' ) [ 0 ]
88
108
const fn = map [ fnName ]
89
109
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)}`
95
115
)
96
116
return fn && value ? fn ( value ) : value
97
117
} )
@@ -135,20 +155,20 @@ function sortHooks(hooks) {
135
155
|| obstacleOrderings . find ( order => order . before === pluginName )
136
156
)
137
157
return true
138
- } )
139
-
140
- if ( obstacle ) {
141
- obstacles . push ( {
142
- plugin : hook . plugin . name ,
143
- obstacle : obstacle . plugin . name
144
158
} )
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
+ }
148
169
}
149
170
}
150
- }
151
- return sortedHooks
171
+ return sortedHooks
152
172
}
153
173
154
174
0 commit comments