11const extendJSConfig = require ( './extendJSConfig' )
22const stringifyJS = require ( './stringifyJS' )
3+ const { loadModule } = require ( './module' )
4+ const merge = require ( 'deepmerge' )
35
46function makeJSTransform ( filename ) {
5- return function transformToJS ( value , checkExisting , files ) {
7+ return function transformToJS ( value , checkExisting , files , context ) {
68 if ( checkExisting && files [ filename ] ) {
9+ // Merge data
10+ let changedData = { }
11+ try {
12+ const originalData = loadModule ( filename , context , true )
13+ // We merge only the modified keys
14+ Object . keys ( value ) . forEach ( key => {
15+ changedData [ key ] = merge ( originalData [ key ] , value [ key ] )
16+ } )
17+ } catch ( e ) {
18+ changedData = value
19+ }
20+ // Write
721 return {
822 filename,
9- content : extendJSConfig ( value , files [ filename ] )
23+ content : extendJSConfig ( changedData , files [ filename ] )
1024 }
1125 } else {
1226 return {
@@ -23,7 +37,7 @@ function makeJSONTransform (filename) {
2337 if ( checkExisting && files [ filename ] ) {
2438 existing = JSON . parse ( files [ filename ] )
2539 }
26- value = Object . assign ( existing , value )
40+ value = merge ( existing , value )
2741 return {
2842 filename,
2943 content : JSON . stringify ( value , null , 2 )
@@ -32,10 +46,10 @@ function makeJSONTransform (filename) {
3246}
3347
3448function makeMutliExtensionJSONTransform ( filename , preferJS ) {
35- return function transformToMultiExtensions ( value , checkExisting , files ) {
49+ return function transformToMultiExtensions ( value , checkExisting , files , context ) {
3650 function defaultTransform ( ) {
3751 if ( preferJS ) {
38- return makeJSTransform ( `${ filename } .js` ) ( value , false , files )
52+ return makeJSTransform ( `${ filename } .js` ) ( value , false , files , context )
3953 } else {
4054 return makeJSONTransform ( filename ) ( value , false , files )
4155 }
@@ -50,7 +64,7 @@ function makeMutliExtensionJSONTransform (filename, preferJS) {
5064 } else if ( files [ `${ filename } .json` ] ) {
5165 return makeJSONTransform ( `${ filename } .json` ) ( value , checkExisting , files )
5266 } else if ( files [ `${ filename } .js` ] ) {
53- return makeJSTransform ( `${ filename } .js` ) ( value , checkExisting , files )
67+ return makeJSTransform ( `${ filename } .js` ) ( value , checkExisting , files , context )
5468 } else if ( files [ `${ filename } .yaml` ] ) {
5569 return transformYAML ( value , `${ filename } .yaml` , files [ `${ filename } .yaml` ] )
5670 } else if ( files [ `${ filename } .yml` ] ) {
@@ -66,7 +80,7 @@ function transformYAML (value, filename, source) {
6680 const existing = yaml . safeLoad ( source )
6781 return {
6882 filename,
69- content : yaml . safeDump ( Object . assign ( existing , value ) )
83+ content : yaml . safeDump ( merge ( existing , value ) )
7084 }
7185}
7286
0 commit comments