1
1
const extendJSConfig = require ( './extendJSConfig' )
2
2
const stringifyJS = require ( './stringifyJS' )
3
+ const { loadModule } = require ( './module' )
4
+ const merge = require ( 'deepmerge' )
3
5
4
6
function makeJSTransform ( filename ) {
5
- return function transformToJS ( value , checkExisting , files ) {
7
+ return function transformToJS ( value , checkExisting , files , context ) {
6
8
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
7
21
return {
8
22
filename,
9
- content : extendJSConfig ( value , files [ filename ] )
23
+ content : extendJSConfig ( changedData , files [ filename ] )
10
24
}
11
25
} else {
12
26
return {
@@ -23,7 +37,7 @@ function makeJSONTransform (filename) {
23
37
if ( checkExisting && files [ filename ] ) {
24
38
existing = JSON . parse ( files [ filename ] )
25
39
}
26
- value = Object . assign ( existing , value )
40
+ value = merge ( existing , value )
27
41
return {
28
42
filename,
29
43
content : JSON . stringify ( value , null , 2 )
@@ -32,10 +46,10 @@ function makeJSONTransform (filename) {
32
46
}
33
47
34
48
function makeMutliExtensionJSONTransform ( filename , preferJS ) {
35
- return function transformToMultiExtensions ( value , checkExisting , files ) {
49
+ return function transformToMultiExtensions ( value , checkExisting , files , context ) {
36
50
function defaultTransform ( ) {
37
51
if ( preferJS ) {
38
- return makeJSTransform ( `${ filename } .js` ) ( value , false , files )
52
+ return makeJSTransform ( `${ filename } .js` ) ( value , false , files , context )
39
53
} else {
40
54
return makeJSONTransform ( filename ) ( value , false , files )
41
55
}
@@ -50,7 +64,7 @@ function makeMutliExtensionJSONTransform (filename, preferJS) {
50
64
} else if ( files [ `${ filename } .json` ] ) {
51
65
return makeJSONTransform ( `${ filename } .json` ) ( value , checkExisting , files )
52
66
} else if ( files [ `${ filename } .js` ] ) {
53
- return makeJSTransform ( `${ filename } .js` ) ( value , checkExisting , files )
67
+ return makeJSTransform ( `${ filename } .js` ) ( value , checkExisting , files , context )
54
68
} else if ( files [ `${ filename } .yaml` ] ) {
55
69
return transformYAML ( value , `${ filename } .yaml` , files [ `${ filename } .yaml` ] )
56
70
} else if ( files [ `${ filename } .yml` ] ) {
@@ -66,7 +80,7 @@ function transformYAML (value, filename, source) {
66
80
const existing = yaml . safeLoad ( source )
67
81
return {
68
82
filename,
69
- content : yaml . safeDump ( Object . assign ( existing , value ) )
83
+ content : yaml . safeDump ( merge ( existing , value ) )
70
84
}
71
85
}
72
86
0 commit comments