@@ -9,6 +9,8 @@ const folders = require('./folders')
9
9
const prompts = require ( './prompts' )
10
10
// Utils
11
11
const { get, set, remove } = require ( '../../util/object' )
12
+ const { loadModule } = require ( '@vue/cli/lib/util/module' )
13
+ const extendJSConfig = require ( '@vue/cli/lib/util/extendJSConfig' )
12
14
13
15
const fileTypes = [ 'js' , 'json' , 'yaml' ]
14
16
let current = { }
@@ -56,24 +58,24 @@ function readData (config, context) {
56
58
if ( file . type === 'package' ) {
57
59
const pkg = folders . readPackage ( cwd . get ( ) , context )
58
60
return pkg [ config . files . package ]
61
+ } else if ( file . type === 'js' ) {
62
+ return loadModule ( file . path , cwd . get ( ) , true )
59
63
} else {
60
64
const rawContent = fs . readFileSync ( file . path , { encoding : 'utf8' } )
61
65
if ( file . type === 'json' ) {
62
66
return JSON . parse ( rawContent )
63
67
} else if ( file . type === 'yaml' ) {
64
68
return yaml . safeLoad ( rawContent )
65
- } else if ( file . type === 'js' ) {
66
- // TODO
67
- console . warn ( 'JS config read not implemented' )
68
69
}
69
70
}
70
71
}
71
72
return { }
72
73
}
73
74
74
- function writeData ( { config, data } , context ) {
75
+ function writeData ( { config, data, changedFields } , context ) {
75
76
const file = findFile ( config , context )
76
77
if ( file ) {
78
+ if ( process . env . NODE_ENV !== 'production' ) console . log ( 'write' , config . id , data , changedFields )
77
79
let rawContent
78
80
if ( file . type === 'package' ) {
79
81
const pkg = folders . readPackage ( cwd . get ( ) , context )
@@ -85,8 +87,12 @@ function writeData ({ config, data }, context) {
85
87
} else if ( file . type === 'yaml' ) {
86
88
rawContent = yaml . safeDump ( data )
87
89
} else if ( file . type === 'js' ) {
88
- // TODO
89
- console . warn ( 'JS config write not implemented' )
90
+ const changedData = changedFields . reduce ( ( obj , field ) => {
91
+ obj [ field ] = data [ field ]
92
+ return obj
93
+ } , { } )
94
+ const source = fs . readFileSync ( file . path , { encoding : 'utf8' } )
95
+ rawContent = extendJSConfig ( changedData , source )
90
96
}
91
97
}
92
98
fs . writeFileSync ( file . path , rawContent , { encoding : 'utf8' } )
@@ -97,6 +103,7 @@ async function getPrompts (id, context) {
97
103
const config = findOne ( id , context )
98
104
if ( config ) {
99
105
const data = readData ( config , context )
106
+ if ( process . env . NODE_ENV !== 'production' ) console . log ( 'read' , config . id , data )
100
107
current = {
101
108
config,
102
109
data
@@ -121,17 +128,26 @@ async function save (id, context) {
121
128
if ( current . config === config ) {
122
129
const answers = prompts . getAnswers ( )
123
130
let data = clone ( current . data )
131
+ const changedFields = [ ]
124
132
await config . onWrite ( {
125
133
prompts : prompts . list ( ) ,
126
134
answers,
127
- data,
135
+ data : current . data ,
128
136
file : config . file ,
129
137
api : {
130
138
assignData : newData => {
139
+ changedFields . push ( ...Object . keys ( newData ) )
131
140
Object . assign ( data , newData )
132
141
} ,
133
142
setData : newData => {
134
143
Object . keys ( newData ) . forEach ( key => {
144
+ let field = key
145
+ const dotIndex = key . indexOf ( '.' )
146
+ if ( dotIndex !== - 1 ) {
147
+ field = key . substr ( 0 , dotIndex )
148
+ }
149
+ changedFields . push ( field )
150
+
135
151
const value = newData [ key ]
136
152
if ( typeof value === 'undefined' ) {
137
153
remove ( data , key )
@@ -155,7 +171,7 @@ async function save (id, context) {
155
171
}
156
172
}
157
173
} )
158
- writeData ( { config, data } , context )
174
+ writeData ( { config, data, changedFields } , context )
159
175
current = { }
160
176
}
161
177
}
0 commit comments