@@ -14,7 +14,7 @@ module.exports = class Processor {
1414 this . files = [ ] ;
1515 console . log ( chalk . yellow ( `>>> PROCESSING FILES` ) ) ;
1616
17- this . config . forEach ( config => {
17+ this . config . forEach ( ( config ) => {
1818 let file = this . processFile ( config ) ;
1919
2020 this . files . push ( file ) ;
@@ -28,7 +28,7 @@ module.exports = class Processor {
2828 this . files . forEach ( file => {
2929 console . log ( chalk . green ( `>>>>> ${ file . getOutputPath ( ) } ` ) ) ;
3030
31- fs . writeFile ( file . getOutputPath ( ) , JSON . stringify ( file . getContent ( ) , null , 2 ) , 'UTF-8' )
31+ fs . writeFile ( file . getOutputPath ( ) , JSON . stringify ( file . getContent ( ) , null , 2 ) , 'UTF-8' ) ;
3232 } ) ;
3333 }
3434
@@ -37,19 +37,19 @@ module.exports = class Processor {
3737 *
3838 * @returns {File }
3939 */
40- processFile ( config ) {
40+ processFile ( { source , output , envMap , skipUndefined } ) {
4141 const file = new File ( ) ;
4242
43- const pathSource = this . resolvePath ( config . source ) ;
44- const pathOutput = this . resolvePath ( config . output ) ;
43+ const pathSource = this . resolvePath ( source ) ;
44+ const pathOutput = this . resolvePath ( output ) ;
4545
4646 const packageJsonPath = this . resolvePath ( pathSource ) ;
4747 const packageJsonContent = fs . readFileSync ( packageJsonPath ) ;
4848
4949 /** @param {{extra: {}} } content */
5050 const packageJson = JSON . parse ( packageJsonContent ) ;
51- const solvedJson = this . resolveOverwritten ( config . envMap ) ;
52- const completedJson = this . constructor . getMergedData ( packageJson , solvedJson ) ;
51+ const solvedJson = Processor . resolveOverwritten ( envMap , skipUndefined ) ;
52+ const completedJson = deepmerge ( packageJson , solvedJson ) ;
5353
5454 file . setSourcePath ( pathSource )
5555 . setOutputPath ( pathOutput )
@@ -69,24 +69,20 @@ module.exports = class Processor {
6969 return `${ this . cwd } /${ path } ` ;
7070 }
7171
72- resolveOverwritten ( envMapping ) {
73- const object = { } ;
72+ static resolveOverwritten ( envMap , skipUndefined ) {
73+ const obj = { } ;
7474
75- Object . keys ( envMapping ) . forEach ( abstractPath => {
76- const envVariable = envMapping [ abstractPath ] ;
77- const value = this . constructor . getEnvironmentValue ( envVariable ) ;
75+ Object . keys ( envMap ) . forEach ( ( abstractPath ) => {
76+ const envVariable = envMap [ abstractPath ] ;
77+ const value = Processor . getEnvironmentValue ( envVariable ) ;
7878
79- undefined !== value && overwriteFieldValue ( abstractPath , value , object ) ;
79+ ( undefined !== value || ! skipUndefined ) && overwriteFieldValue ( abstractPath , value , obj ) ;
8080 } ) ;
8181
82- return object ;
82+ return obj ;
8383 }
8484
85- static getEnvironmentValue ( index ) {
86- return process . env [ index ] || undefined ;
87- }
88-
89- static getMergedData ( data , overwrittenData ) {
90- return deepmerge ( data , overwrittenData ) ;
85+ static getEnvironmentValue ( i ) {
86+ return process . env [ i ] || undefined ;
9187 }
9288} ;
0 commit comments