@@ -63,6 +63,23 @@ class Config {
63
63
}
64
64
}
65
65
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
+
66
83
removeOldConfigStoreFile ( ) {
67
84
if ( existsSync ( oldConfigPath ) ) {
68
85
unlinkSync ( oldConfigPath ) ; // NOTE remove old configstore file
@@ -110,8 +127,10 @@ class Config {
110
127
private getEncryptedConfig ( configData ?: Record < string , unknown > , skip = false ) {
111
128
const getEncryptedDataElseFallBack = ( ) => {
112
129
try {
130
+
113
131
// NOTE reading current code base encrypted file if exist
114
132
const encryptionKey : any = this . getObfuscationKey ( ) ;
133
+ this . safeDeleteConfigIfInvalid ( oldConfigPath ) ;
115
134
this . config = new Conf ( { configName : CONFIG_NAME , encryptionKey, cwd } ) ;
116
135
117
136
if ( Object . keys ( configData || { } ) ?. length ) {
@@ -133,6 +152,7 @@ class Config {
133
152
134
153
try {
135
154
if ( skip === false ) {
155
+ this . safeDeleteConfigIfInvalid ( oldConfigPath ) ;
136
156
const config = new Conf ( { configName : CONFIG_NAME } ) ;
137
157
const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config ) ;
138
158
this . getEncryptedConfig ( oldConfigData , true ) ;
@@ -150,6 +170,7 @@ class Config {
150
170
151
171
private getDecryptedConfig ( configData ?: Record < string , unknown > ) {
152
172
try {
173
+ this . safeDeleteConfigIfInvalid ( oldConfigPath ) ;
153
174
this . config = new Conf ( { configName : CONFIG_NAME , cwd } ) ;
154
175
155
176
if ( Object . keys ( configData || { } ) ?. length ) {
@@ -160,6 +181,7 @@ class Config {
160
181
161
182
try {
162
183
const encryptionKey : any = this . getObfuscationKey ( ) ;
184
+ this . safeDeleteConfigIfInvalid ( oldConfigPath ) ;
163
185
let config = new Conf ( { configName : CONFIG_NAME , encryptionKey, cwd } ) ;
164
186
const oldConfigData = this . getConfigDataAndUnlinkConfigFile ( config ) ;
165
187
this . getDecryptedConfig ( oldConfigData ) ; // NOTE NOTE reinitialize the config with old data and new decrypted file
0 commit comments