1
- const fs = require ( 'fs' )
2
- const path = require ( 'path' )
3
1
const extendJSConfig = require ( './extendJSConfig' )
4
2
const stringifyJS = require ( './stringifyJS' )
5
3
6
4
function makeJSTransform ( filename ) {
7
- return function transformToJS ( value , checkExisting , context ) {
8
- const absolutePath = path . resolve ( context , filename )
9
- if ( checkExisting && fs . existsSync ( absolutePath ) ) {
5
+ return function transformToJS ( value , checkExisting , files ) {
6
+ if ( checkExisting && files [ filename ] ) {
10
7
return {
11
8
filename,
12
- content : extendJSConfig ( value , fs . readFileSync ( absolutePath , 'utf-8' ) )
9
+ content : extendJSConfig ( value , files [ filename ] )
13
10
}
14
11
} else {
15
12
return {
@@ -21,11 +18,10 @@ function makeJSTransform (filename) {
21
18
}
22
19
23
20
function makeJSONTransform ( filename ) {
24
- return function transformToJSON ( value , checkExisting , context ) {
21
+ return function transformToJSON ( value , checkExisting , files ) {
25
22
let existing = { }
26
- const absolutePath = path . resolve ( context , filename )
27
- if ( checkExisting && fs . existsSync ( absolutePath ) ) {
28
- existing = JSON . parse ( fs . readFileSync ( absolutePath , 'utf-8' ) )
23
+ if ( checkExisting && files [ filename ] ) {
24
+ existing = JSON . parse ( files [ filename ] )
29
25
}
30
26
value = Object . assign ( existing , value )
31
27
return {
@@ -36,30 +32,29 @@ function makeJSONTransform (filename) {
36
32
}
37
33
38
34
function makeMutliExtensionJSONTransform ( filename , preferJS ) {
39
- return function transformToMultiExtensions ( value , checkExisting , context ) {
35
+ return function transformToMultiExtensions ( value , checkExisting , files ) {
40
36
function defaultTransform ( ) {
41
37
if ( preferJS ) {
42
- return makeJSTransform ( `${ filename } .js` ) ( value , false , context )
38
+ return makeJSTransform ( `${ filename } .js` ) ( value , false , files )
43
39
} else {
44
- return makeJSONTransform ( filename ) ( value , false , context )
40
+ return makeJSONTransform ( filename ) ( value , false , files )
45
41
}
46
42
}
47
43
48
44
if ( ! checkExisting ) {
49
45
return defaultTransform ( )
50
46
}
51
47
52
- const absolutePath = path . resolve ( context , filename )
53
- if ( fs . existsSync ( absolutePath ) ) {
54
- return makeJSONTransform ( filename ) ( value , checkExisting , context )
55
- } else if ( fs . existsSync ( `${ absolutePath } .json` ) ) {
56
- return makeJSONTransform ( `${ filename } .json` ) ( value , checkExisting , context )
57
- } else if ( fs . existsSync ( `${ absolutePath } .js` ) ) {
58
- return makeJSTransform ( `${ filename } .js` ) ( value , checkExisting , context )
59
- } else if ( fs . existsSync ( `${ absolutePath } .yaml` ) ) {
60
- return transformYAML ( value , `${ filename } .yaml` , fs . readFileSync ( `${ absolutePath } .yaml` , 'utf-8' ) )
61
- } else if ( fs . existsSync ( `${ absolutePath } .yml` ) ) {
62
- return transformYAML ( value , `${ filename } .yml` , fs . readFileSync ( `${ absolutePath } .yml` , 'utf-8' ) )
48
+ if ( files [ filename ] ) {
49
+ return makeJSONTransform ( filename ) ( value , checkExisting , files )
50
+ } else if ( files [ `${ filename } .json` ] ) {
51
+ return makeJSONTransform ( `${ filename } .json` ) ( value , checkExisting , files )
52
+ } else if ( files [ `${ filename } .js` ] ) {
53
+ return makeJSTransform ( `${ filename } .js` ) ( value , checkExisting , files )
54
+ } else if ( files [ `${ filename } .yaml` ] ) {
55
+ return transformYAML ( value , `${ filename } .yaml` , files [ `${ filename } .yaml` ] )
56
+ } else if ( files [ `${ filename } .yml` ] ) {
57
+ return transformYAML ( value , `${ filename } .yml` , files [ `${ filename } .yml` ] )
63
58
} else {
64
59
return defaultTransform ( )
65
60
}
0 commit comments