Skip to content

Commit 33ceb4f

Browse files
committed
fix:config
1 parent 0212222 commit 33ceb4f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/contentstack-utilities/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/cli-utilities",
3-
"version": "1.11.0",
3+
"version": "1.11.1",
44
"description": "Utilities for contentstack projects",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

packages/contentstack-utilities/src/config-handler.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)