@@ -63,6 +63,23 @@ class Config {
6363 }
6464 }
6565
66+ isConfigFileValid ( configPath : string ) : boolean {
67+ try {
68+ const content = readFileSync ( configPath , 'utf8' ) ;
69+ JSON . parse ( content ) ;
70+ return true ;
71+ } catch ( e ) {
72+ return false ;
73+ }
74+ }
75+
76+ safeDeleteConfigIfInvalid ( configFilePath : string ) {
77+ if ( existsSync ( configFilePath ) && ! this . isConfigFileValid ( configFilePath ) ) {
78+ console . warn ( chalk . yellow ( `Warning: Detected corrupted config at ${ configFilePath } . Removing...` ) ) ;
79+ unlinkSync ( configFilePath ) ;
80+ }
81+ }
82+
6683 removeOldConfigStoreFile ( ) {
6784 if ( existsSync ( oldConfigPath ) ) {
6885 unlinkSync ( oldConfigPath ) ; // NOTE remove old configstore file
@@ -110,8 +127,10 @@ class Config {
110127 private getEncryptedConfig ( configData ?: Record < string , unknown > , skip = false ) {
111128 const getEncryptedDataElseFallBack = ( ) => {
112129 try {
130+
113131 // NOTE reading current code base encrypted file if exist
114132 const encryptionKey : any = this . getObfuscationKey ( ) ;
133+ this . safeDeleteConfigIfInvalid ( oldConfigPath ) ;
115134 this . config = new Conf ( { configName : CONFIG_NAME , encryptionKey, cwd } ) ;
116135
117136 if ( Object . keys ( configData || { } ) ?. length ) {
@@ -133,6 +152,7 @@ class Config {
133152
134153 try {
135154 if ( skip === false ) {
155+ this . safeDeleteConfigIfInvalid ( oldConfigPath ) ;
136156 const config = new Conf ( { configName : CONFIG_NAME } ) ;
137157 const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config ) ;
138158 this . getEncryptedConfig ( oldConfigData , true ) ;
@@ -150,6 +170,7 @@ class Config {
150170
151171 private getDecryptedConfig ( configData ?: Record < string , unknown > ) {
152172 try {
173+ this . safeDeleteConfigIfInvalid ( oldConfigPath ) ;
153174 this . config = new Conf ( { configName : CONFIG_NAME , cwd } ) ;
154175
155176 if ( Object . keys ( configData || { } ) ?. length ) {
@@ -160,6 +181,7 @@ class Config {
160181
161182 try {
162183 const encryptionKey : any = this . getObfuscationKey ( ) ;
184+ this . safeDeleteConfigIfInvalid ( oldConfigPath ) ;
163185 let config = new Conf ( { configName : CONFIG_NAME , encryptionKey, cwd } ) ;
164186 const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config ) ;
165187 this . getDecryptedConfig ( oldConfigData ) ; // NOTE NOTE reinitialize the config with old data and new decrypted file
0 commit comments