@@ -3,10 +3,10 @@ import { v4 as uuid } from 'uuid';
33import { existsSync , unlinkSync , readFileSync } from 'fs' ;
44
55const ENC_KEY = process . env . ENC_KEY || 'encryptionKey' ;
6- const ENCRYPT_CONF : boolean = process . env . ENCRYPT_CONF === 'true' || false ;
6+ const ENCRYPT_CONF : boolean = process . env . ENCRYPT_CONF === 'true' || true ;
77const CONFIG_NAME = process . env . CONFIG_NAME || 'contentstack_cli' ;
88const ENC_CONFIG_NAME = process . env . ENC_CONFIG_NAME || 'contentstack_cli_obfuscate' ;
9- const OLD_CONFIG_BACKUP_FLAG = 'isOldConfigBackup'
9+ const OLD_CONFIG_BACKUP_FLAG = 'isOldConfigBackup' ;
1010
1111const xdgBasedir = require ( 'xdg-basedir' ) ;
1212const path = require ( 'path' ) ;
@@ -20,50 +20,47 @@ class Config {
2020 private config : Conf ;
2121
2222 constructor ( ) {
23- this . init ( )
24- this . importOldConfig ( )
23+ this . init ( ) ;
24+ this . importOldConfig ( ) ;
2525 }
2626
2727 init ( ) {
28- return ENCRYPT_CONF === true
29- ? this . getEncryptedConfig ( )
30- : this . getDecryptedConfig ( )
31-
28+ return ENCRYPT_CONF === true ? this . getEncryptedConfig ( ) : this . getDecryptedConfig ( ) ;
3229 }
3330
3431 importOldConfig ( ) {
3532 if ( ! this . get ( OLD_CONFIG_BACKUP_FLAG ) ) {
3633 try {
37- const oldConfigStoreData = this . getOldConfig ( )
34+ const oldConfigStoreData = this . getOldConfig ( ) ;
3835 if ( oldConfigStoreData ) {
39- this . setOldConfigStoreData ( oldConfigStoreData , '' )
40- this . removeOldConfigStoreFile ( )
36+ this . setOldConfigStoreData ( oldConfigStoreData , '' ) ;
37+ this . removeOldConfigStoreFile ( ) ;
4138 }
4239 } catch ( error ) {
43- console . log ( " No data to be imported from Old config file" ) ;
40+ console . log ( ' No data to be imported from Old config file' ) ;
4441 }
4542
46- this . set ( OLD_CONFIG_BACKUP_FLAG , true )
43+ this . set ( OLD_CONFIG_BACKUP_FLAG , true ) ;
4744 }
4845 }
4946
5047 // Recursive function to migrate from the old config
5148 setOldConfigStoreData ( data , _path = '' ) {
5249 for ( const key in data ) {
5350 const value = data [ key ] ;
54- const setPath = _path ? _path + '.' + key : key
51+ const setPath = _path ? _path + '.' + key : key ;
5552
56- if ( typeof ( value ) == " object" ) {
57- this . setOldConfigStoreData ( value , setPath )
53+ if ( typeof value == ' object' ) {
54+ this . setOldConfigStoreData ( value , setPath ) ;
5855 } else {
59- this . set ( setPath , value )
56+ this . set ( setPath , value ) ;
6057 }
6158 }
6259 }
6360
6461 removeOldConfigStoreFile ( ) {
6562 if ( existsSync ( oldConfigPath ) ) {
66- unlinkSync ( oldConfigPath ) // NOTE remove old configstore file
63+ unlinkSync ( oldConfigPath ) ; // NOTE remove old configstore file
6764 }
6865 }
6966
@@ -76,103 +73,103 @@ class Config {
7673 }
7774
7875 private fallbackInit ( ) : Conf {
79- return new Conf ( { configName : CONFIG_NAME , encryptionKey : ENC_KEY } )
76+ return new Conf ( { configName : CONFIG_NAME , encryptionKey : ENC_KEY } ) ;
8077 }
8178
8279 private getObfuscationKey ( ) {
83- const obfuscationKeyName = 'obfuscation_key'
84- const encConfig = new Conf ( { configName : ENC_CONFIG_NAME } )
85- let obfuscationKey : any = encConfig . get ( obfuscationKeyName )
80+ const obfuscationKeyName = 'obfuscation_key' ;
81+ const encConfig = new Conf ( { configName : ENC_CONFIG_NAME } ) ;
82+ let obfuscationKey : any = encConfig . get ( obfuscationKeyName ) ;
8683
8784 if ( ! obfuscationKey ) {
88- encConfig . set ( obfuscationKeyName , uuid ( ) )
89- obfuscationKey = encConfig . get ( obfuscationKeyName )
85+ encConfig . set ( obfuscationKeyName , uuid ( ) ) ;
86+ obfuscationKey = encConfig . get ( obfuscationKeyName ) ;
9087 }
9188
92- return obfuscationKey
89+ return obfuscationKey ;
9390 }
9491
9592 private getConfigDataAndUnlinkConfigFile ( config : Conf ) {
96- let configData
93+ let configData ;
9794
9895 if ( config ?. path ) {
9996 if ( existsSync ( config . path ) ) {
100- configData = JSON . parse ( JSON . stringify ( config ?. store || { } ) ) // NOTE convert prototype object to plain object
101- unlinkSync ( config . path ) // NOTE remove old config file
97+ configData = JSON . parse ( JSON . stringify ( config ?. store || { } ) ) ; // NOTE convert prototype object to plain object
98+ unlinkSync ( config . path ) ; // NOTE remove old config file
10299 }
103100 }
104101
105- return configData
102+ return configData ;
106103 }
107104
108105 private getEncryptedConfig ( configData ?: Record < string , unknown > , skip = false ) {
109106 const getEncryptedDataElseFallBack = ( ) => {
110107 try {
111108 // NOTE reading current code base encrypted file if exist
112- const encryptionKey : any = this . getObfuscationKey ( )
113- this . config = new Conf ( { configName : CONFIG_NAME , encryptionKey } )
109+ const encryptionKey : any = this . getObfuscationKey ( ) ;
110+ this . config = new Conf ( { configName : CONFIG_NAME , encryptionKey } ) ;
114111
115112 if ( Object . keys ( configData || { } ) ?. length ) {
116- this . config . set ( configData ) // NOTE set config data if passed any
113+ this . config . set ( configData ) ; // NOTE set config data if passed any
117114 }
118115 } catch ( error ) {
119116 // NOTE reading old code base encrypted file if exist
120117 try {
121- const config = this . fallbackInit ( )
122- const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config )
123- this . getEncryptedConfig ( oldConfigData , true )
118+ const config = this . fallbackInit ( ) ;
119+ const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config ) ;
120+ this . getEncryptedConfig ( oldConfigData , true ) ;
124121 } catch ( _error ) {
125122 // console.trace(error.message)
126123 }
127124 }
128- }
125+ } ;
129126
130127 try {
131128 if ( skip === false ) {
132- const config = new Conf ( { configName : CONFIG_NAME } )
133- const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config )
134- this . getEncryptedConfig ( oldConfigData , true )
129+ const config = new Conf ( { configName : CONFIG_NAME } ) ;
130+ const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config ) ;
131+ this . getEncryptedConfig ( oldConfigData , true ) ;
135132 } else {
136- getEncryptedDataElseFallBack ( )
133+ getEncryptedDataElseFallBack ( ) ;
137134 }
138135 } catch ( error ) {
139136 // console.trace(error.message)
140137 // NOTE reading current code base encrypted file if exist
141- getEncryptedDataElseFallBack ( )
138+ getEncryptedDataElseFallBack ( ) ;
142139 }
143140
144- return this . config
141+ return this . config ;
145142 }
146143
147144 private getDecryptedConfig ( configData ?: Record < string , unknown > ) {
148145 try {
149- this . config = new Conf ( { configName : CONFIG_NAME } )
146+ this . config = new Conf ( { configName : CONFIG_NAME } ) ;
150147
151148 if ( Object . keys ( configData || { } ) ?. length ) {
152- this . config . set ( configData ) // NOTE set config data if passed any
149+ this . config . set ( configData ) ; // NOTE set config data if passed any
153150 }
154151 } catch ( error ) {
155152 // console.trace(error.message)
156153
157154 try {
158- const encryptionKey : any = this . getObfuscationKey ( )
159- let config = new Conf ( { configName : CONFIG_NAME , encryptionKey } )
160- const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config )
161- this . getDecryptedConfig ( oldConfigData ) // NOTE NOTE reinitialize the config with old data and new decrypted file
155+ const encryptionKey : any = this . getObfuscationKey ( ) ;
156+ let config = new Conf ( { configName : CONFIG_NAME , encryptionKey } ) ;
157+ const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config ) ;
158+ this . getDecryptedConfig ( oldConfigData ) ; // NOTE NOTE reinitialize the config with old data and new decrypted file
162159 } catch ( _error ) {
163160 // console.trace(error.message)
164161
165162 try {
166- const config = this . fallbackInit ( )
167- const _configData = this . getConfigDataAndUnlinkConfigFile ( config )
168- this . getDecryptedConfig ( _configData ) // NOTE reinitialize the config with old data and new decrypted file
163+ const config = this . fallbackInit ( ) ;
164+ const _configData = this . getConfigDataAndUnlinkConfigFile ( config ) ;
165+ this . getDecryptedConfig ( _configData ) ; // NOTE reinitialize the config with old data and new decrypted file
169166 } catch ( __error ) {
170167 // console.trace(error.message)
171168 }
172169 }
173170 }
174171
175- return this . config
172+ return this . config ;
176173 }
177174
178175 get ( key ) : string | any {
@@ -190,7 +187,7 @@ class Config {
190187 }
191188
192189 clear ( ) {
193- this . config . clear ( )
190+ this . config . clear ( ) ;
194191 }
195192}
196193
0 commit comments