@@ -30,7 +30,7 @@ export default function (plopfileApi, flags) {
30
30
} ) ;
31
31
32
32
// Run the actions for this generator
33
- const runGeneratorActions = co . wrap ( function * ( genObject , data , hooks = { } ) {
33
+ const runGeneratorActions = co . wrap ( function * ( genObject , data = { } , hooks = { } ) {
34
34
const noop = ( ) => { } ;
35
35
const {
36
36
onSuccess= noop , // runs after each successful action
@@ -111,28 +111,29 @@ export default function (plopfileApi, flags) {
111
111
const executeActionLogic = co . wrap ( function * ( action , cfg , data ) {
112
112
const type = cfg . type || '' ;
113
113
114
- // convert any returned data into a promise to
115
- // return and wait on
116
114
let cfgData = cfg . data || { } ;
117
115
// data can also be a function that returns a data object
118
116
if ( typeof cfgData === 'function' ) { cfgData = yield cfgData ( ) ; }
119
- Object . keys ( cfgData || { } ) . forEach ( k => {
120
- if ( typeof data [ k ] === 'undefined' || ( data [ k ] && cfgData [ k ] ) ) {
121
- data [ k ] = cfgData [ k ] ;
122
- }
123
- } ) ;
124
- return yield Promise . resolve ( action ( data , cfg , plopfileApi ) ) . then (
117
+
118
+ // track data properties that can be applied to the main data scope
119
+ const cfgDataProps = Object . keys ( cfgData ) . filter ( k => typeof data [ k ] === 'undefined' ) ;
120
+ // copy config data into main data scope so it's available for templates
121
+ cfgDataProps . forEach ( k => { data [ k ] = cfgData [ k ] ; } ) ;
122
+
123
+ return yield ( Promise . resolve ( action ( data , cfg , plopfileApi ) )
125
124
// show the resolved value in the console
126
- result => ( {
125
+ . then ( result => ( {
127
126
type, path : ( typeof result === 'string'
128
127
? result
129
128
: JSON . stringify ( result )
130
129
)
131
130
} ) ,
132
131
// a rejected promise is treated as a failure
133
- function ( err ) {
132
+ err => {
134
133
throw { type, path : '' , error : err . message || err . toString ( ) } ;
135
- }
134
+ } )
135
+ // cleanup main data scope so config data doesn't leak
136
+ . finally ( ( ) => cfgDataProps . forEach ( k => { delete data [ k ] ; } ) )
136
137
) ;
137
138
} ) ;
138
139
0 commit comments