11const ejs = require ( 'ejs' )
22const slash = require ( 'slash' )
33const debug = require ( 'debug' )
4+ const configMap = require ( './util/configMap' )
45const GeneratorAPI = require ( './GeneratorAPI' )
56const sortObject = require ( './util/sortObject' )
67const writeFileTree = require ( './util/writeFileTree' )
78
89module . exports = class Generator {
9- constructor ( context , pkg , plugins , completeCbs = [ ] ) {
10+ constructor ( context , pkg , plugins , extractConfigFiles , completeCbs = [ ] ) {
1011 this . context = context
1112 this . plugins = plugins
1213 this . pkg = pkg
@@ -26,6 +27,11 @@ module.exports = class Generator {
2627 const api = new GeneratorAPI ( id , this , options , rootOptions || { } )
2728 apply ( api , options , rootOptions )
2829 } )
30+ // if the user has chosen so, extract configs from package.json into
31+ // dedicated files.
32+ if ( extractConfigFiles ) {
33+ this . extractConfigFiles ( )
34+ }
2935 }
3036
3137 async generate ( ) {
@@ -38,6 +44,17 @@ module.exports = class Generator {
3844 await writeFileTree ( this . context , this . files )
3945 }
4046
47+ extractConfigFiles ( ) {
48+ for ( const key in this . pkg ) {
49+ if ( configMap [ key ] ) {
50+ const value = this . pkg [ key ]
51+ const { transform, filename } = configMap [ key ]
52+ this . files [ filename ] = transform ( value )
53+ delete this . pkg [ key ]
54+ }
55+ }
56+ }
57+
4158 sortPkg ( ) {
4259 // ensure package.json keys has readable order
4360 this . pkg . dependencies = sortObject ( this . pkg . dependencies )
0 commit comments